Pillow/Tests/helper.py

297 lines
7.7 KiB
Python
Raw Normal View History

"""
Helper functions.
"""
import logging
import os
import shutil
import sys
2020-05-25 23:21:51 +03:00
import sysconfig
import tempfile
from io import BytesIO
import pytest
2020-09-01 20:16:46 +03:00
from PIL import Image, ImageMath, features
2017-11-17 22:47:05 +03:00
logger = logging.getLogger(__name__)
HAS_UPLOADER = False
2019-06-13 18:53:42 +03:00
if os.environ.get("SHOW_ERRORS", None):
2018-01-27 09:07:24 +03:00
# local img.show for errors.
2018-03-06 11:53:07 +03:00
HAS_UPLOADER = True
2018-03-03 12:54:00 +03:00
class test_image_results:
2019-10-12 13:59:03 +03:00
@staticmethod
def upload(a, b):
a.show()
b.show()
2019-06-13 18:53:42 +03:00
2019-10-12 13:59:03 +03:00
elif "GITHUB_ACTIONS" in os.environ:
HAS_UPLOADER = True
class test_image_results:
@staticmethod
def upload(a, b):
dir_errors = os.path.join(os.path.dirname(__file__), "errors")
os.makedirs(dir_errors, exist_ok=True)
tmpdir = tempfile.mkdtemp(dir=dir_errors)
a.save(os.path.join(tmpdir, "a.png"))
b.save(os.path.join(tmpdir, "b.png"))
return tmpdir
else:
try:
import test_image_results
2019-06-13 18:53:42 +03:00
HAS_UPLOADER = True
except ImportError:
pass
def convert_to_comparable(a, b):
new_a, new_b = a, b
2019-06-13 18:53:42 +03:00
if a.mode == "P":
new_a = Image.new("L", a.size)
new_b = Image.new("L", b.size)
new_a.putdata(a.getdata())
new_b.putdata(b.getdata())
2019-06-13 18:53:42 +03:00
elif a.mode == "I;16":
new_a = a.convert("I")
new_b = b.convert("I")
return new_a, new_b
def assert_deep_equal(a, b, msg=None):
try:
assert len(a) == len(b), msg or f"got length {len(a)}, expected {len(b)}"
except Exception:
assert a == b, msg
def assert_image(im, mode, size, msg=None):
if mode is not None:
assert im.mode == mode, (
msg or f"got mode {repr(im.mode)}, expected {repr(mode)}"
2019-06-13 18:53:42 +03:00
)
if size is not None:
assert im.size == size, (
msg or f"got size {repr(im.size)}, expected {repr(size)}"
2019-06-13 18:53:42 +03:00
)
def assert_image_equal(a, b, msg=None):
assert a.mode == b.mode, msg or f"got mode {repr(a.mode)}, expected {repr(b.mode)}"
assert a.size == b.size, msg or f"got size {repr(a.size)}, expected {repr(b.size)}"
if a.tobytes() != b.tobytes():
if HAS_UPLOADER:
try:
url = test_image_results.upload(a, b)
logger.error(f"Url for test images: {url}")
except Exception:
pass
assert False, msg or "got different content"
def assert_image_equal_tofile(a, filename, msg=None, mode=None):
with Image.open(filename) as img:
if mode:
img = img.convert(mode)
assert_image_equal(a, img, msg)
def assert_image_similar(a, b, epsilon, msg=None):
assert a.mode == b.mode, msg or f"got mode {repr(a.mode)}, expected {repr(b.mode)}"
assert a.size == b.size, msg or f"got size {repr(a.size)}, expected {repr(b.size)}"
a, b = convert_to_comparable(a, b)
diff = 0
for ach, bch in zip(a.split(), b.split()):
chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert("L")
diff += sum(i * num for i, num in enumerate(chdiff.histogram()))
ave_diff = diff / (a.size[0] * a.size[1])
try:
assert epsilon >= ave_diff, (
(msg or "")
+ f" average pixel value difference {ave_diff:.4f} > epsilon {epsilon:.4f}"
)
except Exception as e:
if HAS_UPLOADER:
try:
url = test_image_results.upload(a, b)
logger.error(f"Url for test images: {url}")
except Exception:
pass
raise e
def assert_image_similar_tofile(a, filename, epsilon, msg=None, mode=None):
with Image.open(filename) as img:
if mode:
img = img.convert(mode)
assert_image_similar(a, img, epsilon, msg)
def assert_all_same(items, msg=None):
assert items.count(items[0]) == len(items), msg
def assert_not_all_same(items, msg=None):
assert items.count(items[0]) != len(items), msg
def assert_tuple_approx_equal(actuals, targets, threshold, msg):
"""Tests if actuals has values within threshold from targets"""
value = True
for i, target in enumerate(targets):
value *= target - threshold <= actuals[i] <= target + threshold
assert value, msg + ": " + repr(actuals) + " != " + repr(targets)
def skip_unless_feature(feature):
reason = f"{feature} not available"
return pytest.mark.skipif(not features.check(feature), reason=reason)
2020-03-02 17:02:19 +03:00
@pytest.mark.skipif(sys.platform.startswith("win32"), reason="Requires Unix or macOS")
class PillowLeakTestCase:
2018-05-13 13:55:04 +03:00
# requires unix/macOS
2017-12-19 18:16:15 +03:00
iterations = 100 # count
mem_limit = 512 # k
2017-09-02 19:03:38 +03:00
def _get_mem_usage(self):
"""
Gets the RUSAGE memory usage, returns in K. Encapsulates the difference
2018-05-13 13:55:04 +03:00
between macOS and Linux rss reporting
2017-09-02 19:03:38 +03:00
2018-05-13 13:52:11 +03:00
:returns: memory usage in kilobytes
2017-09-02 19:03:38 +03:00
"""
2020-09-01 20:16:46 +03:00
from resource import RUSAGE_SELF, getrusage
2019-06-13 18:53:42 +03:00
2017-09-02 19:03:38 +03:00
mem = getrusage(RUSAGE_SELF).ru_maxrss
2019-06-13 18:53:42 +03:00
if sys.platform == "darwin":
2017-09-02 19:03:38 +03:00
# man 2 getrusage:
2018-06-24 15:32:25 +03:00
# ru_maxrss
# This is the maximum resident set size utilized (in bytes).
return mem / 1024 # Kb
2017-09-02 19:03:38 +03:00
else:
# linux
# man 2 getrusage
# ru_maxrss (since Linux 2.6.32)
2017-10-26 08:20:16 +03:00
# This is the maximum resident set size used (in kilobytes).
return mem # Kb
2017-09-02 19:03:38 +03:00
def _test_leak(self, core):
start_mem = self._get_mem_usage()
for cycle in range(self.iterations):
core()
2019-06-13 18:53:42 +03:00
mem = self._get_mem_usage() - start_mem
msg = f"memory usage limit exceeded in iteration {cycle}"
assert mem < self.mem_limit, msg
2017-09-02 19:03:38 +03:00
2014-07-07 21:03:50 +04:00
# helpers
def fromstring(data):
return Image.open(BytesIO(data))
def tostring(im, string_format, **options):
out = BytesIO()
im.save(out, string_format, **options)
return out.getvalue()
def hopper(mode=None, cache={}):
if mode is None:
# Always return fresh not-yet-loaded version of image.
# Operations on not-yet-loaded images is separate class of errors
# what we should catch.
return Image.open("Tests/images/hopper.ppm")
# Use caching to reduce reading from disk but so an original copy is
# returned each time and the cached image isn't modified by tests
# (for fast, isolated, repeatable tests).
im = cache.get(mode)
if im is None:
if mode == "F":
2014-09-05 13:36:24 +04:00
im = hopper("L").convert(mode)
elif mode[:4] == "I;16":
2014-09-05 13:36:24 +04:00
im = hopper("I").convert(mode)
else:
im = hopper().convert(mode)
cache[mode] = im
return im.copy()
def djpeg_available():
return bool(shutil.which("djpeg"))
2014-07-07 21:03:50 +04:00
def cjpeg_available():
return bool(shutil.which("cjpeg"))
2014-07-07 21:03:50 +04:00
def netpbm_available():
return bool(shutil.which("ppmquant") and shutil.which("ppmtogif"))
2014-07-07 21:03:50 +04:00
def imagemagick_available():
return bool(IMCONVERT and shutil.which(IMCONVERT))
2015-06-20 06:43:14 +03:00
2015-06-16 23:35:34 +03:00
def on_appveyor():
2019-06-13 18:53:42 +03:00
return "APPVEYOR" in os.environ
2017-12-19 18:16:15 +03:00
def on_github_actions():
return "GITHUB_ACTIONS" in os.environ
2019-09-21 15:28:05 +03:00
def on_ci():
2020-04-16 20:42:29 +03:00
# GitHub Actions, Travis and AppVeyor have "CI"
return "CI" in os.environ
2019-09-21 15:28:05 +03:00
def is_big_endian():
2020-01-08 16:22:53 +03:00
return sys.byteorder == "big"
2019-09-25 12:46:54 +03:00
def is_win32():
return sys.platform.startswith("win32")
def is_pypy():
return hasattr(sys, "pypy_translation_info")
2019-09-21 15:28:05 +03:00
2020-05-23 22:24:11 +03:00
def is_mingw():
return sysconfig.get_platform() == "mingw"
2019-06-13 18:53:42 +03:00
if sys.platform == "win32":
IMCONVERT = os.environ.get("MAGICK_HOME", "")
if IMCONVERT:
2019-06-13 18:53:42 +03:00
IMCONVERT = os.path.join(IMCONVERT, "convert.exe")
else:
2019-06-13 18:53:42 +03:00
IMCONVERT = "convert"
2016-09-19 02:59:34 +03:00
2017-04-20 14:14:23 +03:00
class cached_property:
2016-09-19 02:59:34 +03:00
def __init__(self, func):
self.func = func
def __get__(self, instance, cls=None):
result = instance.__dict__[self.func.__name__] = self.func(instance)
return result