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. 10
      src/python/magnum/gl.cpp

8
doc/python/conf.py

@ -58,14 +58,6 @@ magnum.TARGET_WEBGL = DoNotPrintValue()
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,

10
src/python/magnum/gl.cpp

@ -373,7 +373,12 @@ 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&) {
.def_property_readonly_static("current",
std::getenv("MCSS_GENERATING_OUTPUT") ?
[](const py::object&) {
return ContextHolder<GL::Context>{nullptr};
} :
[](const py::object&) {
if(!GL::Context::hasCurrent()) {
PyErr_SetString(PyExc_RuntimeError, "no current context");
throw py::error_already_set{};
@ -387,7 +392,8 @@ void gl(py::module_& m) {
}
return ContextHolder<GL::Context>{&GL::Context::current(), std::move(owner)};
}, "Current context")
}
, "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")

Loading…
Cancel
Save