cpython: 726308cfe3b5 (original) (raw)
Mercurial > cpython
changeset 105474:726308cfe3b5 3.6
catch_warnings() calls showwarning() if overriden Issue #28089: Fix a regression introduced in warnings.catch_warnings(): call warnings.showwarning() if it was overriden inside the context manager. [#28089]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Tue, 06 Dec 2016 10:53:52 +0100 |
parents | ab66423c3581 |
children | 2bd1717b6e00 150d36dbe3ba |
files | Lib/test/test_warnings/__init__.py Lib/warnings.py Misc/NEWS |
diffstat | 3 files changed, 60 insertions(+), 2 deletions(-)[+] [-] Lib/test/test_warnings/__init__.py 45 Lib/warnings.py 14 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/test/test_warnings/init.py +++ b/Lib/test/test_warnings/init.py @@ -944,6 +944,51 @@ class CatchWarningTests(BaseTest): self.assertTrue(wmod.filters is not orig_filters) self.assertTrue(wmod.filters is orig_filters)
- def test_record_override_showwarning_before(self):
# Issue #28089: If warnings.showwarning() was overriden, make sure[](#l1.8)
# that catch_warnings(record=True) overrides it again.[](#l1.9)
text = "This is a warning"[](#l1.10)
wmod = self.module[](#l1.11)
my_log = [][](#l1.12)
def my_logger(message, category, filename, lineno, file=None, line=None):[](#l1.14)
nonlocal my_log[](#l1.15)
my_log.append(message)[](#l1.16)
# Override warnings.showwarning() before calling catch_warnings()[](#l1.18)
with support.swap_attr(wmod, 'showwarning', my_logger):[](#l1.19)
with wmod.catch_warnings(module=wmod, record=True) as log:[](#l1.20)
self.assertIsNot(wmod.showwarning, my_logger)[](#l1.21)
wmod.simplefilter("always")[](#l1.23)
wmod.warn(text)[](#l1.24)
self.assertIs(wmod.showwarning, my_logger)[](#l1.26)
self.assertEqual(len(log), 1, log)[](#l1.28)
self.assertEqual(log[0].message.args[0], text)[](#l1.29)
self.assertEqual(my_log, [])[](#l1.30)
- def test_record_override_showwarning_inside(self):
# Issue #28089: It is possible to override warnings.showwarning()[](#l1.33)
# in the catch_warnings(record=True) context manager.[](#l1.34)
text = "This is a warning"[](#l1.35)
wmod = self.module[](#l1.36)
my_log = [][](#l1.37)
def my_logger(message, category, filename, lineno, file=None, line=None):[](#l1.39)
nonlocal my_log[](#l1.40)
my_log.append(message)[](#l1.41)
with wmod.catch_warnings(module=wmod, record=True) as log:[](#l1.43)
wmod.simplefilter("always")[](#l1.44)
wmod.showwarning = my_logger[](#l1.45)
wmod.warn(text)[](#l1.46)
self.assertEqual(len(my_log), 1, my_log)[](#l1.48)
self.assertEqual(my_log[0].args[0], text)[](#l1.49)
self.assertEqual(log, [])[](#l1.50)
+ def test_check_warnings(self): # Explicit tests for the test.support convenience wrapper wmod = self.module
--- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -447,11 +447,20 @@ class catch_warnings(object): self._module._filters_mutated() self._showwarning = self._module.showwarning self._showwarnmsg = self._module._showwarnmsg
self._showwarnmsg_impl = self._module._showwarnmsg_impl[](#l2.7) if self._record:[](#l2.8) log = [][](#l2.9)
def showarnmsg(msg):[](#l2.10)
def showarnmsg_logger(msg):[](#l2.12)
nonlocal log[](#l2.13) log.append(msg)[](#l2.14)
self._module._showwarnmsg = showarnmsg[](#l2.15)
self._module._showwarnmsg_impl = showarnmsg_logger[](#l2.17)
# Reset showwarning() to the default implementation to make sure[](#l2.19)
# that _showwarnmsg() calls _showwarnmsg_impl()[](#l2.20)
self._module.showwarning = self._module._showwarning[](#l2.21)
+ return log else: return None @@ -463,6 +472,7 @@ class catch_warnings(object): self._module._filters_mutated() self._module.showwarning = self._showwarning self._module._showwarnmsg = self._showwarnmsg
self._module._showwarnmsg_impl = self._showwarnmsg_impl[](#l2.30)
filters contains a sequence of filter 5-tuples
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,6 +26,9 @@ Core and Builtins Library ------- +- Issue #28089: Fix a regression introduced in warnings.catch_warnings():