mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 01:17:23 +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:
+27
-12
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -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<PyObject *> m_screenglobals; // the globals of each screen file, kept as long as its classes are
|
||||
PyObject *m_stderr{nullptr};
|
||||
|
||||
Reference in New Issue
Block a user