summaryrefslogtreecommitdiffstats
path: root/Modules/main.c
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2017-02-04 23:39:38 (GMT)
committerSteve Dower <steve.dower@microsoft.com>2017-02-04 23:39:38 (GMT)
commitc008ddeb21528d4394fb20156a81f851af79f701 (patch)
treeb9b2109a872a33cbe6f6d56791e39abd9a6856a0 /Modules/main.c
parent5f9193a6edf1864a243def36ec4d1277054236c6 (diff)
parent6d46ae7d12d2cbe4c5b2bd7b023dd37ff12dec68 (diff)
downloadcpython-c008ddeb21528d4394fb20156a81f851af79f701.zip
cpython-c008ddeb21528d4394fb20156a81f851af79f701.tar.gz
cpython-c008ddeb21528d4394fb20156a81f851af79f701.tar.bz2
Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
Diffstat (limited to 'Modules/main.c')
-rw-r--r--Modules/main.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 4cbe376..2e6a60b 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;
}