From f7959a9fe7e7e316899c60251e29390c4ed0307a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 20 Mar 2019 00:30:45 +0100 Subject: 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. --- Python/pathconfig.c | 13 +++++++++---- Python/sysmodule.c | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'Python') diff --git a/Python/pathconfig.c b/Python/pathconfig.c index aacc5f5a5f..3a43143135 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 cdc2edf038..c01a04e6c0 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"); } -- cgit v1.2.1