diff --git a/scripting/PyInt.cpp b/scripting/PyInt.cpp index 2b751225d..876934d4a 100644 --- a/scripting/PyInt.cpp +++ b/scripting/PyInt.cpp @@ -53,15 +53,23 @@ auto screen_builtins() -> PyObject * } // the names a screen defined go to the builtins of the screens, for those that read them without defining them. those of -// abstractscreenrenderer (and __builtins__, __name__) stay as each screen has them, even where the screen rebound them +// abstractscreenrenderer (and __builtins__, __name__) stay as each screen has them, even where the screen rebound them, +// and so do python's own builtins: a screen defining its own max() or round() keeps it to itself void share_screen_names(PyObject *Globals, PyObject *Main, PyObject *Builtins) { + auto *pythonbuiltinsmodule{PyImport_ImportModule("builtins")}; + if (pythonbuiltinsmodule == nullptr) + { + PyErr_Clear(); + return; + } + auto *pythonbuiltins{PyModule_GetDict(pythonbuiltinsmodule)}; // borrowed PyObject *name{nullptr}; PyObject *value{nullptr}; Py_ssize_t position{0}; while (PyDict_Next(Globals, &position, &name, &value) != 0) { - if (PyDict_Contains(Main, name) != 0) + if (PyDict_Contains(Main, name) != 0 || PyDict_Contains(pythonbuiltins, name) != 0) { continue; } @@ -70,6 +78,7 @@ void share_screen_names(PyObject *Globals, PyObject *Main, PyObject *Builtins) PyErr_Clear(); } } + Py_DECREF(pythonbuiltinsmodule); } } // namespace @@ -405,14 +414,6 @@ auto python_taskqueue::init() -> bool { goto release_and_exit; } - // each screen runs in globals of its own, so that one doesn't change the names another (or abstractscreenrenderer) uses - m_screenbuiltins = screen_builtins(); - if (m_screenbuiltins == nullptr) - { - error(); - goto release_and_exit; - } - // release the lock, save the state for future use m_mainthread = PyEval_SaveThread(); @@ -564,6 +565,16 @@ auto python_taskqueue::run_screen_file(std::string_view File, std::string_view P { return nullptr; } + // each screen runs in globals of its own, so that one doesn't change the names another (or abstractscreenrenderer) uses + if (m_screenbuiltins == nullptr) + { + m_screenbuiltins = screen_builtins(); + } + if (m_screenbuiltins == nullptr) + { + error(); + return nullptr; + } // the names of abstractscreenrenderer are copied, not left to the builtins: a screen checks globals() for its class auto *screenglobals{PyDict_Copy(PyModule_GetDict(m_main))}; if (screenglobals == nullptr) @@ -646,12 +657,16 @@ auto python_taskqueue::fetch_renderer(std::string const &Renderer) -> PyObject * goto cache_and_return; } renderer = PyObject_CallObject(renderername, rendererarguments); - - PyObject_CallMethod(renderer, "manul_set_format", "(s)", "RGBA"); + if (renderer != nullptr) + { + auto *formatresult{PyObject_CallMethod(renderer, "manul_set_format", "(s)", "RGBA")}; + Py_XDECREF(formatresult); + } if (PyErr_Occurred() != nullptr) { error(); + Py_XDECREF(renderer); renderer = nullptr; } diff --git a/scripting/PyInt.h b/scripting/PyInt.h index ad033ceaa..dce6ab059 100644 --- a/scripting/PyInt.h +++ b/scripting/PyInt.h @@ -155,7 +155,9 @@ class python_taskqueue PyObject *m_main{nullptr}; // the builtins of the screens: python's own, and the names defined by the screens loaded so far, the last one's when // several define a name. a screen finds there a name it reads but doesn't define itself, as it did when all the screens - // ran in __main__ and one could rebind the names of another + // ran in __main__ and one could rebind the names of another. python's own builtins are not overwritten by them. a name is + // shared by reference as it is once its screen has loaded: a screen rebinding it later (global in a method) changes it + // for itself only, while a change to a shared object (a dict, a list) is seen by all. made with the first screen PyObject *m_screenbuiltins{nullptr}; std::vector m_screenglobals; // the globals of each screen file, kept as long as its classes are PyObject *m_stderr{nullptr};