diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-10-02 13:59:05 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-02 19:59:05 +0000 |
commit | a040a32ea2f13f16172394d3e3e3f80f47f25a68 (patch) | |
tree | 35048792d20e773569686cdcfe06de881f823b2d /Python/pylifecycle.c | |
parent | fc2cb86d210555d509debaeefd370d5331cd9d93 (diff) | |
download | cpython-a040a32ea2f13f16172394d3e3e3f80f47f25a68.tar.gz cpython-a040a32ea2f13f16172394d3e3e3f80f47f25a68.zip |
gh-109853: Fix sys.path[0] For Subinterpreters (gh-109994)
This change makes sure sys.path[0] is set properly for subinterpreters. Before, it wasn't getting set at all. This PR does not address the broader concerns from gh-109853.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index f3ed77e5162..c0323763f44 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1209,6 +1209,31 @@ init_interp_main(PyThreadState *tstate) } } + if (!is_main_interp) { + // The main interpreter is handled in Py_Main(), for now. + if (config->sys_path_0 != NULL) { + PyObject *path0 = PyUnicode_FromWideChar(config->sys_path_0, -1); + if (path0 == NULL) { + return _PyStatus_ERR("can't initialize sys.path[0]"); + } + PyObject *sysdict = interp->sysdict; + if (sysdict == NULL) { + Py_DECREF(path0); + return _PyStatus_ERR("can't initialize sys.path[0]"); + } + PyObject *sys_path = PyDict_GetItemWithError(sysdict, &_Py_ID(path)); + if (sys_path == NULL) { + Py_DECREF(path0); + return _PyStatus_ERR("can't initialize sys.path[0]"); + } + int res = PyList_Insert(sys_path, 0, path0); + Py_DECREF(path0); + if (res) { + return _PyStatus_ERR("can't initialize sys.path[0]"); + } + } + } + assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); |