Added running_in_another_thread()

This commit is contained in:
Andrew Murray 2024-11-02 00:47:03 +11:00
parent 16372dd951
commit 1df561869f
6 changed files with 36 additions and 4 deletions

View File

@ -4,6 +4,7 @@ Helper functions.
from __future__ import annotations from __future__ import annotations
import inspect
import logging import logging
import os import os
import shutil import shutil
@ -29,6 +30,8 @@ if os.environ.get("SHOW_ERRORS"):
elif "GITHUB_ACTIONS" in os.environ: elif "GITHUB_ACTIONS" in os.environ:
uploader = "github_actions" uploader = "github_actions"
already_running = []
def upload(a: Image.Image, b: Image.Image) -> str | None: def upload(a: Image.Image, b: Image.Image) -> str | None:
if uploader == "show": if uploader == "show":
@ -165,6 +168,18 @@ def assert_tuple_approx_equal(
pytest.fail(msg + ": " + repr(actuals) + " != " + repr(targets)) 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: def skip_unless_feature(feature: str) -> pytest.MarkDecorator:
reason = f"{feature} not available" reason = f"{feature} not available"
return pytest.mark.skipif(not features.check(feature), reason=reason) return pytest.mark.skipif(not features.check(feature), reason=reason)

View File

@ -6,7 +6,7 @@ import pytest
from PIL import Image from PIL import Image
from .helper import is_pypy from .helper import is_pypy, running_in_another_thread
def test_get_stats() -> None: def test_get_stats() -> None:
@ -48,6 +48,9 @@ class TestCoreMemory:
assert alignment > 0 assert alignment > 0
def test_set_alignment(self) -> None: def test_set_alignment(self) -> None:
if running_in_another_thread():
return
for i in [1, 2, 4, 8, 16, 32]: for i in [1, 2, 4, 8, 16, 32]:
Image.core.set_alignment(i) Image.core.set_alignment(i)
alignment = Image.core.get_alignment() alignment = Image.core.get_alignment()

View File

@ -7,7 +7,7 @@ import pytest
from PIL import BufrStubImagePlugin, Image, ImageFile 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" 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: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.saved = True self.saved = True
if running_in_another_thread():
return
handler = TestHandler() handler = TestHandler()
BufrStubImagePlugin.register_handler(handler) BufrStubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:

View File

@ -7,7 +7,7 @@ import pytest
from PIL import GribStubImagePlugin, Image, ImageFile 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" 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: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.saved = True self.saved = True
if running_in_another_thread():
return
handler = TestHandler() handler = TestHandler()
GribStubImagePlugin.register_handler(handler) GribStubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:

View File

@ -8,6 +8,8 @@ import pytest
from PIL import Hdf5StubImagePlugin, Image, ImageFile from PIL import Hdf5StubImagePlugin, Image, ImageFile
from .helper import running_in_another_thread
TEST_FILE = "Tests/images/hdf5.h5" 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: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.saved = True self.saved = True
if running_in_another_thread():
return
handler = TestHandler() handler = TestHandler()
Hdf5StubImagePlugin.register_handler(handler) Hdf5StubImagePlugin.register_handler(handler)
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:

View File

@ -8,7 +8,7 @@ import pytest
from PIL import Image, ImageFile, WmfImagePlugin 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: 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: def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.methodCalled = True self.methodCalled = True
if running_in_another_thread():
return
handler = TestHandler() handler = TestHandler()
original_handler = WmfImagePlugin._handler original_handler = WmfImagePlugin._handler
WmfImagePlugin.register_handler(handler) WmfImagePlugin.register_handler(handler)