Skip to content

Commit

Permalink
Issue python#29319: Prevent RunMainFromImporter overwriting sys.path[0].
Browse files Browse the repository at this point in the history
  • Loading branch information
zooba committed Feb 4, 2017
2 parents 5f9193a + 6d46ae7 commit c008dde
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ What's New in Python 3.6.1 release candidate 1?
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.

Expand Down
14 changes: 12 additions & 2 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,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));
Expand All @@ -253,7 +253,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;
}
Expand Down

0 comments on commit c008dde

Please sign in to comment.