]> granicus.if.org Git - python/commitdiff
bpo-36356: pymain_exit_error() only call pymain_free() for exit (GH-12968)
authorVictor Stinner <vstinner@redhat.com>
Fri, 26 Apr 2019 11:05:47 +0000 (13:05 +0200)
committerGitHub <noreply@github.com>
Fri, 26 Apr 2019 11:05:47 +0000 (13:05 +0200)
Add _Py_INIT_HAS_EXITCODE() macro.

Include/cpython/coreconfig.h
Modules/main.c
Python/pylifecycle.c

index c1a72989a5e979a68f42e96ba057069809a5f4ce..ed2f09f933b23a4ffea458dc9b78dda0b2de5fd2 100644 (file)
@@ -33,8 +33,10 @@ typedef struct {
 #define _Py_INIT_NO_MEMORY() _Py_INIT_USER_ERR("memory allocation failed")
 #define _Py_INIT_EXIT(EXITCODE) \
     (_PyInitError){.prefix = NULL, .msg = NULL, .user_err = 0, .exitcode = (EXITCODE)}
+#define _Py_INIT_HAS_EXITCODE(err) \
+    (err.exitcode != -1)
 #define _Py_INIT_FAILED(err) \
-    (err.msg != NULL || err.exitcode != -1)
+    (err.msg != NULL || _Py_INIT_HAS_EXITCODE(err))
 
 /* --- _PyWstrList ------------------------------------------------ */
 
index 6a7f735ed692af33c1970718e06f9319986f5911..68f0b99c9fbbf407703ed5c1444f6f5c405bd6bd 100644 (file)
@@ -570,7 +570,12 @@ exit_sigint(void)
 static void _Py_NO_RETURN
 pymain_exit_error(_PyInitError err)
 {
-    pymain_free();
+    if (_Py_INIT_HAS_EXITCODE(err)) {
+        /* If it's an error rather than a regular exit, leave Python runtime
+           alive: _Py_ExitInitError() uses the current exception and use
+           sys.stdout in this case. */
+        pymain_free();
+    }
     _Py_ExitInitError(err);
 }
 
index ae2d0bf92c6e1f6b4c5956a7fc54bb1b8867154c..d93fe065558aa39bbc499c16363114631342922f 100644 (file)
@@ -2172,7 +2172,7 @@ Py_FatalError(const char *msg)
 void _Py_NO_RETURN
 _Py_ExitInitError(_PyInitError err)
 {
-    if (err.exitcode >= 0) {
+    if (_Py_INIT_HAS_EXITCODE(err)) {
         exit(err.exitcode);
     }
     else {