From e6b42438fa53f7bcadc12bdcfa491b99f280f115 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 10 Dec 2014 23:05:33 +0200 Subject: [PATCH] Issue #23016: A warning no longer produces an AttributeError when sys.stderr is None. --- Lib/test/test_warnings.py | 9 +++++++++ Lib/warnings.py | 3 +++ Misc/NEWS | 3 +++ 3 files changed, 15 insertions(+) diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index fb2046e99c..7a9459a47e 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -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): diff --git a/Lib/warnings.py b/Lib/warnings.py index bf9a5d86e1..fbec94b7f0 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -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: diff --git a/Misc/NEWS b/Misc/NEWS index f8f9339e4e..6d0f3eb7a7 100644 --- 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 -- 2.50.1