Skip to content

[3.7] bpo-36236: Handle removed cwd at Python init #12450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Include/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
At Python initialization, the current directory is no longer prepended to
:data:`sys.path` if it has been removed.
48 changes: 18 additions & 30 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 9 additions & 4 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}


Expand Down
5 changes: 4 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down