bpo-36829: sys.excepthook and sys.unraisablehook flush (GH-13620) · python/cpython@a85a1d3 (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ extern "C" {
26 26
27 27 _Py_IDENTIFIER(builtins);
28 28 _Py_IDENTIFIER(stderr);
29 +_Py_IDENTIFIER(flush);
29 30
30 31
31 32 /* Forward declarations */
@@ -1254,6 +1255,14 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
1254 1255 if (PyFile_WriteString("\n", file) < 0) {
1255 1256 return -1;
1256 1257 }
1258 +
1259 +/* Explicitly call file.flush() */
1260 +PyObject *res = _PyObject_CallMethodId(file, &PyId_flush, NULL);
1261 +if (!res) {
1262 +return -1;
1263 + }
1264 +Py_DECREF(res);
1265 +
1257 1266 return 0;
1258 1267 }
1259 1268
Original file line number Diff line number Diff line change
@@ -978,6 +978,16 @@ _PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *t
978 978 }
979 979 print_exception_recursive(file, value, seen);
980 980 Py_XDECREF(seen);
981 +
982 +/* Call file.flush() */
983 +PyObject *res = _PyObject_CallMethodId(file, &PyId_flush, NULL);
984 +if (!res) {
985 +/* Silently ignore file.flush() error */
986 +PyErr_Clear();
987 + }
988 +else {
989 +Py_DECREF(res);
990 + }
981 991 }
982 992
983 993 void