mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-14 05:36:48 +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.
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
from PIL import Image
|
|
|
|
from .helper import PillowTestCase, hopper
|
|
|
|
TEST_FILE = "Tests/images/hopper.ppm"
|
|
|
|
ORIGINAL_LIMIT = Image.MAX_IMAGE_PIXELS
|
|
|
|
|
|
class TestDecompressionBomb(PillowTestCase):
|
|
def tearDown(self):
|
|
Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT
|
|
|
|
def test_no_warning_small_file(self):
|
|
# Implicit assert: no warning.
|
|
# A warning would cause a failure.
|
|
with Image.open(TEST_FILE):
|
|
pass
|
|
|
|
def test_no_warning_no_limit(self):
|
|
# Arrange
|
|
# Turn limit off
|
|
Image.MAX_IMAGE_PIXELS = None
|
|
self.assertIsNone(Image.MAX_IMAGE_PIXELS)
|
|
|
|
# Act / Assert
|
|
# Implicit assert: no warning.
|
|
# A warning would cause a failure.
|
|
with Image.open(TEST_FILE):
|
|
pass
|
|
|
|
def test_warning(self):
|
|
# Set limit to trigger warning on the test file
|
|
Image.MAX_IMAGE_PIXELS = 128 * 128 - 1
|
|
self.assertEqual(Image.MAX_IMAGE_PIXELS, 128 * 128 - 1)
|
|
|
|
def open():
|
|
with Image.open(TEST_FILE):
|
|
pass
|
|
|
|
self.assert_warning(Image.DecompressionBombWarning, open)
|
|
|
|
def test_exception(self):
|
|
# Set limit to trigger exception on the test file
|
|
Image.MAX_IMAGE_PIXELS = 64 * 128 - 1
|
|
self.assertEqual(Image.MAX_IMAGE_PIXELS, 64 * 128 - 1)
|
|
|
|
with self.assertRaises(Image.DecompressionBombError):
|
|
with Image.open(TEST_FILE):
|
|
pass
|
|
|
|
def test_exception_ico(self):
|
|
with self.assertRaises(Image.DecompressionBombError):
|
|
Image.open("Tests/images/decompression_bomb.ico")
|
|
|
|
def test_exception_gif(self):
|
|
with self.assertRaises(Image.DecompressionBombError):
|
|
Image.open("Tests/images/decompression_bomb.gif")
|
|
|
|
|
|
class TestDecompressionCrop(PillowTestCase):
|
|
def setUp(self):
|
|
self.src = hopper()
|
|
self.addCleanup(self.src.close)
|
|
Image.MAX_IMAGE_PIXELS = self.src.height * self.src.width * 4 - 1
|
|
|
|
def tearDown(self):
|
|
Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT
|
|
|
|
def testEnlargeCrop(self):
|
|
# Crops can extend the extents, therefore we should have the
|
|
# same decompression bomb warnings on them.
|
|
box = (0, 0, self.src.width * 2, self.src.height * 2)
|
|
self.assert_warning(Image.DecompressionBombWarning, self.src.crop, box)
|
|
|
|
def test_crop_decompression_checks(self):
|
|
|
|
im = Image.new("RGB", (100, 100))
|
|
|
|
good_values = ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990))
|
|
|
|
warning_values = ((-160, -160, 99, 99), (160, 160, -99, -99))
|
|
|
|
error_values = ((-99909, -99990, 99999, 99999), (99909, 99990, -99999, -99999))
|
|
|
|
for value in good_values:
|
|
self.assertEqual(im.crop(value).size, (9, 9))
|
|
|
|
for value in warning_values:
|
|
self.assert_warning(Image.DecompressionBombWarning, im.crop, value)
|
|
|
|
for value in error_values:
|
|
with self.assertRaises(Image.DecompressionBombError):
|
|
im.crop(value)
|