Close internal fp when closing and deleting

This commit is contained in:
Andrew Murray 2019-04-01 00:02:22 +11:00
parent ea02e28adb
commit e1e35cc819
2 changed files with 17 additions and 2 deletions

View File

@ -17,6 +17,12 @@ class TestImagePsd(PillowTestCase):
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"

View File

@ -129,7 +129,7 @@ class PsdImageFile(ImageFile.ImageFile):
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
@ -151,7 +151,7 @@ class PsdImageFile(ImageFile.ImageFile):
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")
@ -168,6 +168,15 @@ class PsdImageFile(ImageFile.ImageFile):
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