[Python-checkins] cpython (3.4): Issue #23016: A warning no longer produces AttributeError when the program

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


https://hg.python.org/cpython/rev/d04dab84388f
changeset:   93817:d04dab84388f
branch:      3.4
parent:      93814:957117d642f3
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Dec 10 22:59:55 2014 +0200
summary:
  Issue #23016: A warning no longer produces AttributeError when the program
is run with pythonw.exe.

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
@@ -631,6 +631,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(BaseTest):
 
diff --git a/Lib/warnings.py b/Lib/warnings.py
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -11,6 +11,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 when ran with pythonw.exe - warnings get lost
+            return
     try:
         file.write(formatwarning(message, category, filename, lineno, line))
     except OSError:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@
 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

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


More information about the Python-checkins mailing list