diff options
author | Steve Dower <steve.dower@microsoft.com> | 2017-02-04 15:39:21 -0800 |
---|---|---|
committer | Steve Dower <steve.dower@microsoft.com> | 2017-02-04 15:39:21 -0800 |
commit | 956c7cfa7111ab5458e2f69868a05b7b84fc6843 (patch) | |
tree | 7695481ab9636bb17b1b2e4b5fec592d746cae82 /Modules | |
parent | 134e58fd3aaa2e91390041e143f3f0a21a60142b (diff) | |
download | cpython-956c7cfa7111ab5458e2f69868a05b7b84fc6843.tar.gz |
Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/main.c | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Modules/main.c b/Modules/main.c index e4c955e011..75ee2f607c 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -223,7 +223,7 @@ static int RunModule(wchar_t *modname, int set_argv0) static int RunMainFromImporter(wchar_t *filename) { - PyObject *argv0 = NULL, *importer, *sys_path; + PyObject *argv0 = NULL, *importer, *sys_path, *sys_path0; int sts; argv0 = PyUnicode_FromWideChar(filename, wcslen(filename)); @@ -248,7 +248,17 @@ RunMainFromImporter(wchar_t *filename) PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); goto error; } - if (PyList_SetItem(sys_path, 0, argv0)) { + sys_path0 = PyList_GetItem(sys_path, 0); + sts = 0; + if (!sys_path0) { + PyErr_Clear(); + sts = PyList_Append(sys_path, argv0); + } else if (PyObject_IsTrue(sys_path0)) { + sts = PyList_Insert(sys_path, 0, argv0); + } else { + sts = PyList_SetItem(sys_path, 0, argv0); + } + if (sts) { argv0 = NULL; goto error; } |