Skip to content
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

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

Merged
merged 1 commit into from Jul 18, 2012
Merged

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

merged 1 commit into from Jul 18, 2012

Conversation

cgohlke
Copy link
Contributor

@cgohlke cgohlke commented Jun 23, 2012

Backported from #616

@pelson
Copy link
Member

pelson commented Jun 24, 2012

@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'):
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.

@WeatherGod
Copy link
Member

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

@pelson
Copy link
Member

pelson commented Jul 18, 2012

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
Copy link
Member

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

WeatherGod added a commit that referenced this pull request Jul 18, 2012
Add detection of file extension for file-like objects
@WeatherGod WeatherGod merged commit cbb11da into matplotlib:v1.1.x Jul 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants