Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add detection of file extension for file-like objects #963

Merged
merged 1 commit into from

4 participants

@cgohlke

Backported from #616

@pelson
Collaborator

@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.

@pelson pelson commented on the diff
lib/matplotlib/image.py
@@ -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'):
@pelson Collaborator
pelson added a note

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.
@efiring Owner
efiring added a note

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?

@pelson Collaborator
pelson added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@WeatherGod
Collaborator

Since v1.1.1 is passed. What is the status of this PR?

@pelson
Collaborator

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.

@WeatherGod
Collaborator

I'll merge it anyway just in case there is ever anymore releases on this branch.

@WeatherGod WeatherGod merged commit cbb11da into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 23, 2012
  1. @cgohlke
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 0 deletions.
  1. +3 −0  lib/matplotlib/image.py
View
3  lib/matplotlib/image.py
@@ -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'):
@pelson Collaborator
pelson added a note

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.
@efiring Owner
efiring added a note

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?

@pelson Collaborator
pelson added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ basename, ext = os.path.splitext(fname.name)
+ ext = ext.lower()[1:]
else:
ext = 'png'
else:
Something went wrong with that request. Please try again.