changeset: 106432:c6506f759db1 branch: 3.5 parent: 106424:f48bdcd02b57 user: Steve Dower date: Sat Feb 04 15:39:21 2017 -0800 files: Misc/NEWS Modules/main.c description: Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0]. diff -r f48bdcd02b57 -r c6506f759db1 Misc/NEWS --- a/Misc/NEWS Sat Feb 04 15:05:13 2017 -0800 +++ b/Misc/NEWS Sat Feb 04 15:39:21 2017 -0800 @@ -10,6 +10,8 @@ Core and Builtins ----------------- +- Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0]. + - Issue #29337: Fixed possible BytesWarning when compare the code objects. Warnings could be emitted at compile time. diff -r f48bdcd02b57 -r c6506f759db1 Modules/main.c --- a/Modules/main.c Sat Feb 04 15:05:13 2017 -0800 +++ b/Modules/main.c Sat Feb 04 15:39:21 2017 -0800 @@ -223,7 +223,7 @@ 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 @@ 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; }