18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 21:15:20 +01:00

fix(python): the site-packages folders taken from site.getsitepackages(), as site added them

The first version took the folders from sysconfig, which isn't always what the site module added: on Windows site
also put python64 itself on sys.path, and on Linux a distribution's patched Python can give sysconfig another
install scheme than site uses (Debian's posix_local), while linuxpython64 is a venv whose packages are where site
looks. The folders now come from site.getsitepackages(), the ones that exist, added with site.makepath() as
site.addsitepackages() does, still without the .pth files. Importing site doesn't run its main() when the
interpreter was started without it (sys.flags.no_site).

Tried again with the stale easy-install.pth and egg planted in python64: "Python Interpreter: setup complete", no
ImportError. The snippet under python -S adds the same folders as site, minus the user site folder.
This commit is contained in:
maj00r
2026-12-01 22:32:05 +01:00
parent 1ebdb4d6fd
commit c8a3c8feb6
+19 -31
View File
@@ -23,43 +23,31 @@ 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;
}
// the folders site.addsitepackages() puts on sys.path (python64 and python64\Lib\site-packages on Windows,
// lib/python3.x/site-packages elsewhere), the same way, but without running the .pth files in them. importing site
// doesn't run its main() when the interpreter was started without it
constexpr char const *site_packages_code = R"(
import os, site, sys
_known = {site.makepath(_path)[1] for _path in sys.path if _path}
for _folder in site.getsitepackages():
if os.path.isdir(_folder):
_folder, _case = site.makepath(_folder)
if _case not in _known:
sys.path.append(_folder)
_known.add(_case)
)";
// 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);
PyObject *globals = PyDict_New();
PyObject *result = globals != nullptr ? PyRun_String(site_packages_code, Py_file_input, globals, globals) : nullptr;
bool const added = result != nullptr;
Py_XDECREF(result);
Py_XDECREF(globals);
PyErr_Clear();
return result;
return added;
}
// 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