[Python-3000-checkins] r56217 - python/branches/py3k-struni/Python/pythonrun.c

guido.van.rossum python-3000-checkins at python.org
Mon Jul 9 17:04:50 CEST 2007


Author: guido.van.rossum
Date: Mon Jul  9 17:04:50 2007
New Revision: 56217

Modified:
   python/branches/py3k-struni/Python/pythonrun.c
Log:
Upon exit, flush stdout and stderr (twice: before and after the code that
attempts to delete all modules).  This makes test_subprocess work again.
(I can't quite figure out why stdout/stderr don't get deleted properly,
which would flush them anyway, but that's a separate issue.)


Modified: python/branches/py3k-struni/Python/pythonrun.c
==============================================================================
--- python/branches/py3k-struni/Python/pythonrun.c	(original)
+++ python/branches/py3k-struni/Python/pythonrun.c	Mon Jul  9 17:04:50 2007
@@ -291,6 +291,32 @@
 extern void dump_counts(FILE*);
 #endif
 
+/* Flush stdout and stderr */
+
+void
+flush_std_files()
+{
+	PyObject *fout = PySys_GetObject("stdout");
+	PyObject *ferr = PySys_GetObject("stderr");
+	PyObject *tmp;
+
+	if (fout != NULL) {
+		tmp = PyObject_CallMethod(fout, "flush", "");
+		if (tmp == NULL)
+			PyErr_Clear();
+		else
+			Py_DECREF(tmp);
+	}
+
+	if (ferr != NULL) {
+		tmp = PyObject_CallMethod(ferr, "flush", "");
+		if (tmp == NULL)
+			PyErr_Clear();
+		else
+			Py_DECREF(tmp);
+	}
+}
+
 /* Undo the effect of Py_Initialize().
 
    Beware: if multiple interpreter and/or thread states exist, these
@@ -326,6 +352,9 @@
 	call_py_exitfuncs();
 	initialized = 0;
 
+	/* Flush stdout+stderr */
+	flush_std_files();
+
 	/* Get current thread state and interpreter pointer */
 	tstate = PyThreadState_GET();
 	interp = tstate->interp;
@@ -361,6 +390,9 @@
 	/* Destroy all modules */
 	PyImport_Cleanup();
 
+	/* Flush stdout+stderr (again, in case more was printed) */
+	flush_std_files();
+
 	/* Collect final garbage.  This disposes of cycles created by
 	 * new-style class definitions, for example.
 	 * XXX This is disabled because it caused too many problems.  If


More information about the Python-3000-checkins mailing list