From 8e3ca8af2641a47a83f02135a77193b0b80c545e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sun, 23 Jan 2005 09:41:49 +0000 Subject: [PATCH] Flush std{in,out,err} before closing it. Fixes #1074011. Will backport to 2.4 and 2.3. --- Misc/NEWS | 3 +++ Python/sysmodule.c | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 7753df5768..4f41e44a74 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 2.5 alpha 1? Core and builtins ----------------- +- Bug #1074011: closing sys.std{in,out,err} now causes a flush() and + an ferror() call. + - min() and max() now support key= arguments with the same meaning as in list.sort(). diff --git a/Python/sysmodule.c b/Python/sysmodule.c index d246a5998f..3045c46206 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -927,6 +927,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) { @@ -940,9 +947,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 -- 2.40.0