[Python-checkins] bpo-36236: Handle removed cwd at Python init (GH-12450)
Victor Stinner
webhook-mailer at python.org
Tue Mar 19 19:30:51 EDT 2019
https://github.com/python/cpython/commit/f7959a9fe7e7e316899c60251e29390c4ed0307a
commit: f7959a9fe7e7e316899c60251e29390c4ed0307a
branch: 3.7
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019-03-20T00:30:45+01:00
summary:
bpo-36236: Handle removed cwd at Python init (GH-12450)
At Python initialization, the current directory is no longer
prepended to sys.path if it has been removed.
files:
A Misc/NEWS.d/next/Core and Builtins/2019-03-19-23-55-00.bpo-36236.5qN9qK.rst
M Include/pylifecycle.h
M Modules/main.c
M Python/pathconfig.c
M Python/sysmodule.c
diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h
index 05f17dc73f08..5d9f049d8a1d 100644
--- a/Include/pylifecycle.h
+++ b/Include/pylifecycle.h
@@ -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
index 000000000000..e1c1182d181a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-03-19-23-55-00.bpo-36236.5qN9qK.rst
@@ -0,0 +1,2 @@
+At Python initialization, the current directory is no longer prepended to
+:data:`sys.path` if it has been removed.
diff --git a/Modules/main.c b/Modules/main.c
index f94689496a38..9011bd1f69cb 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -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;
}
diff --git a/Python/pathconfig.c b/Python/pathconfig.c
index aacc5f5a5f02..3a431431351b 100644
--- a/Python/pathconfig.c
+++ b/Python/pathconfig.c
@@ -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;
}
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index cdc2edf038a1..c01a04e6c030 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -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");
}
More information about the Python-checkins
mailing list