From: Martin v. Löwis Date: Sun, 23 Jan 2005 09:50:32 +0000 (+0000) Subject: Flush std{in,out,err} before closing it. Fixes #1074011. X-Git-Tag: v2.3.5c1~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03bdf1719cfec90837588833c88733a6d5df3d10;p=thirdparty%2FPython%2Fcpython.git Flush std{in,out,err} before closing it. Fixes #1074011. --- diff --git a/Misc/NEWS b/Misc/NEWS index 223c0fe67c7e..ef1b0869609b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 2.3.5? 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 diff --git a/Python/sysmodule.c b/Python/sysmodule.c index c98e9f1856fc..ed1dd9e1c108 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -896,6 +896,13 @@ settrace() -- set the global debug tracing function\n\ ) /* 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) { @@ -909,9 +916,9 @@ _PySys_Init(void) m = Py_InitModule3("sys", sys_methods, sys_doc); sysdict = PyModule_GetDict(m); - sysin = PyFile_FromFile(stdin, "", "r", NULL); - sysout = PyFile_FromFile(stdout, "", "w", NULL); - syserr = PyFile_FromFile(stderr, "", "w", NULL); + sysin = PyFile_FromFile(stdin, "", "r", _check_and_flush); + sysout = PyFile_FromFile(stdout, "", "w", _check_and_flush); + syserr = PyFile_FromFile(stderr, "", "w", _check_and_flush); if (PyErr_Occurred()) return NULL; #ifdef MS_WINDOWS