Skip to content

Commit dfe8847

Browse files
authored
bpo-36142: Rework error reporting in pymain_main() (GH-12113)
Add a new _Py_INIT_EXIT() macro to be able to exit Python with an exitcode using _PyInitError API. Rewrite function calls by pymain_main() to use _PyInitError. Changes: * Remove _PyMain.err and _PyMain.status field * Add _Py_INIT_EXIT() macro and _PyInitError.exitcode field. * Rename _Py_FatalInitError() to _Py_ExitInitError().
1 parent b9f0354 commit dfe8847

8 files changed

Lines changed: 190 additions & 228 deletions

File tree

‎Include/cpython/coreconfig.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct {
1111
const char *prefix;
1212
const char *msg;
1313
int user_err;
14+
int exitcode;
1415
} _PyInitError;
1516

1617
/* Almost all errors causing Python initialization to fail */
@@ -22,16 +23,18 @@ typedef struct {
2223
#endif
2324

2425
#define _Py_INIT_OK() \
25-
(_PyInitError){.prefix = NULL, .msg = NULL, .user_err = 0}
26+
(_PyInitError){.prefix = NULL, .msg = NULL, .user_err = 0, .exitcode = -1}
2627
#define _Py_INIT_ERR(MSG) \
27-
(_PyInitError){.prefix = _Py_INIT_GET_FUNC(), .msg = (MSG), .user_err = 0}
28+
(_PyInitError){.prefix = _Py_INIT_GET_FUNC(), .msg = (MSG), .user_err = 0, .exitcode = -1}
2829
/* Error that can be fixed by the user like invalid input parameter.
2930
Don't abort() the process on such error. */
3031
#define _Py_INIT_USER_ERR(MSG) \
31-
(_PyInitError){.prefix = _Py_INIT_GET_FUNC(), .msg = (MSG), .user_err = 1}
32+
(_PyInitError){.prefix = _Py_INIT_GET_FUNC(), .msg = (MSG), .user_err = 1, .exitcode = -1}
3233
#define _Py_INIT_NO_MEMORY() _Py_INIT_USER_ERR("memory allocation failed")
34+
#define _Py_INIT_EXIT(EXITCODE) \
35+
(_PyInitError){.prefix = NULL, .msg = NULL, .user_err = 0, .exitcode = (EXITCODE)}
3336
#define _Py_INIT_FAILED(err) \
34-
(err.msg != NULL)
37+
(err.msg != NULL || err.exitcode != -1)
3538

3639
/* _PyCoreConfig */
3740

‎Include/cpython/pylifecycle.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ PyAPI_FUNC(_PyInitError) _Py_InitializeMainInterpreter(
3939

4040
PyAPI_FUNC(_PyInitError) _Py_InitializeFromConfig(
4141
const _PyCoreConfig *config);
42-
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalInitError(_PyInitError err);
42+
PyAPI_FUNC(void) _Py_NO_RETURN _Py_ExitInitError(_PyInitError err);
4343

4444
/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
4545
* exit functions.

0 commit comments

Comments
 (0)