mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-11 12:17:14 +03:00
4cd4adddc3
Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
from PIL import Image, ImageSequence, TiffImagePlugin
|
|
|
|
from .helper import PillowTestCase, hopper
|
|
|
|
|
|
class TestImageSequence(PillowTestCase):
|
|
def test_sanity(self):
|
|
|
|
test_file = self.tempfile("temp.im")
|
|
|
|
im = hopper("RGB")
|
|
im.save(test_file)
|
|
|
|
seq = ImageSequence.Iterator(im)
|
|
|
|
index = 0
|
|
for frame in seq:
|
|
self.assert_image_equal(im, frame)
|
|
self.assertEqual(im.tell(), index)
|
|
index += 1
|
|
|
|
self.assertEqual(index, 1)
|
|
|
|
self.assertRaises(AttributeError, ImageSequence.Iterator, 0)
|
|
|
|
def test_iterator(self):
|
|
with Image.open("Tests/images/multipage.tiff") as im:
|
|
i = ImageSequence.Iterator(im)
|
|
for index in range(0, im.n_frames):
|
|
self.assertEqual(i[index], next(i))
|
|
self.assertRaises(IndexError, lambda: i[index + 1])
|
|
self.assertRaises(StopIteration, next, i)
|
|
|
|
def test_iterator_min_frame(self):
|
|
with Image.open("Tests/images/hopper.psd") as im:
|
|
i = ImageSequence.Iterator(im)
|
|
for index in range(1, im.n_frames):
|
|
self.assertEqual(i[index], next(i))
|
|
|
|
def _test_multipage_tiff(self):
|
|
with Image.open("Tests/images/multipage.tiff") as im:
|
|
for index, frame in enumerate(ImageSequence.Iterator(im)):
|
|
frame.load()
|
|
self.assertEqual(index, im.tell())
|
|
frame.convert("RGB")
|
|
|
|
def test_tiff(self):
|
|
self._test_multipage_tiff()
|
|
|
|
def test_libtiff(self):
|
|
codecs = dir(Image.core)
|
|
|
|
if "libtiff_encoder" not in codecs or "libtiff_decoder" not in codecs:
|
|
self.skipTest("tiff support not available")
|
|
|
|
TiffImagePlugin.READ_LIBTIFF = True
|
|
self._test_multipage_tiff()
|
|
TiffImagePlugin.READ_LIBTIFF = False
|
|
|
|
def test_consecutive(self):
|
|
with Image.open("Tests/images/multipage.tiff") as im:
|
|
firstFrame = None
|
|
for frame in ImageSequence.Iterator(im):
|
|
if firstFrame is None:
|
|
firstFrame = frame.copy()
|
|
for frame in ImageSequence.Iterator(im):
|
|
self.assert_image_equal(frame, firstFrame)
|
|
break
|
|
|
|
def test_palette_mmap(self):
|
|
# Using mmap in ImageFile can require to reload the palette.
|
|
with Image.open("Tests/images/multipage-mmap.tiff") as im:
|
|
color1 = im.getpalette()[0:3]
|
|
im.seek(0)
|
|
color2 = im.getpalette()[0:3]
|
|
self.assertEqual(color1, color2)
|
|
|
|
def test_all_frames(self):
|
|
# Test a single image
|
|
with Image.open("Tests/images/iss634.gif") as im:
|
|
ims = ImageSequence.all_frames(im)
|
|
|
|
self.assertEqual(len(ims), 42)
|
|
for i, im_frame in enumerate(ims):
|
|
self.assertFalse(im_frame is im)
|
|
|
|
im.seek(i)
|
|
self.assert_image_equal(im, im_frame)
|
|
|
|
# Test a series of images
|
|
ims = ImageSequence.all_frames([im, hopper(), im])
|
|
self.assertEqual(len(ims), 85)
|
|
|
|
# Test an operation
|
|
ims = ImageSequence.all_frames(im, lambda im_frame: im_frame.rotate(90))
|
|
for i, im_frame in enumerate(ims):
|
|
im.seek(i)
|
|
self.assert_image_equal(im.rotate(90), im_frame)
|