diff options
author | Ćukasz Langa <lukasz@langa.pl> | 2023-10-14 21:32:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-14 21:32:57 (GMT) |
commit | 84b7e9e3fa67fb9b92088d17839d8235f1cec62e (patch) | |
tree | 662ceaa5e4c9c4fd12695756ef3d0d199f85701d /Python/pylifecycle.c | |
parent | ab08ff7882b6181fb785eed7410dbf8030aded70 (diff) | |
download | cpython-84b7e9e3fa67fb9b92088d17839d8235f1cec62e.zip cpython-84b7e9e3fa67fb9b92088d17839d8235f1cec62e.tar.gz cpython-84b7e9e3fa67fb9b92088d17839d8235f1cec62e.tar.bz2 |
gh-110722: Add PYTHON_PRESITE to import a module before site.py is run (#110769)
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 1403316..7b56034 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1076,6 +1076,38 @@ pyinit_main_reconfigure(PyThreadState *tstate) } +#ifdef Py_DEBUG +static void +run_presite(PyThreadState *tstate) +{ + PyInterpreterState *interp = tstate->interp; + const PyConfig *config = _PyInterpreterState_GetConfig(interp); + + if (!config->run_presite) { + return; + } + + PyObject *presite_modname = PyUnicode_FromWideChar( + config->run_presite, + wcslen(config->run_presite) + ); + if (presite_modname == NULL) { + fprintf(stderr, "Could not convert pre-site module name to unicode\n"); + Py_DECREF(presite_modname); + } + else { + PyObject *presite = PyImport_Import(presite_modname); + if (presite == NULL) { + fprintf(stderr, "pre-site import failed:\n"); + _PyErr_Print(tstate); + } + Py_XDECREF(presite); + Py_DECREF(presite_modname); + } +} +#endif + + static PyStatus init_interp_main(PyThreadState *tstate) { @@ -1157,6 +1189,10 @@ init_interp_main(PyThreadState *tstate) return status; } +#ifdef Py_DEBUG + run_presite(tstate); +#endif + status = add_main_module(interp); if (_PyStatus_EXCEPTION(status)) { return status; |