mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 18:26:17 +03:00
Use functools.lru_cache for hopper()
This commit is contained in:
parent
e3aeaa6ff8
commit
504a2e17c8
|
@ -11,6 +11,7 @@ import subprocess
|
||||||
import sys
|
import sys
|
||||||
import sysconfig
|
import sysconfig
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from functools import lru_cache
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Any, Callable, Sequence
|
from typing import Any, Callable, Sequence
|
||||||
|
|
||||||
|
@ -250,25 +251,27 @@ def tostring(im: Image.Image, string_format: str, **options: Any) -> bytes:
|
||||||
return out.getvalue()
|
return out.getvalue()
|
||||||
|
|
||||||
|
|
||||||
def hopper(mode: str | None = None, cache: dict[str, Image.Image] = {}) -> Image.Image:
|
def hopper(mode: str | None = None) -> Image.Image:
|
||||||
|
# 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).
|
||||||
|
return _cached_hopper(mode).copy()
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def _cached_hopper(mode: str | None = None) -> Image.Image:
|
||||||
if mode is None:
|
if mode is None:
|
||||||
# Always return fresh not-yet-loaded version of image.
|
# Always return fresh not-yet-loaded version of image.
|
||||||
# Operations on not-yet-loaded images is separate class of errors
|
# Operations on not-yet-loaded images is separate class of errors
|
||||||
# what we should catch.
|
# what we should catch.
|
||||||
return Image.open("Tests/images/hopper.ppm")
|
return Image.open("Tests/images/hopper.ppm")
|
||||||
# Use caching to reduce reading from disk but so an original copy is
|
if mode == "F":
|
||||||
# returned each time and the cached image isn't modified by tests
|
im = _cached_hopper("L").convert(mode)
|
||||||
# (for fast, isolated, repeatable tests).
|
elif mode[:4] == "I;16":
|
||||||
im = cache.get(mode)
|
im = _cached_hopper("I").convert(mode)
|
||||||
if im is None:
|
else:
|
||||||
if mode == "F":
|
im = _cached_hopper().convert(mode)
|
||||||
im = hopper("L").convert(mode)
|
return im
|
||||||
elif mode[:4] == "I;16":
|
|
||||||
im = hopper("I").convert(mode)
|
|
||||||
else:
|
|
||||||
im = hopper().convert(mode)
|
|
||||||
cache[mode] = im
|
|
||||||
return im.copy()
|
|
||||||
|
|
||||||
|
|
||||||
def djpeg_available() -> bool:
|
def djpeg_available() -> bool:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user