]> granicus.if.org Git - python/commitdiff
Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
authorSteve Dower <steve.dower@microsoft.com>
Sat, 4 Feb 2017 23:39:21 +0000 (15:39 -0800)
committerSteve Dower <steve.dower@microsoft.com>
Sat, 4 Feb 2017 23:39:21 +0000 (15:39 -0800)
Misc/NEWS
Modules/main.c

index fc8b3ade5036d37185e116bdcc41247019ad6f98..8419915c8ece96c3bf8bfd0194435c7ad02211ce 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: XXXX-XX-XX
 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.
 
index e4c955e011db25368b92c0b8fe9c8d3dbc8490ba..75ee2f607ca83d1ada94f340961a52ee01292a83 100644 (file)
@@ -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;
     }