Browse Source

python: make it possible to skip gl.Context.current() blowing up.

This was originally added in d6fec89dc5 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.
next
Vladimír Vondruš 2 years ago
parent
commit
3218db2e2e
  1. 8
      doc/python/conf.py
  2. 36
      src/python/magnum/gl.cpp

8
doc/python/conf.py

@ -58,14 +58,6 @@ magnum.TARGET_WEBGL = DoNotPrintValue()
magnum.TARGET_EGL = DoNotPrintValue() magnum.TARGET_EGL = DoNotPrintValue()
magnum.TARGET_VK = 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 # 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? # from it so i don't need to repeat them in docs/*.rst files?
for i in [magnum.text.AbstractFont, for i in [magnum.text.AbstractFont,

36
src/python/magnum/gl.cpp

@ -373,21 +373,27 @@ void gl(py::module_& m) {
.def_property_readonly_static("has_current", [](const py::object&) { .def_property_readonly_static("has_current", [](const py::object&) {
return GL::Context::hasCurrent(); return GL::Context::hasCurrent();
}, "Whether there is any current context") }, "Whether there is any current context")
.def_property_readonly_static("current", [](const py::object&) { .def_property_readonly_static("current",
if(!GL::Context::hasCurrent()) { std::getenv("MCSS_GENERATING_OUTPUT") ?
PyErr_SetString(PyExc_RuntimeError, "no current context"); [](const py::object&) {
throw py::error_already_set{}; return ContextHolder<GL::Context>{nullptr};
} } :
[](const py::object&) {
py::object owner = py::none{}; if(!GL::Context::hasCurrent()) {
auto* glContextOwner = reinterpret_cast<std::pair<const void*, const std::type_info*>*>(py::get_shared_data("magnumGLContextOwner")); PyErr_SetString(PyExc_RuntimeError, "no current context");
if(glContextOwner && glContextOwner->first) { throw py::error_already_set{};
CORRADE_INTERNAL_ASSERT(glContextOwner->second); }
owner = Corrade::pyObjectFromInstance(glContextOwner->first, *glContextOwner->second);
} py::object owner = py::none{};
auto* glContextOwner = reinterpret_cast<std::pair<const void*, const std::type_info*>*>(py::get_shared_data("magnumGLContextOwner"));
return ContextHolder<GL::Context>{&GL::Context::current(), std::move(owner)}; if(glContextOwner && glContextOwner->first) {
}, "Current context") CORRADE_INTERNAL_ASSERT(glContextOwner->second);
owner = Corrade::pyObjectFromInstance(glContextOwner->first, *glContextOwner->second);
}
return ContextHolder<GL::Context>{&GL::Context::current(), std::move(owner)};
}
, "Current context")
/** @todo context switching (needs additions to the "who owns /** @todo context switching (needs additions to the "who owns
current context instance" variable -- a map?) */ current context instance" variable -- a map?) */
.def_property_readonly("version", &GL::Context::version, "OpenGL version") .def_property_readonly("version", &GL::Context::version, "OpenGL version")

Loading…
Cancel
Save