summaryrefslogtreecommitdiffstats
path: root/Modules/main.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-10-02 19:59:05 (GMT)
committerGitHub <noreply@github.com>2023-10-02 19:59:05 (GMT)
commita040a32ea2f13f16172394d3e3e3f80f47f25a68 (patch)
tree35048792d20e773569686cdcfe06de881f823b2d /Modules/main.c
parentfc2cb86d210555d509debaeefd370d5331cd9d93 (diff)
downloadcpython-a040a32ea2f13f16172394d3e3e3f80f47f25a68.zip
cpython-a040a32ea2f13f16172394d3e3e3f80f47f25a68.tar.gz
cpython-a040a32ea2f13f16172394d3e3e3f80f47f25a68.tar.bz2
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 'Modules/main.c')
-rw-r--r--Modules/main.c38
1 files changed, 28 insertions, 10 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 05bedff..8184bed 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -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;
}
}