From 3218db2e2e7e79653414af5d1ca790a2ef8b044c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 11 Jun 2024 19:26:25 +0200 Subject: [PATCH] python: make it possible to skip gl.Context.current() blowing up. This was originally added in d6fec89dc5077d3c3b28488c1b5ab49f887b2b04 as a doc-generation-only hack, but other tools such as stub generation may need similar special cases, so it's now a env var check in the binding generation directly. It's not a check in every invocation because that *feels* slow (although pybind11 itself likely does a lot more nasty string comparisons, hashmap lookups and linked link traversal than that), so if such an env var was defined while importing the module, the current() is then forever broken, until interpreter restart. --- doc/python/conf.py | 8 -------- src/python/magnum/gl.cpp | 36 +++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/doc/python/conf.py b/doc/python/conf.py index 2c7a475..63123f9 100644 --- a/doc/python/conf.py +++ b/doc/python/conf.py @@ -58,14 +58,6 @@ class DoNotPrintValue: pass magnum.TARGET_EGL = DoNotPrintValue() magnum.TARGET_VK = DoNotPrintValue() -# Otherwise it blows during doc generation up because there's no content -# TODO is there a way to make Python not execute the property when inspecting -# it?! -# TODO also, it's a static property together with has_current, and as such it -# has no docstring -- fix -delattr(magnum.gl.Context, 'current') -setattr(magnum.gl.Context, 'current', DoNotPrintValue()) - # TODO ugh... can this be expressed directly in pybind? and the docs parsed # from it so i don't need to repeat them in docs/*.rst files? for i in [magnum.text.AbstractFont, diff --git a/src/python/magnum/gl.cpp b/src/python/magnum/gl.cpp index 998b9a5..a9e6ae0 100644 --- a/src/python/magnum/gl.cpp +++ b/src/python/magnum/gl.cpp @@ -373,21 +373,27 @@ void gl(py::module_& m) { .def_property_readonly_static("has_current", [](const py::object&) { return GL::Context::hasCurrent(); }, "Whether there is any current context") - .def_property_readonly_static("current", [](const py::object&) { - if(!GL::Context::hasCurrent()) { - PyErr_SetString(PyExc_RuntimeError, "no current context"); - throw py::error_already_set{}; - } - - py::object owner = py::none{}; - auto* glContextOwner = reinterpret_cast*>(py::get_shared_data("magnumGLContextOwner")); - if(glContextOwner && glContextOwner->first) { - CORRADE_INTERNAL_ASSERT(glContextOwner->second); - owner = Corrade::pyObjectFromInstance(glContextOwner->first, *glContextOwner->second); - } - - return ContextHolder{&GL::Context::current(), std::move(owner)}; - }, "Current context") + .def_property_readonly_static("current", + std::getenv("MCSS_GENERATING_OUTPUT") ? + [](const py::object&) { + return ContextHolder{nullptr}; + } : + [](const py::object&) { + if(!GL::Context::hasCurrent()) { + PyErr_SetString(PyExc_RuntimeError, "no current context"); + throw py::error_already_set{}; + } + + py::object owner = py::none{}; + auto* glContextOwner = reinterpret_cast*>(py::get_shared_data("magnumGLContextOwner")); + if(glContextOwner && glContextOwner->first) { + CORRADE_INTERNAL_ASSERT(glContextOwner->second); + owner = Corrade::pyObjectFromInstance(glContextOwner->first, *glContextOwner->second); + } + + return ContextHolder{&GL::Context::current(), std::move(owner)}; + } + , "Current context") /** @todo context switching (needs additions to the "who owns current context instance" variable -- a map?) */ .def_property_readonly("version", &GL::Context::version, "OpenGL version")