Pillow/Tests/test_bmp_reference.py

112 lines
3.9 KiB
Python
Raw Normal View History

import os
import pytest
from PIL import Image
from .helper import PillowTestCase, assert_image_similar
2019-06-13 18:53:42 +03:00
base = os.path.join("Tests", "images", "bmp")
2014-06-10 13:10:47 +04:00
class TestBmpReference(PillowTestCase):
2019-06-13 18:53:42 +03:00
def get_files(self, d, ext=".bmp"):
return [
os.path.join(base, d, f)
for f in os.listdir(os.path.join(base, d))
if ext in f
]
2014-06-10 13:10:47 +04:00
def test_bad(self):
""" These shouldn't crash/dos, but they shouldn't return anything
either """
2019-06-13 18:53:42 +03:00
for f in self.get_files("b"):
2018-11-09 03:35:08 +03:00
def open(f):
try:
Improve handling of file resources 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.
2019-05-25 19:30:58 +03:00
with Image.open(f) as im:
im.load()
2018-11-09 03:35:08 +03:00
except Exception: # as msg:
pass
# Assert that there is no unclosed file warning
pytest.warns(None, open, f)
2014-06-10 13:10:47 +04:00
def test_questionable(self):
2016-04-13 17:08:44 +03:00
""" These shouldn't crash/dos, but it's not well defined that these
2014-06-10 13:10:47 +04:00
are in spec """
supported = [
"pal8os2v2.bmp",
"rgb24prof.bmp",
"pal1p1.bmp",
"pal8offs.bmp",
"rgb24lprof.bmp",
"rgb32fakealpha.bmp",
"rgb24largepal.bmp",
"pal8os2sp.bmp",
"rgb32bf-xbgr.bmp",
]
2019-06-13 18:53:42 +03:00
for f in self.get_files("q"):
2014-06-10 13:10:47 +04:00
try:
Improve handling of file resources 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.
2019-05-25 19:30:58 +03:00
with Image.open(f) as im:
im.load()
if os.path.basename(f) not in supported:
2019-06-13 18:53:42 +03:00
print("Please add %s to the partially supported bmp specs." % f)
2014-06-10 13:10:47 +04:00
except Exception: # as msg:
if os.path.basename(f) in supported:
raise
2014-06-10 13:10:47 +04:00
def test_good(self):
""" These should all work. There's a set of target files in the
html directory that we can compare against. """
2014-06-10 13:10:47 +04:00
# Target files, if they're not just replacing the extension
2019-06-13 18:53:42 +03:00
file_map = {
"pal1wb.bmp": "pal1.png",
"pal4rle.bmp": "pal4.png",
"pal8-0.bmp": "pal8.png",
"pal8rle.bmp": "pal8.png",
"pal8topdown.bmp": "pal8.png",
"pal8nonsquare.bmp": "pal8nonsquare-v.png",
"pal8os2.bmp": "pal8.png",
"pal8os2sp.bmp": "pal8.png",
"pal8os2v2.bmp": "pal8.png",
"pal8os2v2-16.bmp": "pal8.png",
"pal8v4.bmp": "pal8.png",
"pal8v5.bmp": "pal8.png",
"rgb16-565pal.bmp": "rgb16-565.png",
"rgb24pal.bmp": "rgb24.png",
"rgb32.bmp": "rgb24.png",
"rgb32bf.bmp": "rgb24.png",
}
2014-06-10 13:10:47 +04:00
def get_compare(f):
2015-04-24 11:24:52 +03:00
name = os.path.split(f)[1]
2014-06-10 13:10:47 +04:00
if name in file_map:
2019-06-13 18:53:42 +03:00
return os.path.join(base, "html", file_map[name])
2015-04-24 11:24:52 +03:00
name = os.path.splitext(name)[0]
2019-06-13 18:53:42 +03:00
return os.path.join(base, "html", "%s.png" % name)
2014-06-10 13:10:47 +04:00
2019-06-13 18:53:42 +03:00
for f in self.get_files("g"):
2014-06-10 13:10:47 +04:00
try:
Improve handling of file resources 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.
2019-05-25 19:30:58 +03:00
with Image.open(f) as im:
im.load()
with Image.open(get_compare(f)) as compare:
compare.load()
if im.mode == "P":
# assert image similar doesn't really work
# with paletized image, since the palette might
# be differently ordered for an equivalent image.
im = im.convert("RGBA")
compare = im.convert("RGBA")
assert_image_similar(im, compare, 5)
2014-06-10 13:10:47 +04:00
except Exception as msg:
# there are three here that are unsupported:
2019-06-13 18:53:42 +03:00
unsupported = (
os.path.join(base, "g", "rgb32bf.bmp"),
os.path.join(base, "g", "pal8rle.bmp"),
os.path.join(base, "g", "pal4rle.bmp"),
)
2014-06-10 13:10:47 +04:00
if f not in unsupported:
self.fail("Unsupported Image {}: {}".format(f, msg))