|
11 | 11 | import sys |
12 | 12 | import sysconfig |
13 | 13 | import tempfile |
| 14 | +from functools import lru_cache |
14 | 15 | from io import BytesIO |
15 | 16 | from typing import Any, Callable, Sequence |
16 | 17 |
|
@@ -250,25 +251,27 @@ def tostring(im: Image.Image, string_format: str, **options: Any) -> bytes: |
250 | 251 | return out.getvalue() |
251 | 252 |
|
252 | 253 |
|
253 | | -def hopper(mode: str | None = None, cache: dict[str, Image.Image] = {}) -> Image.Image: |
| 254 | +def hopper(mode: str | None = None) -> Image.Image: |
| 255 | + # Use caching to reduce reading from disk but so an original copy is |
| 256 | + # returned each time and the cached image isn't modified by tests |
| 257 | + # (for fast, isolated, repeatable tests). |
| 258 | + return _cached_hopper(mode).copy() |
| 259 | + |
| 260 | + |
| 261 | +@lru_cache(maxsize=None) |
| 262 | +def _cached_hopper(mode: str | None = None) -> Image.Image: |
254 | 263 | if mode is None: |
255 | 264 | # Always return fresh not-yet-loaded version of image. |
256 | 265 | # Operations on not-yet-loaded images is separate class of errors |
257 | 266 | # what we should catch. |
258 | 267 | return Image.open("Tests/images/hopper.ppm") |
259 | | - # Use caching to reduce reading from disk but so an original copy is |
260 | | - # returned each time and the cached image isn't modified by tests |
261 | | - # (for fast, isolated, repeatable tests). |
262 | | - im = cache.get(mode) |
263 | | - if im is None: |
264 | | - if mode == "F": |
265 | | - im = hopper("L").convert(mode) |
266 | | - elif mode[:4] == "I;16": |
267 | | - im = hopper("I").convert(mode) |
268 | | - else: |
269 | | - im = hopper().convert(mode) |
270 | | - cache[mode] = im |
271 | | - return im.copy() |
| 268 | + if mode == "F": |
| 269 | + im = _cached_hopper("L").convert(mode) |
| 270 | + elif mode[:4] == "I;16": |
| 271 | + im = _cached_hopper("I").convert(mode) |
| 272 | + else: |
| 273 | + im = _cached_hopper().convert(mode) |
| 274 | + return im |
272 | 275 |
|
273 | 276 |
|
274 | 277 | def djpeg_available() -> bool: |
|
0 commit comments