Issue #23016: A warning no longer produces an AttributeError when sys.stderr
is None.
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index fb2046e..7a9459a 100644
--- 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
index bf9a5d8..fbec94b 100644
--- 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
index f8f9339..6d0f3eb 100644
--- 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