From aa6534120fc77f7de4dfb8314a88fc8f39ee5c95 Mon Sep 17 00:00:00 2001 From: chavlin Date: Mon, 28 Oct 2024 12:00:17 -0500 Subject: [PATCH] set PYOPENGL_PLATFORM before opengl imports --- yt_idv/rendering_contexts/__init__.py | 11 +++++++---- yt_idv/tests/conftest.py | 7 +++++++ yt_idv/tests/test_yt_idv.py | 5 ----- 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 yt_idv/tests/conftest.py diff --git a/yt_idv/rendering_contexts/__init__.py b/yt_idv/rendering_contexts/__init__.py index 50dbcffc..76e07650 100644 --- a/yt_idv/rendering_contexts/__init__.py +++ b/yt_idv/rendering_contexts/__init__.py @@ -1,7 +1,5 @@ import os -import OpenGL.error - def render_context(engine="pyglet", **kwargs): """ @@ -18,6 +16,13 @@ def render_context(engine="pyglet", **kwargs): RenderingContext """ + + # PYOPENGL_PLATFORM must be set before any opengl imports + if engine in ("osmesa", "egl"): + os.environ["PYOPENGL_PLATFORM"] = engine + + import OpenGL.error + if engine == "pyglet": from .pyglet_context import PygletRenderingContext @@ -35,12 +40,10 @@ def render_context(engine="pyglet", **kwargs): raise Exception(extramsg) from oee raise oee elif engine == "osmesa": - os.environ["PYOPENGL_PLATFORM"] = "osmesa" from .osmesa_context import OSMesaRenderingContext return OSMesaRenderingContext(**kwargs) elif engine == "egl": - os.environ["PYOPENGL_PLATFORM"] = "egl" from .egl_context import EGLRenderingContext return EGLRenderingContext(**kwargs) diff --git a/yt_idv/tests/conftest.py b/yt_idv/tests/conftest.py new file mode 100644 index 00000000..8c9a39d9 --- /dev/null +++ b/yt_idv/tests/conftest.py @@ -0,0 +1,7 @@ +import os + + +def pytest_configure(config): + # this will get run before all tests, before collection and + # any opengl imports that happen within test files. + os.environ["PYOPENGL_PLATFORM"] = "osmesa" diff --git a/yt_idv/tests/test_yt_idv.py b/yt_idv/tests/test_yt_idv.py index 11fa8c32..67a0e33a 100644 --- a/yt_idv/tests/test_yt_idv.py +++ b/yt_idv/tests/test_yt_idv.py @@ -14,11 +14,6 @@ from yt_idv.scene_data.curve import CurveCollection, CurveData -@pytest.fixture(autouse=True) -def pyopengl_setup(monkeypatch): - monkeypatch.setenv("PYOPENGL_PLATFORM", "osmesa") - - @pytest.fixture() def osmesa_fake_amr(): """Return an OSMesa context that has a "fake" AMR dataset added, with "radius"