Commits

Martin von Löwis  committed 8c9e3ed

Flush std{in,out,err} before closing it. Fixes #1074011.

  • Participants
  • Parent commits e3d7483
  • Branches 2.3

Comments (0)

Files changed (2)

 Core and builtins
 -----------------
 
+- Bug #1074011: closing sys.std{in,out,err} now causes a flush() and 
+  an ferror() call.
+
 - Bug #1055820 Cyclic garbage collection was not protecting against that
   calling a live weakref to a piece of cyclic trash could resurrect an
   insane mutation of the trash if any Python code ran during gc (via

File Python/sysmodule.c

 )
 /* end of sys_doc */ ;
 
+static int
+_check_and_flush (FILE *stream)
+{
+  int prev_fail = ferror (stream);
+  return fflush (stream) || prev_fail ? EOF : 0;
+}
+
 PyObject *
 _PySys_Init(void)
 {
 	m = Py_InitModule3("sys", sys_methods, sys_doc);
 	sysdict = PyModule_GetDict(m);
 
-	sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
-	sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
-	syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
+	sysin = PyFile_FromFile(stdin, "<stdin>", "r", _check_and_flush);
+	sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
+	syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
 	if (PyErr_Occurred())
 		return NULL;
 #ifdef MS_WINDOWS