From 1df561869f55afaaf2236a1ad3970728b09efdf4 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 2 Nov 2024 00:47:03 +1100 Subject: [PATCH] Added running_in_another_thread() --- Tests/helper.py | 15 +++++++++++++++ Tests/test_core_resources.py | 5 ++++- Tests/test_file_bufrstub.py | 5 ++++- Tests/test_file_gribstub.py | 5 ++++- Tests/test_file_hdf5stub.py | 5 +++++ Tests/test_file_wmf.py | 5 ++++- 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index d6a93a803..5819a3d07 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -4,6 +4,7 @@ Helper functions. from __future__ import annotations +import inspect import logging import os import shutil @@ -29,6 +30,8 @@ if os.environ.get("SHOW_ERRORS"): elif "GITHUB_ACTIONS" in os.environ: uploader = "github_actions" +already_running = [] + def upload(a: Image.Image, b: Image.Image) -> str | None: if uploader == "show": @@ -165,6 +168,18 @@ def assert_tuple_approx_equal( pytest.fail(msg + ": " + repr(actuals) + " != " + repr(targets)) +def running_in_another_thread() -> bool: + frameInfo = inspect.stack()[1] + identifier = ( + frameInfo.filename + ":" + frameInfo.function + ":" + str(frameInfo.lineno) + ) + if identifier in already_running: + return True + + already_running.append(identifier) + return False + + def skip_unless_feature(feature: str) -> pytest.MarkDecorator: reason = f"{feature} not available" return pytest.mark.skipif(not features.check(feature), reason=reason) diff --git a/Tests/test_core_resources.py b/Tests/test_core_resources.py index 2c1de8bc3..68729022a 100644 --- a/Tests/test_core_resources.py +++ b/Tests/test_core_resources.py @@ -6,7 +6,7 @@ import pytest from PIL import Image -from .helper import is_pypy +from .helper import is_pypy, running_in_another_thread def test_get_stats() -> None: @@ -48,6 +48,9 @@ class TestCoreMemory: assert alignment > 0 def test_set_alignment(self) -> None: + if running_in_another_thread(): + return + for i in [1, 2, 4, 8, 16, 32]: Image.core.set_alignment(i) alignment = Image.core.get_alignment() diff --git a/Tests/test_file_bufrstub.py b/Tests/test_file_bufrstub.py index 77ee5b0ea..226b41d5f 100644 --- a/Tests/test_file_bufrstub.py +++ b/Tests/test_file_bufrstub.py @@ -7,7 +7,7 @@ import pytest from PIL import BufrStubImagePlugin, Image, ImageFile -from .helper import hopper +from .helper import hopper, running_in_another_thread TEST_FILE = "Tests/images/gfs.t06z.rassda.tm00.bufr_d" @@ -70,6 +70,9 @@ def test_handler(tmp_path: Path) -> None: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.saved = True + if running_in_another_thread(): + return + handler = TestHandler() BufrStubImagePlugin.register_handler(handler) with Image.open(TEST_FILE) as im: diff --git a/Tests/test_file_gribstub.py b/Tests/test_file_gribstub.py index aba473d24..1a1737a2b 100644 --- a/Tests/test_file_gribstub.py +++ b/Tests/test_file_gribstub.py @@ -7,7 +7,7 @@ import pytest from PIL import GribStubImagePlugin, Image, ImageFile -from .helper import hopper +from .helper import hopper, running_in_another_thread TEST_FILE = "Tests/images/WAlaska.wind.7days.grb" @@ -70,6 +70,9 @@ def test_handler(tmp_path: Path) -> None: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.saved = True + if running_in_another_thread(): + return + handler = TestHandler() GribStubImagePlugin.register_handler(handler) with Image.open(TEST_FILE) as im: diff --git a/Tests/test_file_hdf5stub.py b/Tests/test_file_hdf5stub.py index 8275bd0d8..f7c65e1bb 100644 --- a/Tests/test_file_hdf5stub.py +++ b/Tests/test_file_hdf5stub.py @@ -8,6 +8,8 @@ import pytest from PIL import Hdf5StubImagePlugin, Image, ImageFile +from .helper import running_in_another_thread + TEST_FILE = "Tests/images/hdf5.h5" @@ -72,6 +74,9 @@ def test_handler(tmp_path: Path) -> None: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.saved = True + if running_in_another_thread(): + return + handler = TestHandler() Hdf5StubImagePlugin.register_handler(handler) with Image.open(TEST_FILE) as im: diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 424640d7b..c84bdf750 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -8,7 +8,7 @@ import pytest from PIL import Image, ImageFile, WmfImagePlugin -from .helper import assert_image_similar_tofile, hopper +from .helper import assert_image_similar_tofile, hopper, running_in_another_thread def test_load_raw() -> None: @@ -45,6 +45,9 @@ def test_register_handler(tmp_path: Path) -> None: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.methodCalled = True + if running_in_another_thread(): + return + handler = TestHandler() original_handler = WmfImagePlugin._handler WmfImagePlugin.register_handler(handler)