[Python-checkins] cpython (2.7): Issue #23016: A warning no longer produces an AttributeError when sys.stderr

serhiy.storchaka python-checkins at python.org
Wed Dec 10 22:12:55 CET 2014


https://hg.python.org/cpython/rev/aeeec8a4b9b8
changeset:   93819:aeeec8a4b9b8
branch:      2.7
parent:      93816:e0129395724d
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Dec 10 23:05:33 2014 +0200
summary:
  Issue #23016: A warning no longer produces an AttributeError when sys.stderr
is None.

files:
  Lib/test/test_warnings.py |  9 +++++++++
  Lib/warnings.py           |  3 +++
  Misc/NEWS                 |  3 +++
  3 files changed, 15 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -560,6 +560,15 @@
         finally:
             globals_dict['__file__'] = oldfile
 
+    def test_stderr_none(self):
+        rc, stdout, stderr = assert_python_ok("-c",
+            "import sys; sys.stderr = None; "
+            "import warnings; warnings.simplefilter('always'); "
+            "warnings.warn('Warning!')")
+        self.assertEqual(stdout, b'')
+        self.assertNotIn(b'Warning!', stderr)
+        self.assertNotIn(b'Error', stderr)
+
 
 class WarningsDisplayTests(unittest.TestCase):
 
diff --git a/Lib/warnings.py b/Lib/warnings.py
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -26,6 +26,9 @@
     """Hook to write a warning to a file; replace if you like."""
     if file is None:
         file = sys.stderr
+        if file is None:
+            # sys.stderr is None - warnings get lost
+            return
     try:
         file.write(formatwarning(message, category, filename, lineno, line))
     except IOError:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@
 Library
 -------
 
+- Issue #23016: A warning no longer produces an AttributeError when sys.stderr
+  is None.
+
 - Issue #14099: ZipFile.open() no longer reopen the underlying file.  Objects
   returned by ZipFile.open() can now operate independently of the ZipFile even
   if the ZipFile was created by passing in a file-like object as the first

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list