-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add detection of file extension for file-like objects #963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@cgohlke: I think this change is fine in terms of implementation. But, I do think it is worthwhile adding a test which will exercise this bit of code. |
@@ -1170,6 +1170,9 @@ def pilread(): | |||
if cbook.is_string_like(fname): | |||
basename, ext = os.path.splitext(fname) | |||
ext = ext.lower()[1:] | |||
elif hasattr(fname, 'name'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because fname can be both a string or a file-like, this line reads a little strangely. Would you mind adding a comment? Something along the lines of:
if we've been given a file-like object, and it has a name defined, get the extension from the name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to quibble, but I think that in the context of the few lines above, little or no comment is needed. At most, "file-like object: check for name with extension" or something like that.
This changeset leaves in place the likelihood of somewhat obscure error messages if the file name lacks an extension.
One could use something like this:
try: # file name basename, ext = os.path.splitext(fname) except AttributeError: try: # file-like object basename, ext = os.path.splitext(fname.name) except AttributeError: pass try: ext = ext.lower()[1:] except IndexError, NameError: ext = "png" # No extension, or no name provided: try png.
Too ugly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too ugly?
There is a lot to hold on to in your head. And even then passing a fname='broken.'
would result in no extension being identified.
Apart from the (admittedly necessary) use of is_string_like
, I have no major problem with this code block. To address the IndexError
you are referring to, I would probably factor out the indexing and adding a line before if ext not in handlers.keys():
to the tone of:
if ext.startswith('.'):
ext = ext[1:]
And depending on if we actually care about users who might pass format=''
, I would probably drop the else: ext='png'
out of the if block and do:
if not ext:
ext = 'png'
Later on.
Of course, this is a backport, so I'm not really advocating a structural change for this PR. I do think, however, a test would be beneficial.
Since v1.1.1 is passed. What is the status of this PR? |
Since it was a backport anyway, I would say that it is no longer needed. I could equally be merged or closed as it will have practically the same effect. |
I'll merge it anyway just in case there is ever anymore releases on this branch. |
Add detection of file extension for file-like objects
Backported from #616