]> granicus.if.org Git - python/commitdiff
Issue #23016: A warning no longer produces an AttributeError when sys.stderr
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 10 Dec 2014 21:05:33 +0000 (23:05 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 10 Dec 2014 21:05:33 +0000 (23:05 +0200)
is None.

Lib/test/test_warnings.py
Lib/warnings.py
Misc/NEWS

index fb2046e99c44f9de0d140ad562f01d7d7ced316e..7a9459a47e2b39f6276ac7720dc29888c28b1904 100644 (file)
@@ -560,6 +560,15 @@ class _WarningsTests(BaseTest):
         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):
 
index bf9a5d86e14b4038795ab33d6ec6349078f4a22e..fbec94b7f07aeab475367270b2bca71de343c0e3 100644 (file)
@@ -26,6 +26,9 @@ def _show_warning(message, category, filename, lineno, file=None, line=None):
     """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:
index f8f9339e4ef1cd2a698e70552f9abd6bfa5d504a..6d0f3eb7a709167287648e09ecd77eb2b475946e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 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