diff Python/pythonrun.c @ 44162:ef5399b353c9

Solve issue 1400 at least in part -- whenever we run Python code, at the end we also flush stderr and stdout. (XXX this may override errors if there's a problem flushing.)
author Guido van Rossum <guido@python.org>
date Wed, 05 Dec 2007 05:14:58 +0000
parents e08b12c3b913
children 5a0cbe36a603
line wrap: on
line diff
--- a/Python/pythonrun.c	Wed Dec 05 03:38:10 2007 +0000
+++ b/Python/pythonrun.c	Wed Dec 05 05:14:58 2007 +0000
@@ -1442,6 +1442,28 @@
 	return ret;
 }
 
+static void
+flush_io(void)
+{
+	PyObject *f, *r;
+	f = PySys_GetObject("stderr");
+	if (f != NULL) {
+		r = PyObject_CallMethod(f, "flush", "");
+		if (r)
+			Py_DECREF(r);
+		else
+			PyErr_Clear();
+	}
+	f = PySys_GetObject("stdout");
+	if (f != NULL) {
+		r = PyObject_CallMethod(f, "flush", "");
+		if (r)
+			Py_DECREF(r);
+		else
+			PyErr_Clear();
+	}
+}
+
 static PyObject *
 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
 	 PyCompilerFlags *flags, PyArena *arena)
@@ -1453,6 +1475,7 @@
 		return NULL;
 	v = PyEval_EvalCode(co, globals, locals);
 	Py_DECREF(co);
+	flush_io();
 	return v;
 }
 
@@ -1485,6 +1508,7 @@
 	if (v && flags)
 		flags->cf_flags |= (co->co_flags & PyCF_MASK);
 	Py_DECREF(co);
+	flush_io();
 	return v;
 }