[Python-checkins] bpo-36829: sys.excepthook and sys.unraisablehook flush (GH-13620)

Victor Stinner webhook-mailer at python.org
Tue May 28 10:01:24 EDT 2019


https://github.com/python/cpython/commit/a85a1d337d26a65036e427341d15e3979f7e9ced
commit: a85a1d337d26a65036e427341d15e3979f7e9ced
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019-05-28T16:01:17+02:00
summary:

bpo-36829: sys.excepthook and sys.unraisablehook flush (GH-13620)

sys.excepthook() and sys.unraisablehook() now explicitly flush the
file (usually sys.stderr).

If file.flush() fails, sys.excepthook() silently ignores the error,
whereas sys.unraisablehook() logs the new exception.

files:
M Python/errors.c
M Python/pythonrun.c

diff --git a/Python/errors.c b/Python/errors.c
index bd33d4d340f6..8a94afdd8c41 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -26,6 +26,7 @@ extern "C" {
 
 _Py_IDENTIFIER(builtins);
 _Py_IDENTIFIER(stderr);
+_Py_IDENTIFIER(flush);
 
 
 /* Forward declarations */
@@ -1254,6 +1255,14 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
     if (PyFile_WriteString("\n", file) < 0) {
         return -1;
     }
+
+    /* Explicitly call file.flush() */
+    PyObject *res = _PyObject_CallMethodId(file, &PyId_flush, NULL);
+    if (!res) {
+        return -1;
+    }
+    Py_DECREF(res);
+
     return 0;
 }
 
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index ba1d1cf02f25..ace9f2f9874e 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -978,6 +978,16 @@ _PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *t
     }
     print_exception_recursive(file, value, seen);
     Py_XDECREF(seen);
+
+    /* Call file.flush() */
+    PyObject *res = _PyObject_CallMethodId(file, &PyId_flush, NULL);
+    if (!res) {
+        /* Silently ignore file.flush() error */
+        PyErr_Clear();
+    }
+    else {
+        Py_DECREF(res);
+    }
 }
 
 void



More information about the Python-checkins mailing list