diff options
author | Steve Dower <steve.dower@microsoft.com> | 2017-02-04 15:41:12 -0800 |
---|---|---|
committer | Steve Dower <steve.dower@microsoft.com> | 2017-02-04 15:41:12 -0800 |
commit | cf1f5444eb42a8dc252b295d3863288309996a5d (patch) | |
tree | f2c6ceb55bd910306c8835c180871ce46a3f87ce | |
parent | 06c5773ee2e8365a84a4ed363d802c7dfa5c7dd8 (diff) | |
parent | 1a5780aabb550cae175ad8711e2f33ba644d0ddb (diff) | |
download | cpython-cf1f5444eb42a8dc252b295d3863288309996a5d.tar.gz |
Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Modules/main.c | 14 |
2 files changed, 14 insertions, 2 deletions
@@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 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. diff --git a/Modules/main.c b/Modules/main.c index 020c353e0c..633e757691 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -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)); @@ -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; } |