]> granicus.if.org Git - python/commitdiff
Issue #13444: When stdout has been closed explicitly, we should not attempt to flush...
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 26 Nov 2011 21:02:29 +0000 (22:02 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 26 Nov 2011 21:02:29 +0000 (22:02 +0100)
This also adds a test for issue #5319, whose resolution introduced the issue.

1  2 
Lib/test/test_cmd_line.py
Misc/NEWS
Python/pythonrun.c

index 9738691693e9b1d772cae89c9ef6090865c320af,8167e78b9a1715ade79111deec2bb437a5f0eba4..52e715c8be1a5844a452ce4b14f296d07c4830a6
@@@ -266,6 -272,25 +266,25 @@@ class CmdLineTest(unittest.TestCase)
          self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
          self.assertEqual(b'', out)
  
 -                         'Exception IOError: .* ignored')
+     def test_stdout_flush_at_shutdown(self):
+         # Issue #5319: if stdout.flush() fails at shutdown, an error should
+         # be printed out.
+         code = """if 1:
+             import os, sys
+             sys.stdout.write('x')
+             os.close(sys.stdout.fileno())"""
+         rc, out, err = assert_python_ok('-c', code)
+         self.assertEqual(b'', out)
+         self.assertRegex(err.decode('ascii', 'ignore'),
++                         'Exception OSError: .* ignored')
+     def test_closed_stdout(self):
+         # Issue #13444: if stdout has been explicitly closed, we should
+         # not attempt to flush it at shutdown.
+         code = "import sys; sys.stdout.close()"
+         rc, out, err = assert_python_ok('-c', code)
+         self.assertEqual(b'', err)
  
  def test_main():
      test.support.run_unittest(CmdLineTest)
diff --cc Misc/NEWS
index c60cea0bbdfb5d55e538a55988f948de7978c83e,eaadaafb03eabf4fdee848417dbbbb775b41d302..ceb3582eac41160e3e9a1635e1f1e12b8a256c64
+++ b/Misc/NEWS
@@@ -395,10 -83,9 +395,13 @@@ Core and Builtin
  Library
  -------
  
+ - Issue #13444: When stdout has been closed explicitly, we should not attempt
+   to flush it at shutdown and print an error.
 +- Issue #12567: The curses module uses Unicode functions for Unicode arguments
 +  when it is linked to the ncurses library. It encodes also Unicode strings to
 +  the locale encoding instead of UTF-8.
 +
  - Issue #12856: Ensure child processes do not inherit the parent's random
    seed for filename generation in the tempfile module.  Patch by Brian
    Harring.
index 389bcd08ecc7e0e15bbd56d0bba96627bd1c5734,fe92d303c8bc43e6c7f0a6e1b7d8e9d4d271a695..4956943f50e9b4dd5c9f26e8a177b7055c54ef07
@@@ -354,18 -354,17 +370,18 @@@ flush_std_files(void
      PyObject *fout = PySys_GetObject("stdout");
      PyObject *ferr = PySys_GetObject("stderr");
      PyObject *tmp;
 +    _Py_IDENTIFIER(flush);
  
-     if (fout != NULL && fout != Py_None) {
+     if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
 -        tmp = PyObject_CallMethod(fout, "flush", "");
 +        tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
          if (tmp == NULL)
              PyErr_WriteUnraisable(fout);
          else
              Py_DECREF(tmp);
      }
  
-     if (ferr != NULL && ferr != Py_None) {
+     if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
 -        tmp = PyObject_CallMethod(ferr, "flush", "");
 +        tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
          if (tmp == NULL)
              PyErr_Clear();
          else