Resolve __fp when closing and deleting

This commit is contained in:
Andrew Murray 2018-11-17 21:56:06 +11:00
parent 50d49d619b
commit 47775f3d50
16 changed files with 110 additions and 1 deletions

View File

@ -20,6 +20,12 @@ class TestFileDcx(PillowTestCase):
orig = hopper()
self.assert_image_equal(im, orig)
def test_unclosed_file(self):
def open():
im = Image.open(TEST_FILE)
im.load()
self.assert_warning(None, open)
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError,

View File

@ -27,6 +27,12 @@ class TestFileFli(PillowTestCase):
self.assertEqual(im.info["duration"], 71)
self.assertTrue(im.is_animated)
def test_unclosed_file(self):
def open():
im = Image.open(static_test_file)
im.load()
self.assert_warning(None, open)
def test_tell(self):
# Arrange
im = Image.open(static_test_file)

View File

@ -33,6 +33,12 @@ class TestFileGif(PillowTestCase):
self.assertEqual(im.format, "GIF")
self.assertEqual(im.info["version"], b"GIF89a")
def test_unclosed_file(self):
def open():
im = Image.open(TEST_GIF)
im.load()
self.assert_warning(None, open)
def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg"

View File

@ -15,6 +15,12 @@ class TestFileIm(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "IM")
def test_unclosed_file(self):
def open():
im = Image.open(TEST_IM)
im.load()
self.assert_warning(None, open)
def test_tell(self):
# Arrange
im = Image.open(TEST_IM)

View File

@ -31,6 +31,12 @@ class TestFileMpo(PillowTestCase):
self.assertEqual(im.size, (640, 480))
self.assertEqual(im.format, "MPO")
def test_unclosed_file(self):
def open():
im = Image.open(test_files[0])
im.load()
self.assert_warning(None, open)
def test_app(self):
for test_file in test_files:
# Test APP/COM reader (@PIL135)

View File

@ -18,6 +18,12 @@ class TestImageSpider(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "SPIDER")
def test_unclosed_file(self):
def open():
im = Image.open(TEST_FILE)
im.load()
self.assert_warning(None, open)
def test_save(self):
# Arrange
temp = self.tempfile('temp.spider')

View File

@ -40,6 +40,12 @@ class TestFileTiff(PillowTestCase):
hopper("I").save(filename)
Image.open(filename)
def test_unclosed_file(self):
def open():
im = Image.open("Tests/images/multipage.tiff")
im.load()
self.assert_warning(None, open)
def test_mac_tiff(self):
# Read RGBa images from macOS [@PIL136]

View File

@ -81,6 +81,14 @@ class DcxImageFile(PcxImageFile):
def tell(self):
return self.frame
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
Image.register_open(DcxImageFile.format, DcxImageFile, _accept)

View File

@ -154,6 +154,14 @@ class FliImageFile(ImageFile.ImageFile):
def tell(self):
return self.__frame
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
#
# registry

View File

@ -296,6 +296,14 @@ class GifImageFile(ImageFile.ImageFile):
self.im = self._prev_im
self._prev_im = self.im.copy()
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
# --------------------------------------------------------------------
# Write GIF files

View File

@ -290,6 +290,14 @@ class ImImageFile(ImageFile.ImageFile):
def tell(self):
return self.frame
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
#
# --------------------------------------------------------------------
# Save IM files

View File

@ -596,6 +596,8 @@ class Image(object):
try:
self.fp.close()
self.fp = None
if hasattr(self, "_close__fp"):
self._close__fp()
except Exception as msg:
logger.debug("Error closing: %s", msg)
@ -613,6 +615,8 @@ class Image(object):
and self.fp and self._exclusive_fp):
self.fp.close()
self.fp = None
if hasattr(self, "_close__fp"):
self._close__fp()
def _copy(self):
self.load()

View File

@ -95,9 +95,16 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
self.frame = frame
def tell(self):
return self.frame
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
#
# --------------------------------------------------------------------

View File

@ -84,6 +84,14 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
def tell(self):
return self.__frame
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
# ---------------------------------------------------------------------
# Registry stuff

View File

@ -193,6 +193,14 @@ class SpiderImageFile(ImageFile.ImageFile):
from PIL import ImageTk
return ImageTk.PhotoImage(self.convert2byte(), palette=256)
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
# --------------------------------------------------------------------
# Image series

View File

@ -1347,6 +1347,14 @@ class TiffImageFile(ImageFile.ImageFile):
palette = [o8(b // 256) for b in self.tag_v2[COLORMAP]]
self.palette = ImagePalette.raw("RGB;L", b"".join(palette))
def _close__fp(self):
try:
self.__fp.close()
except AttributeError:
pass
finally:
self.__fp = None
#
# --------------------------------------------------------------------