]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23016: A warning no longer produces AttributeError when the program
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 10 Dec 2014 20:59:55 +0000 (22:59 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 10 Dec 2014 20:59:55 +0000 (22:59 +0200)
is run with pythonw.exe.

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

index 0f2e089408ccab474426757bb633d556b95885f4..fb01b83e0c86c948985da5b5706b7cecc86d0efd 100644 (file)
@@ -631,6 +631,15 @@ class _WarningsTests(BaseTest, unittest.TestCase):
         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(BaseTest):
 
index 5d5c9a9dcbc518acc90f10d9bfdea54d3f3480cb..3a8fc5db6516570d2c0e0ad37d5d64c29b917200 100644 (file)
@@ -11,6 +11,9 @@ def showwarning(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 when ran with pythonw.exe - warnings get lost
+            return
     try:
         file.write(formatwarning(message, category, filename, lineno, line))
     except OSError:
index 4807dc4065e1622f67253f2139837e0eaa00cc61..d4cd62eb23c2d78782db15cfda63998c840a8d89 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23016: A warning no longer produces an AttributeError when the program
+  is run with pythonw.exe.
+
 - Issue #21775: shutil.copytree(): fix crash when copying to VFAT. An exception
   handler assumed that that OSError objects always have a 'winerror' attribute.
   That is not the case, so the exception handler itself raised AttributeError