diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-11-27 22:21:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-27 22:21:12 (GMT) |
commit | 313554457e5a07e19e455cf6334925670be199a0 (patch) | |
tree | 361379340c55d87d044314858aa52a0c7de93f69 /Python | |
parent | 592a849fdf72f03a05f7e05570fab539b2c89257 (diff) | |
download | cpython-313554457e5a07e19e455cf6334925670be199a0.zip cpython-313554457e5a07e19e455cf6334925670be199a0.tar.gz cpython-313554457e5a07e19e455cf6334925670be199a0.tar.bz2 |
[3.12] gh-109853: Fix sys.path[0] For Subinterpreters (gh-109994) (gh-110701)
This change makes sure sys.path[0] is set properly for subinterpreters. Before, it wasn't getting set at all.
This change does not address the broader concerns from gh-109853.
(cherry-picked from commit a040a32ea2f13f16172394d3e3e3f80f47f25a68)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pylifecycle.c | 26 | ||||
-rw-r--r-- | Python/pystate.c | 4 |
2 files changed, 30 insertions, 0 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 29771e0..ee0eb79 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1200,6 +1200,32 @@ init_interp_main(PyThreadState *tstate) #endif } + if (!is_main_interp) { + // The main interpreter is handled in Py_Main(), for now. + wchar_t *sys_path_0 = interp->runtime->sys_path_0; + if (sys_path_0 != NULL) { + PyObject *path0 = PyUnicode_FromWideChar(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(); diff --git a/Python/pystate.c b/Python/pystate.c index 4dfaded..e789eb9 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -524,6 +524,10 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime) } #undef FREE_LOCK + if (runtime->sys_path_0 != NULL) { + PyMem_RawFree(runtime->sys_path_0); + runtime->sys_path_0 = NULL; + } PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } |