Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c008dde

Browse files
committed
Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
2 parents 5f9193a + 6d46ae7 commit c008dde

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.6.1 release candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
14+
1315
- Issue #29337: Fixed possible BytesWarning when compare the code objects.
1416
Warnings could be emitted at compile time.
1517

Modules/main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static int RunModule(wchar_t *modname, int set_argv0)
228228
static int
229229
RunMainFromImporter(wchar_t *filename)
230230
{
231-
PyObject *argv0 = NULL, *importer, *sys_path;
231+
PyObject *argv0 = NULL, *importer, *sys_path, *sys_path0;
232232
int sts;
233233

234234
argv0 = PyUnicode_FromWideChar(filename, wcslen(filename));
@@ -253,7 +253,17 @@ RunMainFromImporter(wchar_t *filename)
253253
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
254254
goto error;
255255
}
256-
if (PyList_SetItem(sys_path, 0, argv0)) {
256+
sys_path0 = PyList_GetItem(sys_path, 0);
257+
sts = 0;
258+
if (!sys_path0) {
259+
PyErr_Clear();
260+
sts = PyList_Append(sys_path, argv0);
261+
} else if (PyObject_IsTrue(sys_path0)) {
262+
sts = PyList_Insert(sys_path, 0, argv0);
263+
} else {
264+
sts = PyList_SetItem(sys_path, 0, argv0);
265+
}
266+
if (sts) {
257267
argv0 = NULL;
258268
goto error;
259269
}

0 commit comments

Comments
 (0)