Skip to content

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

Merged
merged 1 commit into from
Jul 18, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

basename, ext = os.path.splitext(fname.name)
ext = ext.lower()[1:]
else:
ext = 'png'
else:
Expand Down