◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
4 changes: 0 additions & 4 deletions Include/cpython/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ typedef struct {
} _type;
const char *_func;
const char *err_msg;
#ifdef MS_WINDOWS
unsigned int exitcode;
#else
int exitcode;
#endif
} _PyInitError;

/* Almost all errors causing Python initialization to fail */
Expand Down
48 changes: 22 additions & 26 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,21 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
if (sysdict != NULL) {
sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
if (sys_path == NULL && PyErr_Occurred()) {
goto error;
}
}
else {
sys_path = NULL;
}
if (sys_path == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
goto error;
}

if (PyList_Insert(sys_path, 0, path0)) {
goto error;
}
return 0;

error:
PyErr_Print();
return -1;
}


Expand Down Expand Up @@ -443,11 +439,9 @@ pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
}


static _PyInitError
pymain_run_python(int *exitcode)
{
_PyInitError err;

PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* pymain_run_stdin() modify the config */
_PyCoreConfig *config = &interp->core_config;
Expand All @@ -464,22 +458,20 @@ pymain_run_python(int *exitcode)

if (main_importer_path != NULL) {
if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
err = _Py_INIT_EXIT(1);
goto done;
}
}
else if (!config->isolated) {
PyObject *path0 = NULL;
if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) {
if (path0 == NULL) {
err = _Py_INIT_NO_MEMORY();
goto done;
}

if (pymain_sys_path_add_path0(interp, path0) < 0) {
Py_DECREF(path0);
err = _Py_INIT_EXIT(1);
goto done;
}
Py_DECREF(path0);
}
Expand Down Expand Up @@ -508,11 +500,14 @@ pymain_run_python(int *exitcode)
}

pymain_repl(config, &cf, exitcode);
err = _Py_INIT_OK();

done:
Py_XDECREF(main_importer_path);
return err;
}


Expand Down Expand Up @@ -578,17 +573,14 @@ _Py_RunMain(void)
{
int exitcode = 0;

_PyInitError err = pymain_run_python(&exitcode);
if (_Py_INIT_FAILED(err)) {
pymain_exit_error(err);
}

if (Py_FinalizeEx() < 0) {
/* Value unlikely to be confused with a non-error exit status or
other special meaning */
exitcode = 120;
}

pymain_free();

if (_Py_UnhandledKeyboardInterrupt) {
Expand All @@ -603,6 +595,10 @@ static int
pymain_main(_PyArgv *args)
{
_PyInitError err = pymain_init(args);
if (_Py_INIT_FAILED(err)) {
pymain_exit_error(err);
}
Expand Down
18 changes: 11 additions & 7 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
@@ -570,18 +570,17 @@ Py_GetProgramName(void)
directory ("-m module" case) which will be prepended to sys.argv:
sys.path[0].

Return 1 if the path is correctly resolved, but *path0_p can be NULL
if the Unicode object fail to be created.

Return 0 if it fails to resolve the full path (and *path0_p will be NULL).
For example, return 0 if the current working directory has been removed
(bpo-36236) or if argv is empty.
*/
int
_PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p)
{
assert(_PyWstrList_CheckConsistency(argv));
assert(*path0_p == NULL);

if (argv->length == 0) {
/* Leave sys.path unchanged if sys.argv is empty */
Expand Down Expand Up @@ -697,7 +696,12 @@ _PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p)
}
#endif /* All others */

*path0_p = PyUnicode_FromWideChar(path0, n);
return 1;
}

Expand Down
Toggle all file notes Toggle all file annotations