18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 03:37:09 +01:00

fix(python): a screen's own builtin names stay its own; the format call released

After review of #561:

- share_screen_names() skips the names of Python's builtins module, so a screen that defines its own max() or
  round() at module level no longer changes them for every other screen.
- The comment of m_screenbuiltins says that the names are shared by reference as they are once their screen has
  loaded: a later rebinding (global in a method) stays with that screen, a change to a shared object doesn't.
- The builtins of the screens are made with the first screen rather than in init(), which keeps init() as it was.
- fetch_renderer() calls manul_set_format only on a renderer that was made, releases its result, and releases a
  renderer that failed there instead of leaking it.
This commit is contained in:
maj00r
2026-11-30 19:20:08 +01:00
parent ee03ddf0f1
commit 2a7a002bbc
2 changed files with 30 additions and 13 deletions
+27 -12
View File
@@ -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;
}