mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +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.
201 lines
6.6 KiB
Python
201 lines
6.6 KiB
Python
import os
|
|
from glob import glob
|
|
from itertools import product
|
|
|
|
from PIL import Image
|
|
|
|
from .helper import PillowTestCase
|
|
|
|
_TGA_DIR = os.path.join("Tests", "images", "tga")
|
|
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
|
|
|
|
|
|
class TestFileTga(PillowTestCase):
|
|
|
|
_MODES = ("L", "LA", "P", "RGB", "RGBA")
|
|
_ORIGINS = ("tl", "bl")
|
|
|
|
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
|
|
|
|
def test_sanity(self):
|
|
for mode in self._MODES:
|
|
png_paths = glob(
|
|
os.path.join(_TGA_DIR_COMMON, "*x*_{}.png".format(mode.lower()))
|
|
)
|
|
|
|
for png_path in png_paths:
|
|
reference_im = Image.open(png_path)
|
|
self.assertEqual(reference_im.mode, mode)
|
|
|
|
path_no_ext = os.path.splitext(png_path)[0]
|
|
for origin, rle in product(self._ORIGINS, (True, False)):
|
|
tga_path = "{}_{}_{}.tga".format(
|
|
path_no_ext, origin, "rle" if rle else "raw"
|
|
)
|
|
|
|
original_im = Image.open(tga_path)
|
|
self.assertEqual(original_im.format, "TGA")
|
|
self.assertEqual(original_im.get_format_mimetype(), "image/x-tga")
|
|
if rle:
|
|
self.assertEqual(original_im.info["compression"], "tga_rle")
|
|
self.assertEqual(
|
|
original_im.info["orientation"],
|
|
self._ORIGIN_TO_ORIENTATION[origin],
|
|
)
|
|
if mode == "P":
|
|
self.assertEqual(
|
|
original_im.getpalette(), reference_im.getpalette()
|
|
)
|
|
|
|
self.assert_image_equal(original_im, reference_im)
|
|
|
|
# Generate a new test name every time so the
|
|
# test will not fail with permission error
|
|
# on Windows.
|
|
out = self.tempfile("temp.tga")
|
|
|
|
original_im.save(out, rle=rle)
|
|
saved_im = Image.open(out)
|
|
if rle:
|
|
self.assertEqual(
|
|
saved_im.info["compression"],
|
|
original_im.info["compression"],
|
|
)
|
|
self.assertEqual(
|
|
saved_im.info["orientation"], original_im.info["orientation"]
|
|
)
|
|
if mode == "P":
|
|
self.assertEqual(
|
|
saved_im.getpalette(), original_im.getpalette()
|
|
)
|
|
|
|
self.assert_image_equal(saved_im, original_im)
|
|
|
|
def test_id_field(self):
|
|
# tga file with id field
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
|
|
# Act
|
|
with Image.open(test_file) as im:
|
|
|
|
# Assert
|
|
self.assertEqual(im.size, (100, 100))
|
|
|
|
def test_id_field_rle(self):
|
|
# tga file with id field
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
|
|
# Act
|
|
with Image.open(test_file) as im:
|
|
|
|
# Assert
|
|
self.assertEqual(im.size, (199, 199))
|
|
|
|
def test_save(self):
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
im = Image.open(test_file)
|
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
# Save
|
|
im.save(out)
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.size, (100, 100))
|
|
self.assertEqual(test_im.info["id_section"], im.info["id_section"])
|
|
|
|
# RGBA save
|
|
im.convert("RGBA").save(out)
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.size, (100, 100))
|
|
|
|
def test_save_id_section(self):
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
im = Image.open(test_file)
|
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
# Check there is no id section
|
|
im.save(out)
|
|
with Image.open(out) as test_im:
|
|
self.assertNotIn("id_section", test_im.info)
|
|
|
|
# Save with custom id section
|
|
im.save(out, id_section=b"Test content")
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.info["id_section"], b"Test content")
|
|
|
|
# Save with custom id section greater than 255 characters
|
|
id_section = b"Test content" * 25
|
|
self.assert_warning(UserWarning, lambda: im.save(out, id_section=id_section))
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.info["id_section"], id_section[:255])
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
with Image.open(test_file) as im:
|
|
|
|
# Save with no id section
|
|
im.save(out, id_section="")
|
|
with Image.open(out) as test_im:
|
|
self.assertNotIn("id_section", test_im.info)
|
|
|
|
def test_save_orientation(self):
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
im = Image.open(test_file)
|
|
self.assertEqual(im.info["orientation"], -1)
|
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
im.save(out, orientation=1)
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.info["orientation"], 1)
|
|
|
|
def test_save_rle(self):
|
|
test_file = "Tests/images/rgb32rle.tga"
|
|
im = Image.open(test_file)
|
|
self.assertEqual(im.info["compression"], "tga_rle")
|
|
|
|
out = self.tempfile("temp.tga")
|
|
|
|
# Save
|
|
im.save(out)
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.size, (199, 199))
|
|
self.assertEqual(test_im.info["compression"], "tga_rle")
|
|
|
|
# Save without compression
|
|
im.save(out, compression=None)
|
|
with Image.open(out) as test_im:
|
|
self.assertNotIn("compression", test_im.info)
|
|
|
|
# RGBA save
|
|
im.convert("RGBA").save(out)
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.size, (199, 199))
|
|
|
|
test_file = "Tests/images/tga_id_field.tga"
|
|
im = Image.open(test_file)
|
|
self.assertNotIn("compression", im.info)
|
|
|
|
# Save with compression
|
|
im.save(out, compression="tga_rle")
|
|
with Image.open(out) as test_im:
|
|
self.assertEqual(test_im.info["compression"], "tga_rle")
|
|
|
|
def test_save_l_transparency(self):
|
|
# There are 559 transparent pixels in la.tga.
|
|
num_transparent = 559
|
|
|
|
in_file = "Tests/images/la.tga"
|
|
im = Image.open(in_file)
|
|
self.assertEqual(im.mode, "LA")
|
|
self.assertEqual(im.getchannel("A").getcolors()[0][0], num_transparent)
|
|
|
|
out = self.tempfile("temp.tga")
|
|
im.save(out)
|
|
|
|
test_im = Image.open(out)
|
|
self.assertEqual(test_im.mode, "LA")
|
|
self.assertEqual(test_im.getchannel("A").getcolors()[0][0], num_transparent)
|
|
|
|
self.assert_image_equal(im, test_im)
|