]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36236: Handle removed cwd at Python init (GH-12450)
authorVictor Stinner <vstinner@redhat.com>
Tue, 19 Mar 2019 23:30:45 +0000 (00:30 +0100)
committerGitHub <noreply@github.com>
Tue, 19 Mar 2019 23:30:45 +0000 (00:30 +0100)
At Python initialization, the current directory is no longer
prepended to sys.path if it has been removed.

Include/pylifecycle.h
Misc/NEWS.d/next/Core and Builtins/2019-03-19-23-55-00.bpo-36236.5qN9qK.rst [new file with mode: 0644]
Modules/main.c
Python/pathconfig.c
Python/sysmodule.c

index 05f17dc73f08ce0df36fc9978d4b4009bb38de8b..5d9f049d8a1d93533120205dd89dbb1448359421 100644 (file)
@@ -139,7 +139,9 @@ PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
 PyAPI_FUNC(wchar_t *) Py_GetPath(void);
 #ifdef Py_BUILD_CORE
 PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(const _PyCoreConfig *core_config);
-PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
+PyAPI_FUNC(int) _PyPathConfig_ComputeArgv0(
+    int argc, wchar_t **argv,
+    PyObject **argv0_p);
 PyAPI_FUNC(int) _Py_FindEnvConfigValue(
     FILE *env_file,
     const wchar_t *key,
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-03-19-23-55-00.bpo-36236.5qN9qK.rst b/Misc/NEWS.d/next/Core and Builtins/2019-03-19-23-55-00.bpo-36236.5qN9qK.rst
new file mode 100644 (file)
index 0000000..e1c1182
--- /dev/null
@@ -0,0 +1,2 @@
+At Python initialization, the current directory is no longer prepended to
+:data:`sys.path` if it has been removed.
index f94689496a38fca00c7f69566ba16f1eb90a4b3b..9011bd1f69cb05b763f7811c0ff9285fa4871ac2 100644 (file)
@@ -1339,29 +1339,6 @@ _Py_wstrlist_as_pylist(int len, wchar_t **list)
 }
 
 
-static int
-pymain_compute_path0(_PyMain *pymain, _PyCoreConfig *config, PyObject **path0)
-{
-    if (pymain->main_importer_path != NULL) {
-        /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
-        *path0 = NULL;
-        return 0;
-    }
-
-    if (Py_IsolatedFlag) {
-        *path0 = NULL;
-        return 0;
-    }
-
-    *path0 = _PyPathConfig_ComputeArgv0(config->argc, config->argv);
-    if (*path0 == NULL) {
-        pymain->err = _Py_INIT_NO_MEMORY();
-        return -1;
-    }
-    return 0;
-}
-
-
 static int
 pymain_update_sys_path(_PyMain *pymain, PyObject *path0)
 {
@@ -2845,18 +2822,29 @@ pymain_init_sys_path(_PyMain *pymain, _PyCoreConfig *config)
         pymain->main_importer_path = pymain_get_importer(pymain->filename);
     }
 
-    PyObject *path0;
-    if (pymain_compute_path0(pymain, config, &path0) < 0) {
+    if (pymain->main_importer_path != NULL) {
+        /* Let pymain_run_main_from_importer() adjust sys.path[0] later */
+        return 0;
+    }
+
+    if (Py_IsolatedFlag) {
+        return 0;
+    }
+
+    PyObject *path0 = NULL;
+    if (!_PyPathConfig_ComputeArgv0(config->argc, config->argv, &path0)) {
+        return 0;
+    }
+    if (path0 == NULL) {
+        pymain->err = _Py_INIT_NO_MEMORY();
         return -1;
     }
 
-    if (path0 != NULL) {
-        if (pymain_update_sys_path(pymain, path0) < 0) {
-            Py_DECREF(path0);
-            return -1;
-        }
+    if (pymain_update_sys_path(pymain, path0) < 0) {
         Py_DECREF(path0);
+        return -1;
     }
+    Py_DECREF(path0);
     return 0;
 }
 
index aacc5f5a5f0211a1ae26df963c04de1fc33332a9..3a431431351bf7fd80ccb80018b9735cc04fa1d7 100644 (file)
@@ -278,8 +278,8 @@ Py_GetProgramName(void)
 }
 
 /* Compute argv[0] which will be prepended to sys.argv */
-PyObject*
-_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
+int
+_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv, PyObject **argv0_p)
 {
     wchar_t *argv0;
     wchar_t *p = NULL;
@@ -297,6 +297,8 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
     wchar_t fullpath[MAX_PATH];
 #endif
 
+    assert(*argv0_p == NULL);
+
     argv0 = argv[0];
     if (argc > 0 && argv0 != NULL) {
         have_module_arg = (wcscmp(argv0, L"-m") == 0);
@@ -305,7 +307,9 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
 
     if (have_module_arg) {
         #if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
-            _Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
+            if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
+                return 0;
+            }
             argv0 = fullpath;
             n = wcslen(argv0);
         #else
@@ -384,7 +388,8 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
     }
 #endif /* All others */
 
-    return PyUnicode_FromWideChar(argv0, n);
+    *argv0_p = PyUnicode_FromWideChar(argv0, n);
+    return 1;
 }
 
 
index cdc2edf038a13967705f824e25f1291d3cc28a42..c01a04e6c030fced9f419f858375eb1695cec954 100644 (file)
@@ -2615,7 +2615,10 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
     if (updatepath) {
         /* If argv[0] is not '-c' nor '-m', prepend argv[0] to sys.path.
            If argv[0] is a symlink, use the real path. */
-        PyObject *argv0 = _PyPathConfig_ComputeArgv0(argc, argv);
+        PyObject *argv0 = NULL;
+        if (!_PyPathConfig_ComputeArgv0(argc, argv, &argv0)) {
+            return;
+        }
         if (argv0 == NULL) {
             Py_FatalError("can't compute path0 from argv");
         }