Skip to content

Commit

Permalink
Close internal fp when closing and deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 31, 2019
1 parent 117b7c2 commit 179771b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Tests/test_file_psd.py
Expand Up @@ -17,6 +17,12 @@ def test_sanity(self):
im2 = hopper()
self.assert_image_similar(im, im2, 4.8)

def test_unclosed_file(self):
def open():
im = Image.open(test_file)
im.load()
self.assert_warning(None, open)

def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg"

Expand Down
13 changes: 11 additions & 2 deletions src/PIL/PsdImagePlugin.py
Expand Up @@ -127,7 +127,7 @@ def _open(self):
self.tile = _maketile(self.fp, mode, (0, 0) + self.size, channels)

# keep the file open
self._fp = self.fp
self.__fp = self.fp
self.frame = 1
self._min_frame = 1

Expand All @@ -149,7 +149,7 @@ def seek(self, layer):
self.mode = mode
self.tile = tile
self.frame = layer
self.fp = self._fp
self.fp = self.__fp
return name, bbox
except IndexError:
raise EOFError("no such layer")
Expand All @@ -167,6 +167,15 @@ def load_prepare(self):
if self.mode == "P":
Image.Image.load(self)

def _close__fp(self):
try:
if self.__fp != self.fp:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None


def _layerinfo(file):
# read layerinfo block
Expand Down

0 comments on commit 179771b

Please sign in to comment.