Skip to content
Closed
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
3 changes: 2 additions & 1 deletion Include/internal/pycore_pathconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ PyAPI_FUNC(_PyInitError) _PyPathConfig_SetGlobal(
PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate_impl(
_PyPathConfig *config,
const _PyCoreConfig *core_config);
PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
PyAPI_FUNC(_PyInitError) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv,
PyObject **result);
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 @@
Improve the error message that occurs when executing :code:`python -m
modulename` from a deleted directory on MacOS.
8 changes: 5 additions & 3 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,12 @@ pymain_run_python(PyInterpreterState *interp, int *exitcode)
}
}
else if (!config->preconfig.isolated) {
PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
config->argv);
PyObject *path0 = NULL;
_PyInitError error = _PyPathConfig_ComputeArgv0(config->argc,
config->argv,
&path0);
if (path0 == NULL) {
err = _Py_INIT_NO_MEMORY();
err = error;
goto done;
}

Expand Down
16 changes: 11 additions & 5 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ Py_GetProgramName(void)
}

/* Compute argv[0] which will be prepended to sys.argv */
PyObject*
_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
_PyInitError
_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv, PyObject** result)
{
wchar_t *argv0;
wchar_t *p = NULL;
Expand All @@ -584,6 +584,7 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
#elif defined(MS_WINDOWS)
wchar_t fullpath[MAX_PATH];
#endif
assert(*result == NULL);

argv0 = argv[0];
if (argc > 0 && argv0 != NULL) {
Expand All @@ -593,7 +594,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 _Py_INIT_USER_ERR("Failed the obtain current directory");
}
argv0 = fullpath;
n = wcslen(argv0);
#else
Expand Down Expand Up @@ -671,8 +674,11 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
#endif /* Unix */
}
#endif /* All others */

return PyUnicode_FromWideChar(argv0, n);
*result = PyUnicode_FromWideChar(argv0, n);
if (*result == NULL) {
return _Py_INIT_NO_MEMORY();
}
return _Py_INIT_OK();
}


Expand Down
3 changes: 2 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,8 @@ 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;
_PyPathConfig_ComputeArgv0(argc, argv, &argv0);
if (argv0 == NULL) {
Py_FatalError("can't compute path0 from argv");
}
Expand Down