mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-24 17:06:16 +03:00
Resolve __fp when closing and deleting
This commit is contained in:
parent
50d49d619b
commit
47775f3d50
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
#
|
||||
# --------------------------------------------------------------------
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
#
|
||||
# --------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue
Block a user