Skip to content

Commit 4cb525a

Browse files
authored
bpo-36356: pymain_exit_error() only call pymain_free() for exit (GH-12968)
Add _Py_INIT_HAS_EXITCODE() macro.
1 parent 00db7c7 commit 4cb525a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Include/cpython/coreconfig.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ typedef struct {
3333
#define _Py_INIT_NO_MEMORY() _Py_INIT_USER_ERR("memory allocation failed")
3434
#define _Py_INIT_EXIT(EXITCODE) \
3535
(_PyInitError){.prefix = NULL, .msg = NULL, .user_err = 0, .exitcode = (EXITCODE)}
36+
#define _Py_INIT_HAS_EXITCODE(err) \
37+
(err.exitcode != -1)
3638
#define _Py_INIT_FAILED(err) \
37-
(err.msg != NULL || err.exitcode != -1)
39+
(err.msg != NULL || _Py_INIT_HAS_EXITCODE(err))
3840

3941
/* --- _PyWstrList ------------------------------------------------ */
4042

Modules/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,12 @@ exit_sigint(void)
570570
static void _Py_NO_RETURN
571571
pymain_exit_error(_PyInitError err)
572572
{
573-
pymain_free();
573+
if (_Py_INIT_HAS_EXITCODE(err)) {
574+
/* If it's an error rather than a regular exit, leave Python runtime
575+
alive: _Py_ExitInitError() uses the current exception and use
576+
sys.stdout in this case. */
577+
pymain_free();
578+
}
574579
_Py_ExitInitError(err);
575580
}
576581

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ Py_FatalError(const char *msg)
21722172
void _Py_NO_RETURN
21732173
_Py_ExitInitError(_PyInitError err)
21742174
{
2175-
if (err.exitcode >= 0) {
2175+
if (_Py_INIT_HAS_EXITCODE(err)) {
21762176
exit(err.exitcode);
21772177
}
21782178
else {

0 commit comments

Comments
 (0)