mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
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.
This commit is contained in:
parent
fc2cb86d21
commit
a040a32ea2
7 changed files with 214 additions and 10 deletions
|
@ -556,6 +556,11 @@ pymain_run_python(int *exitcode)
|
|||
goto error;
|
||||
}
|
||||
|
||||
// XXX Calculate config->sys_path_0 in getpath.py.
|
||||
// The tricky part is that we can't check the path importers yet
|
||||
// at that point.
|
||||
assert(config->sys_path_0 == NULL);
|
||||
|
||||
if (config->run_filename != NULL) {
|
||||
/* If filename is a package (ex: directory or ZIP file) which contains
|
||||
__main__.py, main_importer_path is set to filename and will be
|
||||
|
@ -571,24 +576,37 @@ pymain_run_python(int *exitcode)
|
|||
// import readline and rlcompleter before script dir is added to sys.path
|
||||
pymain_import_readline(config);
|
||||
|
||||
PyObject *path0 = NULL;
|
||||
if (main_importer_path != NULL) {
|
||||
if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
|
||||
goto error;
|
||||
}
|
||||
path0 = Py_NewRef(main_importer_path);
|
||||
}
|
||||
else if (!config->safe_path) {
|
||||
PyObject *path0 = NULL;
|
||||
int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
|
||||
if (res < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (res > 0) {
|
||||
if (pymain_sys_path_add_path0(interp, path0) < 0) {
|
||||
Py_DECREF(path0);
|
||||
goto error;
|
||||
}
|
||||
else if (res == 0) {
|
||||
Py_CLEAR(path0);
|
||||
}
|
||||
}
|
||||
// XXX Apply config->sys_path_0 in init_interp_main(). We have
|
||||
// to be sure to get readline/rlcompleter imported at the correct time.
|
||||
if (path0 != NULL) {
|
||||
wchar_t *wstr = PyUnicode_AsWideCharString(path0, NULL);
|
||||
if (wstr == NULL) {
|
||||
Py_DECREF(path0);
|
||||
goto error;
|
||||
}
|
||||
config->sys_path_0 = _PyMem_RawWcsdup(wstr);
|
||||
PyMem_Free(wstr);
|
||||
if (config->sys_path_0 == NULL) {
|
||||
Py_DECREF(path0);
|
||||
goto error;
|
||||
}
|
||||
int res = pymain_sys_path_add_path0(interp, path0);
|
||||
Py_DECREF(path0);
|
||||
if (res < 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue