summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-11-26 21:02:29 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-11-26 21:02:29 (GMT)
commit5604ef3e36756e59d3396ed16d7a94de2687e0ac (patch)
tree73c134b68d7a8536a9002cf427af96e67dafebf7 /Python
parent59ff2c56402362702054fa06c2b360326941e940 (diff)
parentd7c8fbf89e751d43c56de0071702d2578676d0a1 (diff)
downloadcpython-5604ef3e36756e59d3396ed16d7a94de2687e0ac.zip
cpython-5604ef3e36756e59d3396ed16d7a94de2687e0ac.tar.gz
cpython-5604ef3e36756e59d3396ed16d7a94de2687e0ac.tar.bz2
Issue #13444: When stdout has been closed explicitly, we should not attempt to flush it at shutdown and print an error.
This also adds a test for issue #5319, whose resolution introduced the issue.
Diffstat (limited to 'Python')
-rw-r--r--Python/pythonrun.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 389bcd0..4956943 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -348,6 +348,22 @@ extern void dump_counts(FILE*);
/* Flush stdout and stderr */
+static int
+file_is_closed(PyObject *fobj)
+{
+ int r;
+ PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
+ if (tmp == NULL) {
+ PyErr_Clear();
+ return 0;
+ }
+ r = PyObject_IsTrue(tmp);
+ Py_DECREF(tmp);
+ if (r < 0)
+ PyErr_Clear();
+ return r > 0;
+}
+
static void
flush_std_files(void)
{
@@ -356,7 +372,7 @@ flush_std_files(void)
PyObject *tmp;
_Py_IDENTIFIER(flush);
- if (fout != NULL && fout != Py_None) {
+ if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
if (tmp == NULL)
PyErr_WriteUnraisable(fout);
@@ -364,7 +380,7 @@ flush_std_files(void)
Py_DECREF(tmp);
}
- if (ferr != NULL && ferr != Py_None) {
+ if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
if (tmp == NULL)
PyErr_Clear();