18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 22:25:23 +01:00

fix(python): the screens start when an older install left .pth files in site-packages

A player's log showed no Python screen in any cab: importing PIL in abstractscreenrenderer failed with
"cannot import name 'six' from 'pkg_resources.extern'", coming from pillow-4.0.0-py2.7-win-amd64.egg in
python64\Lib\site-packages. Neither that egg nor pkg_resources is in the current Steam build: they are what an
older Python 2.7 install left in the game folder. The site module runs every .pth file in site-packages, and the
easy-install.pth of the old setuptools moves its eggs to the front of sys.path, so the old Pillow was found before
the one the game comes with.

The interpreter now starts without the site module (site_import = 0) and puts the site-packages folders that
sysconfig gives (purelib, platlib) at the end of sys.path itself, where site put them, without running the .pth
files. A clean install has no .pth files, so for it nothing changes. The user site folder of the player's own
Python (%APPDATA%\Python) is no longer read either.

Tried in a test folder with an easy-install.pth and an egg whose PIL raises ImportError planted in python64: the
build of master before this change logs the ImportError from the egg, this one logs "Python Interpreter: setup
complete". Linux and macOS use the same sysconfig paths but weren't run.
This commit is contained in:
maj00r
2026-12-01 22:07:37 +01:00
parent 07b895d8ff
commit 1ebdb4d6fd
+48
View File
@@ -23,6 +23,45 @@ http://mozilla.org/MPL/2.0/.
namespace
{
// appends the item to the list unless it's there already. false on a Python error
auto append_once(PyObject *List, PyObject *Item) -> bool
{
int const contained = PySequence_Contains(List, Item);
if (contained != 0)
{
return contained > 0;
}
return PyList_Append(List, Item) == 0;
}
// appends the folders of the installed packages (site-packages) of the interpreter to sys.path, as the site module does,
// but without running the .pth files in them. returns false when they couldn't be told or added
auto add_site_packages() -> bool
{
PyObject *syspath = PySys_GetObject("path"); // borrowed ref
PyObject *sysconfig = PyImport_ImportModule("sysconfig");
if (syspath == nullptr || sysconfig == nullptr)
{
Py_XDECREF(sysconfig);
PyErr_Clear();
return false;
}
bool result = true;
for (char const *scheme : {"purelib", "platlib"})
{
PyObject *folder = PyObject_CallMethod(sysconfig, "get_path", "s", scheme);
// the two are the same folder on Windows; a folder is added once
if (folder == nullptr || false == append_once(syspath, folder))
{
result = false;
}
Py_XDECREF(folder);
}
Py_DECREF(sysconfig);
PyErr_Clear();
return result;
}
// the file a script is in, looked for in the path given and then in python/local, and its text. no file name when there's none
auto read_script(std::string_view File, std::string_view Path) -> std::pair<std::string, std::string>
{
@@ -352,6 +391,10 @@ auto python_taskqueue::init() -> bool
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.install_signal_handlers = 0; // matches former Py_InitializeEx(0)
// no site module: besides putting site-packages on sys.path it runs every .pth file there, and those an older
// install left in the game folder (easy-install.pth of a Python 2.7 one) put its Pillow ahead of the one the game
// comes with, so the import failed and no screen was drawn. add_site_packages() puts the folders on sys.path instead
config.site_import = 0;
PyStatus status = PyConfig_SetString(&config, &config.home, pythonhome);
if (PyStatus_Exception(status))
@@ -376,6 +419,11 @@ auto python_taskqueue::init() -> bool
PyConfig_Clear(&config);
}
if (false == add_site_packages())
{
ErrorLog("Python Interpreter: failed to add the site-packages folders to sys.path");
}
// make CWD-local packages (e.g. the asset-side "scripts" package used by
// texture/screen renderers) importable. Embedded CPython builds sys.path
// from PYTHONHOME only; unlike the python CLI it does not prepend the