2014-06-10 13:12:53 +04:00
|
|
|
"""
|
|
|
|
Helper functions.
|
|
|
|
"""
|
2024-02-05 20:18:49 +03:00
|
|
|
|
2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2019-07-06 23:40:53 +03:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2020-02-14 04:08:44 +03:00
|
|
|
import shutil
|
2023-01-01 05:18:30 +03:00
|
|
|
import subprocess
|
2014-06-10 13:12:53 +04:00
|
|
|
import sys
|
2020-05-25 23:21:51 +03:00
|
|
|
import sysconfig
|
2014-06-26 04:08:24 +04:00
|
|
|
import tempfile
|
2024-07-03 09:44:45 +03:00
|
|
|
from collections.abc import Sequence
|
2024-03-20 10:49:18 +03:00
|
|
|
from functools import lru_cache
|
2019-10-07 16:28:36 +03:00
|
|
|
from io import BytesIO
|
2024-07-03 09:44:45 +03:00
|
|
|
from typing import Any, Callable
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
import pytest
|
2020-10-12 00:26:11 +03:00
|
|
|
from packaging.version import parse as parse_version
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2024-06-22 03:09:11 +03:00
|
|
|
from PIL import Image, ImageFile, ImageMath, features
|
2016-12-07 03:07:20 +03:00
|
|
|
|
2017-11-17 22:47:05 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
uploader = None
|
2023-01-01 21:32:21 +03:00
|
|
|
if os.environ.get("SHOW_ERRORS"):
|
2024-01-20 03:13:06 +03:00
|
|
|
uploader = "show"
|
2019-10-12 13:59:03 +03:00
|
|
|
elif "GITHUB_ACTIONS" in os.environ:
|
2024-01-20 03:13:06 +03:00
|
|
|
uploader = "github_actions"
|
2017-12-13 15:47:11 +03:00
|
|
|
|
2016-12-07 03:07:20 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def upload(a: Image.Image, b: Image.Image) -> str | None:
|
|
|
|
if uploader == "show":
|
|
|
|
# local img.show for errors.
|
|
|
|
a.show()
|
|
|
|
b.show()
|
|
|
|
elif uploader == "github_actions":
|
|
|
|
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
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_comparable(
|
|
|
|
a: Image.Image, b: Image.Image
|
|
|
|
) -> tuple[Image.Image, Image.Image]:
|
2016-12-07 03:07:20 +03:00
|
|
|
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)
|
2016-12-07 03:07:20 +03:00
|
|
|
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")
|
2016-12-07 03:07:20 +03:00
|
|
|
return new_a, new_b
|
|
|
|
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2024-07-05 20:56:24 +03:00
|
|
|
def assert_deep_equal(a: Any, b: Any, msg: str | None = None) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
try:
|
2020-07-16 12:43:29 +03:00
|
|
|
assert len(a) == len(b), msg or f"got length {len(a)}, expected {len(b)}"
|
2020-01-30 17:56:07 +03:00
|
|
|
except Exception:
|
|
|
|
assert a == b, msg
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_image(
|
|
|
|
im: Image.Image, mode: str, size: tuple[int, int], msg: str | None = None
|
|
|
|
) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
if mode is not None:
|
2020-07-16 12:43:29 +03:00
|
|
|
assert im.mode == mode, (
|
|
|
|
msg or f"got mode {repr(im.mode)}, expected {repr(mode)}"
|
2019-06-13 18:53:42 +03:00
|
|
|
)
|
2020-01-30 17:56:07 +03:00
|
|
|
|
|
|
|
if size is not None:
|
2020-07-16 12:43:29 +03:00
|
|
|
assert im.size == size, (
|
|
|
|
msg or f"got size {repr(im.size)}, expected {repr(size)}"
|
2019-06-13 18:53:42 +03:00
|
|
|
)
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2016-12-07 03:07:20 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_image_equal(a: Image.Image, b: Image.Image, msg: str | None = None) -> None:
|
2020-07-16 12:43:29 +03:00
|
|
|
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)}"
|
2020-01-30 17:56:07 +03:00
|
|
|
if a.tobytes() != b.tobytes():
|
2024-01-20 13:04:57 +03:00
|
|
|
try:
|
|
|
|
url = upload(a, b)
|
|
|
|
if url:
|
2023-09-09 14:03:39 +03:00
|
|
|
logger.error("URL for test images: %s", url)
|
2024-01-20 13:04:57 +03:00
|
|
|
except Exception:
|
|
|
|
pass
|
2020-01-30 17:56:07 +03:00
|
|
|
|
2023-11-12 23:30:28 +03:00
|
|
|
pytest.fail(msg or "got different content")
|
2020-01-30 17:56:07 +03:00
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_image_equal_tofile(
|
|
|
|
a: Image.Image, filename: str, msg: str | None = None, mode: str | None = None
|
|
|
|
) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
with Image.open(filename) as img:
|
|
|
|
if mode:
|
|
|
|
img = img.convert(mode)
|
|
|
|
assert_image_equal(a, img, msg)
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_image_similar(
|
|
|
|
a: Image.Image, b: Image.Image, epsilon: float, msg: str | None = None
|
|
|
|
) -> None:
|
2020-07-16 12:43:29 +03:00
|
|
|
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)}"
|
2020-01-30 17:56:07 +03:00
|
|
|
|
|
|
|
a, b = convert_to_comparable(a, b)
|
|
|
|
|
|
|
|
diff = 0
|
|
|
|
for ach, bch in zip(a.split(), b.split()):
|
2024-03-25 12:38:52 +03:00
|
|
|
chdiff = ImageMath.lambda_eval(
|
|
|
|
lambda args: abs(args["a"] - args["b"]), a=ach, b=bch
|
|
|
|
).convert("L")
|
2020-01-30 17:56:07 +03:00
|
|
|
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, (
|
2020-07-16 12:43:29 +03:00
|
|
|
(msg or "")
|
|
|
|
+ f" average pixel value difference {ave_diff:.4f} > epsilon {epsilon:.4f}"
|
|
|
|
)
|
2020-01-30 17:56:07 +03:00
|
|
|
except Exception as e:
|
2024-01-20 13:04:57 +03:00
|
|
|
try:
|
|
|
|
url = upload(a, b)
|
|
|
|
if url:
|
2023-09-09 14:04:38 +03:00
|
|
|
logger.exception("URL for test images: %s", url)
|
2024-01-20 13:04:57 +03:00
|
|
|
except Exception:
|
|
|
|
pass
|
2020-01-30 17:56:07 +03:00
|
|
|
raise e
|
|
|
|
|
2016-12-07 03:07:20 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_image_similar_tofile(
|
|
|
|
a: Image.Image,
|
|
|
|
filename: str,
|
|
|
|
epsilon: float,
|
|
|
|
msg: str | None = None,
|
|
|
|
mode: str | None = None,
|
|
|
|
) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
with Image.open(filename) as img:
|
|
|
|
if mode:
|
|
|
|
img = img.convert(mode)
|
|
|
|
assert_image_similar(a, img, epsilon, msg)
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_all_same(items: Sequence[Any], msg: str | None = None) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert items.count(items[0]) == len(items), msg
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_not_all_same(items: Sequence[Any], msg: str | None = None) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert items.count(items[0]) != len(items), msg
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def assert_tuple_approx_equal(
|
|
|
|
actuals: Sequence[int], targets: tuple[int, ...], threshold: int, msg: str
|
|
|
|
) -> None:
|
2020-01-30 17:56:07 +03:00
|
|
|
"""Tests if actuals has values within threshold from targets"""
|
|
|
|
for i, target in enumerate(targets):
|
2024-01-20 03:13:06 +03:00
|
|
|
if not (target - threshold <= actuals[i] <= target + threshold):
|
|
|
|
pytest.fail(msg + ": " + repr(actuals) + " != " + repr(targets))
|
2020-01-30 17:56:07 +03:00
|
|
|
|
|
|
|
|
2024-01-19 00:37:58 +03:00
|
|
|
def skip_unless_feature(feature: str) -> pytest.MarkDecorator:
|
2020-07-16 12:43:29 +03:00
|
|
|
reason = f"{feature} not available"
|
2020-02-18 01:03:32 +03:00
|
|
|
return pytest.mark.skipif(not features.check(feature), reason=reason)
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def skip_unless_feature_version(
|
|
|
|
feature: str, required: str, reason: str | None = None
|
|
|
|
) -> pytest.MarkDecorator:
|
2024-05-29 15:51:02 +03:00
|
|
|
version = features.version(feature)
|
|
|
|
if version is None:
|
2020-10-12 00:26:11 +03:00
|
|
|
return pytest.mark.skip(f"{feature} not available")
|
|
|
|
if reason is None:
|
2024-01-20 03:13:06 +03:00
|
|
|
reason = f"{feature} is older than {required}"
|
|
|
|
version_required = parse_version(required)
|
2024-05-29 15:51:02 +03:00
|
|
|
version_available = parse_version(version)
|
2020-10-12 00:26:11 +03:00
|
|
|
return pytest.mark.skipif(version_available < version_required, reason=reason)
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def mark_if_feature_version(
|
|
|
|
mark: pytest.MarkDecorator,
|
|
|
|
feature: str,
|
|
|
|
version_blacklist: str,
|
|
|
|
reason: str | None = None,
|
|
|
|
) -> pytest.MarkDecorator:
|
2024-05-29 15:51:02 +03:00
|
|
|
version = features.version(feature)
|
|
|
|
if version is None:
|
2021-04-10 13:03:15 +03:00
|
|
|
return pytest.mark.pil_noop_mark()
|
2021-04-10 00:33:21 +03:00
|
|
|
if reason is None:
|
|
|
|
reason = f"{feature} is {version_blacklist}"
|
|
|
|
version_required = parse_version(version_blacklist)
|
2024-05-29 15:51:02 +03:00
|
|
|
version_available = parse_version(version)
|
2021-04-10 17:58:01 +03:00
|
|
|
if (
|
|
|
|
version_available.major == version_required.major
|
|
|
|
and version_available.minor == version_required.minor
|
|
|
|
):
|
2021-04-10 13:03:15 +03:00
|
|
|
return mark(reason=reason)
|
|
|
|
return pytest.mark.pil_noop_mark()
|
2021-04-10 00:33:21 +03:00
|
|
|
|
|
|
|
|
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-12-19 18:12:58 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def _get_mem_usage(self) -> float:
|
2017-09-02 19:03:38 +03:00
|
|
|
"""
|
|
|
|
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
|
|
|
"""
|
2017-12-19 18:12:58 +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
|
2024-05-29 15:51:02 +03:00
|
|
|
# man 2 getrusage:
|
|
|
|
# ru_maxrss
|
|
|
|
# This is the maximum resident set size utilized
|
|
|
|
# in bytes on macOS, in kilobytes on Linux
|
|
|
|
return mem / 1024 if sys.platform == "darwin" else mem
|
2017-09-02 19:03:38 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def _test_leak(self, core: Callable[[], None]) -> None:
|
2017-09-02 19:03:38 +03:00
|
|
|
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
|
2020-07-16 12:43:29 +03:00
|
|
|
msg = f"memory usage limit exceeded in iteration {cycle}"
|
2020-02-22 16:06:21 +03:00
|
|
|
assert mem < self.mem_limit, msg
|
2017-09-02 19:03:38 +03:00
|
|
|
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2014-06-24 15:03:10 +04:00
|
|
|
# helpers
|
2014-06-10 13:12:53 +04:00
|
|
|
|
|
|
|
|
2024-06-22 03:09:11 +03:00
|
|
|
def fromstring(data: bytes) -> ImageFile.ImageFile:
|
2014-06-10 13:12:53 +04:00
|
|
|
return Image.open(BytesIO(data))
|
|
|
|
|
|
|
|
|
2024-02-12 01:28:53 +03:00
|
|
|
def tostring(im: Image.Image, string_format: str, **options: Any) -> bytes:
|
2014-06-10 13:12:53 +04:00
|
|
|
out = BytesIO()
|
2014-10-01 17:50:33 +04:00
|
|
|
im.save(out, string_format, **options)
|
2014-06-10 13:12:53 +04:00
|
|
|
return out.getvalue()
|
|
|
|
|
|
|
|
|
2024-03-20 10:49:18 +03:00
|
|
|
def hopper(mode: str | None = None) -> Image.Image:
|
2024-03-28 23:13:09 +03:00
|
|
|
# Use caching to reduce reading from disk, but return a copy
|
|
|
|
# so that the cached image isn't modified by the tests
|
2024-03-20 10:49:18 +03:00
|
|
|
# (for fast, isolated, repeatable tests).
|
|
|
|
|
2014-11-19 03:41:44 +03:00
|
|
|
if mode is None:
|
|
|
|
# Always return fresh not-yet-loaded version of image.
|
2024-04-01 08:13:30 +03:00
|
|
|
# Operations on not-yet-loaded images are a separate class of errors
|
|
|
|
# that we should catch.
|
2014-11-19 03:41:44 +03:00
|
|
|
return Image.open("Tests/images/hopper.ppm")
|
2024-03-31 15:07:15 +03:00
|
|
|
|
|
|
|
return _cached_hopper(mode).copy()
|
|
|
|
|
|
|
|
|
2024-04-01 08:13:30 +03:00
|
|
|
@lru_cache
|
|
|
|
def _cached_hopper(mode: str) -> Image.Image:
|
2024-03-20 10:49:18 +03:00
|
|
|
if mode == "F":
|
2024-04-01 08:13:30 +03:00
|
|
|
im = hopper("L")
|
2024-03-20 10:49:18 +03:00
|
|
|
else:
|
2024-04-01 08:13:30 +03:00
|
|
|
im = hopper()
|
2024-04-15 12:28:52 +03:00
|
|
|
if mode.startswith("BGR;"):
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
im = im.convert(mode)
|
|
|
|
else:
|
2024-04-26 02:08:43 +03:00
|
|
|
try:
|
|
|
|
im = im.convert(mode)
|
|
|
|
except ImportError:
|
|
|
|
if mode == "LAB":
|
|
|
|
im = Image.open("Tests/images/hopper.Lab.tif")
|
|
|
|
else:
|
|
|
|
raise
|
2024-04-15 15:22:40 +03:00
|
|
|
return im
|
2014-06-10 13:12:53 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def djpeg_available() -> bool:
|
2023-01-01 05:18:30 +03:00
|
|
|
if shutil.which("djpeg"):
|
|
|
|
try:
|
|
|
|
subprocess.check_call(["djpeg", "-version"])
|
|
|
|
return True
|
2023-11-28 00:32:00 +03:00
|
|
|
except subprocess.CalledProcessError: # pragma: no cover
|
2024-01-20 13:23:08 +03:00
|
|
|
return False
|
2024-01-20 03:13:06 +03:00
|
|
|
return False
|
2014-06-28 02:12:37 +04:00
|
|
|
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def cjpeg_available() -> bool:
|
2023-01-01 05:18:30 +03:00
|
|
|
if shutil.which("cjpeg"):
|
|
|
|
try:
|
|
|
|
subprocess.check_call(["cjpeg", "-version"])
|
|
|
|
return True
|
2023-11-28 00:32:00 +03:00
|
|
|
except subprocess.CalledProcessError: # pragma: no cover
|
2024-01-20 13:23:08 +03:00
|
|
|
return False
|
2024-01-20 03:13:06 +03:00
|
|
|
return False
|
2014-06-28 02:12:37 +04:00
|
|
|
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def netpbm_available() -> bool:
|
2020-02-14 04:08:44 +03:00
|
|
|
return bool(shutil.which("ppmquant") and shutil.which("ppmtogif"))
|
2014-07-07 21:03:50 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def magick_command() -> list[str] | None:
|
2021-03-21 23:48:13 +03:00
|
|
|
if sys.platform == "win32":
|
2023-01-01 21:32:21 +03:00
|
|
|
magickhome = os.environ.get("MAGICK_HOME")
|
2021-03-22 21:17:40 +03:00
|
|
|
if magickhome:
|
|
|
|
imagemagick = [os.path.join(magickhome, "convert.exe")]
|
|
|
|
graphicsmagick = [os.path.join(magickhome, "gm.exe"), "convert"]
|
2021-03-21 23:48:13 +03:00
|
|
|
else:
|
2021-03-22 21:17:40 +03:00
|
|
|
imagemagick = None
|
2021-03-21 23:48:13 +03:00
|
|
|
graphicsmagick = None
|
|
|
|
else:
|
|
|
|
imagemagick = ["convert"]
|
|
|
|
graphicsmagick = ["gm", "convert"]
|
2021-03-21 17:36:18 +03:00
|
|
|
|
2021-03-21 23:48:13 +03:00
|
|
|
if imagemagick and shutil.which(imagemagick[0]):
|
|
|
|
return imagemagick
|
2022-10-13 05:20:11 +03:00
|
|
|
if graphicsmagick and shutil.which(graphicsmagick[0]):
|
2021-03-21 23:48:13 +03:00
|
|
|
return graphicsmagick
|
2024-01-20 03:13:06 +03:00
|
|
|
return None
|
2014-09-22 04:50:07 +04:00
|
|
|
|
2015-06-20 06:43:14 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def on_appveyor() -> bool:
|
2019-06-13 18:53:42 +03:00
|
|
|
return "APPVEYOR" in os.environ
|
2014-09-22 04:50:07 +04:00
|
|
|
|
2017-12-19 18:16:15 +03:00
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def on_github_actions() -> bool:
|
2019-09-24 19:15:31 +03:00
|
|
|
return "GITHUB_ACTIONS" in os.environ
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def on_ci() -> bool:
|
2020-12-18 11:27:22 +03:00
|
|
|
# GitHub Actions and AppVeyor have "CI"
|
2020-04-16 20:42:29 +03:00
|
|
|
return "CI" in os.environ
|
2019-09-21 15:28:05 +03:00
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def is_big_endian() -> bool:
|
2020-01-08 16:22:53 +03:00
|
|
|
return sys.byteorder == "big"
|
2020-01-05 00:07:59 +03:00
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def is_ppc64le() -> bool:
|
2020-11-04 03:58:06 +03:00
|
|
|
import platform
|
|
|
|
|
|
|
|
return platform.machine() == "ppc64le"
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def is_win32() -> bool:
|
2019-09-25 12:46:54 +03:00
|
|
|
return sys.platform.startswith("win32")
|
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def is_pypy() -> bool:
|
2019-09-25 12:46:54 +03:00
|
|
|
return hasattr(sys, "pypy_translation_info")
|
2019-09-21 15:28:05 +03:00
|
|
|
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def is_mingw() -> bool:
|
2020-05-23 22:24:11 +03:00
|
|
|
return sysconfig.get_platform() == "mingw"
|
|
|
|
|
|
|
|
|
2022-04-10 21:26:23 +03:00
|
|
|
class CachedProperty:
|
2024-03-02 05:12:17 +03:00
|
|
|
def __init__(self, func: Callable[[Any], Any]) -> None:
|
2016-09-19 02:59:34 +03:00
|
|
|
self.func = func
|
|
|
|
|
2024-01-20 03:13:06 +03:00
|
|
|
def __get__(self, instance: Any, cls: type[Any] | None = None) -> Any:
|
2016-09-19 02:59:34 +03:00
|
|
|
result = instance.__dict__[self.func.__name__] = self.func(instance)
|
|
|
|
return result
|