Skip to content

Commit dbacfc2

Browse files
authored
bpo-36763: _PyInitError always use int for exitcode (GH-13360)
We cannot use "unsigned int" for exitcode on Windows, since Py_Main() and _Py_RunMain() always return an "int". Changes: * _PyPathConfig_ComputeSysPath0() now returns -1 if an exception is raised. * pymain_run_python() no longer uses _PyInitError but display the exception and set exitcode to 1 in case of error. * Fix _Py_RunMain(): return an exitcode rather than calling exit() on pymain_run_python() failure. * _Py_ExitInitError() no longer uses ExitProcess() on Windows, use exit() on all platforms. * _Py_ExitInitError() now fails with a fatal error if 'err' is not an error not an exit.
1 parent 6e78900 commit dbacfc2

File tree

4 files changed

+37
-44
lines changed

4 files changed

+37
-44
lines changed

Include/cpython/coreconfig.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ typedef struct {
1515
} _type;
1616
const char *_func;
1717
const char *err_msg;
18-
#ifdef MS_WINDOWS
19-
unsigned int exitcode;
20-
#else
2118
int exitcode;
22-
#endif
2319
} _PyInitError;
2420

2521
/* Almost all errors causing Python initialization to fail */

Modules/main.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,21 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
130130
if (sysdict != NULL) {
131131
sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
132132
if (sys_path == NULL && PyErr_Occurred()) {
133-
goto error;
133+
return -1;
134134
}
135135
}
136136
else {
137137
sys_path = NULL;
138138
}
139139
if (sys_path == NULL) {
140140
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
141-
goto error;
141+
return -1;
142142
}
143143

144144
if (PyList_Insert(sys_path, 0, path0)) {
145-
goto error;
145+
return -1;
146146
}
147147
return 0;
148-
149-
error:
150-
PyErr_Print();
151-
return -1;
152148
}
153149

154150

@@ -443,11 +439,9 @@ pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
443439
}
444440

445441

446-
static _PyInitError
442+
static void
447443
pymain_run_python(int *exitcode)
448444
{
449-
_PyInitError err;
450-
451445
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
452446
/* pymain_run_stdin() modify the config */
453447
_PyCoreConfig *config = &interp->core_config;
@@ -464,22 +458,20 @@ pymain_run_python(int *exitcode)
464458

465459
if (main_importer_path != NULL) {
466460
if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
467-
err = _Py_INIT_EXIT(1);
468-
goto done;
461+
goto error;
469462
}
470463
}
471464
else if (!config->isolated) {
472465
PyObject *path0 = NULL;
473-
if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) {
474-
if (path0 == NULL) {
475-
err = _Py_INIT_NO_MEMORY();
476-
goto done;
477-
}
466+
int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
467+
if (res < 0) {
468+
goto error;
469+
}
478470

471+
if (res > 0) {
479472
if (pymain_sys_path_add_path0(interp, path0) < 0) {
480473
Py_DECREF(path0);
481-
err = _Py_INIT_EXIT(1);
482-
goto done;
474+
goto error;
483475
}
484476
Py_DECREF(path0);
485477
}
@@ -508,11 +500,14 @@ pymain_run_python(int *exitcode)
508500
}
509501

510502
pymain_repl(config, &cf, exitcode);
511-
err = _Py_INIT_OK();
503+
goto done;
504+
505+
error:
506+
PyErr_Print();
507+
*exitcode = 1;
512508

513509
done:
514510
Py_XDECREF(main_importer_path);
515-
return err;
516511
}
517512

518513

@@ -578,17 +573,14 @@ _Py_RunMain(void)
578573
{
579574
int exitcode = 0;
580575

581-
_PyInitError err = pymain_run_python(&exitcode);
582-
if (_Py_INIT_FAILED(err)) {
583-
pymain_exit_error(err);
584-
}
585-
576+
pymain_run_python(&exitcode);
586577
if (Py_FinalizeEx() < 0) {
587578
/* Value unlikely to be confused with a non-error exit status or
588579
other special meaning */
589580
exitcode = 120;
590581
}
591582

583+
done:
592584
pymain_free();
593585

594586
if (_Py_UnhandledKeyboardInterrupt) {
@@ -603,6 +595,10 @@ static int
603595
pymain_main(_PyArgv *args)
604596
{
605597
_PyInitError err = pymain_init(args);
598+
if (_Py_INIT_IS_EXIT(err)) {
599+
pymain_free();
600+
return err.exitcode;
601+
}
606602
if (_Py_INIT_FAILED(err)) {
607603
pymain_exit_error(err);
608604
}

Python/pathconfig.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,18 +570,17 @@ Py_GetProgramName(void)
570570
directory ("-m module" case) which will be prepended to sys.argv:
571571
sys.path[0].
572572
573-
Return 1 if the path is correctly resolved, but *path0_p can be NULL
574-
if the Unicode object fail to be created.
573+
Return 1 if the path is correctly resolved and written into *path0_p.
575574
576-
Return 0 if it fails to resolve the full path (and *path0_p will be NULL).
577-
For example, return 0 if the current working directory has been removed
578-
(bpo-36236) or if argv is empty.
575+
Return 0 if it fails to resolve the full path. For example, return 0 if the
576+
current working directory has been removed (bpo-36236) or if argv is empty.
577+
578+
Raise an exception and return -1 on error.
579579
*/
580580
int
581581
_PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p)
582582
{
583583
assert(_PyWstrList_CheckConsistency(argv));
584-
assert(*path0_p == NULL);
585584

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

700-
*path0_p = PyUnicode_FromWideChar(path0, n);
699+
PyObject *path0_obj = PyUnicode_FromWideChar(path0, n);
700+
if (path0_obj == NULL) {
701+
return -1;
702+
}
703+
704+
*path0_p = path0_obj;
701705
return 1;
702706
}
703707

Python/pylifecycle.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,18 +2124,15 @@ Py_FatalError(const char *msg)
21242124
void _Py_NO_RETURN
21252125
_Py_ExitInitError(_PyInitError err)
21262126
{
2127-
assert(_Py_INIT_FAILED(err));
21282127
if (_Py_INIT_IS_EXIT(err)) {
2129-
#ifdef MS_WINDOWS
2130-
ExitProcess(err.exitcode);
2131-
#else
21322128
exit(err.exitcode);
2133-
#endif
21342129
}
2135-
else {
2136-
assert(_Py_INIT_IS_ERROR(err));
2130+
else if (_Py_INIT_IS_ERROR(err)) {
21372131
fatal_error(err._func, err.err_msg, 1);
21382132
}
2133+
else {
2134+
Py_FatalError("_Py_ExitInitError() must not be called on success");
2135+
}
21392136
}
21402137

21412138
/* Clean up and exit */

0 commit comments

Comments
 (0)