diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-12-10 21:05:33 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-12-10 21:05:33 (GMT) |
commit | e6b42438fa53f7bcadc12bdcfa491b99f280f115 (patch) | |
tree | 5478bf48ff767cee50342d4f963e2620efa5f545 | |
parent | 083f6fba5a281061701da8cfc4cdd22e4ff624c3 (diff) | |
download | cpython-e6b42438fa53f7bcadc12bdcfa491b99f280f115.zip cpython-e6b42438fa53f7bcadc12bdcfa491b99f280f115.tar.gz cpython-e6b42438fa53f7bcadc12bdcfa491b99f280f115.tar.bz2 |
Issue #23016: A warning no longer produces an AttributeError when sys.stderr
is None.
-rw-r--r-- | Lib/test/test_warnings.py | 9 | ||||
-rw-r--r-- | Lib/warnings.py | 3 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 15 insertions, 0 deletions
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 @@ 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 bf9a5d8..fbec94b 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: @@ -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 |