Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e3ca8a

Browse files
committed
Flush std{in,out,err} before closing it. Fixes #1074011.
Will backport to 2.4 and 2.3.
1 parent c300175 commit 8e3ca8a

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 2.5 alpha 1?
1010
Core and builtins
1111
-----------------
1212

13+
- Bug #1074011: closing sys.std{in,out,err} now causes a flush() and
14+
an ferror() call.
15+
1316
- min() and max() now support key= arguments with the same meaning as in
1417
list.sort().
1518

Python/sysmodule.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,13 @@ settrace() -- set the global debug tracing function\n\
927927
)
928928
/* end of sys_doc */ ;
929929

930+
static int
931+
_check_and_flush (FILE *stream)
932+
{
933+
int prev_fail = ferror (stream);
934+
return fflush (stream) || prev_fail ? EOF : 0;
935+
}
936+
930937
PyObject *
931938
_PySys_Init(void)
932939
{
@@ -940,9 +947,9 @@ _PySys_Init(void)
940947
m = Py_InitModule3("sys", sys_methods, sys_doc);
941948
sysdict = PyModule_GetDict(m);
942949

943-
sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
944-
sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
945-
syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
950+
sysin = PyFile_FromFile(stdin, "<stdin>", "r", _check_and_flush);
951+
sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
952+
syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
946953
if (PyErr_Occurred())
947954
return NULL;
948955
#ifdef MS_WINDOWS

0 commit comments

Comments
 (0)