Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Force closing PIL image files #972

Merged
merged 2 commits into from

4 participants

@cgohlke

This fixes some ResourceWarning: unclosed file warnings under Python 3.2

@pelson pelson commented on the diff
lib/matplotlib/image.py
@@ -1185,12 +1185,20 @@ def imread(fname, format=None):
can be used with :func:`~matplotlib.pyplot.imshow`.
"""
- def pilread():
+ def pilread(fname):
"""try to load the image with PIL or return None"""
@pelson Collaborator
pelson added a note

Since you have extended the capability of this function (file handles are now supported), can you improve the doctoring a little. Perhaps the variable name fname is no longer appropriate - maybe something like img_file?

@cgohlke
cgohlke added a note

I think the capability of the function is the same as before. It just works around another bug in PIL, which does not seem to close all the files it opens.

@pelson Collaborator
pelson added a note

ok. but the variable naming statement still holds (fname could be a file handle or a filename).

@cgohlke
cgohlke added a note

I'm not sure it is a good idea to rename the variable. fname is used as an argument in the outer function. Renaming it there would break the API. Renaming it in pilread only would be confusing I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/matplotlib/image.py
((6 lines not shown))
"""try to load the image with PIL or return None"""
- try: from PIL import Image
- except ImportError: return None
- image = Image.open( fname )
- return pil_to_array(image)
+ try:
+ from PIL import Image
+ except ImportError:
+ return None
+ if cbook.is_string_like(fname):
+ # force close the file after reading the image
+ with open(fname, "rb") as fp:
@pelson Collaborator
pelson added a note

fh is the traditional "file" handle/pointer variable name.

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

+1

@mdboom
Owner

Looks good.

@WeatherGod
Collaborator

what is holding up this PR?

@pelson
Collaborator

Nothing.

@pelson pelson merged commit 7a6897e into matplotlib:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 29, 2012
  1. @cgohlke

    Force closing PIL image files

    cgohlke authored
Commits on Jun 30, 2012
  1. @cgohlke

    Rename variable

    cgohlke authored
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 6 deletions.
  1. +14 −6 lib/matplotlib/image.py
View
20 lib/matplotlib/image.py
@@ -1185,12 +1185,20 @@ def imread(fname, format=None):
can be used with :func:`~matplotlib.pyplot.imshow`.
"""
- def pilread():
+ def pilread(fname):
"""try to load the image with PIL or return None"""
@pelson Collaborator
pelson added a note

Since you have extended the capability of this function (file handles are now supported), can you improve the doctoring a little. Perhaps the variable name fname is no longer appropriate - maybe something like img_file?

@cgohlke
cgohlke added a note

I think the capability of the function is the same as before. It just works around another bug in PIL, which does not seem to close all the files it opens.

@pelson Collaborator
pelson added a note

ok. but the variable naming statement still holds (fname could be a file handle or a filename).

@cgohlke
cgohlke added a note

I'm not sure it is a good idea to rename the variable. fname is used as an argument in the outer function. Renaming it there would break the API. Renaming it in pilread only would be confusing I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- try: from PIL import Image
- except ImportError: return None
- image = Image.open( fname )
- return pil_to_array(image)
+ try:
+ from PIL import Image
+ except ImportError:
+ return None
+ if cbook.is_string_like(fname):
+ # force close the file after reading the image
+ with open(fname, "rb") as fh:
+ image = Image.open(fh)
+ return pil_to_array(image)
+ else:
+ image = Image.open(fname)
+ return pil_to_array(image)
handlers = {'png' :_png.read_png, }
if format is None:
@@ -1206,7 +1214,7 @@ def pilread():
ext = format
if ext not in handlers.iterkeys():
- im = pilread()
+ im = pilread(fname)
if im is None:
raise ValueError('Only know how to handle extensions: %s; with PIL installed matplotlib can handle more images' % handlers.keys())
return im
Something went wrong with that request. Please try again.