diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 56aea6d..cbe3233 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -874,6 +874,8 @@ class InteractiveShell(SingletonConfigurable): def init_events(self): self.events = EventManager(self, available_events) + self.events.register("pre_execute", self._clear_warning_registry) + def register_post_execute(self, func): """DEPRECATED: Use ip.events.register('post_run_cell', func) @@ -883,6 +885,13 @@ class InteractiveShell(SingletonConfigurable): "ip.events.register('post_run_cell', func) instead.") self.events.register('post_run_cell', func) + def _clear_warning_registry(self): + # clear the warning registry, so that different code blocks with + # overlapping line number ranges don't cause spurious suppression of + # warnings (see gh-6611 for details) + if "__warningregistry__" in self.user_global_ns: + del self.user_global_ns["__warningregistry__"] + #------------------------------------------------------------------------- # Things related to the "main" module #------------------------------------------------------------------------- diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index b9e76c2..1e8bfae 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -843,3 +843,17 @@ class TestSyntaxErrorTransformer(unittest.TestCase): +def test_warning_suppression(): + ip.run_cell("import warnings") + try: + with tt.AssertPrints("UserWarning: asdf", channel="stderr"): + ip.run_cell("warnings.warn('asdf')") + # Here's the real test -- if we run that again, we should get the + # warning again. Traditionally, each warning was only issued once per + # IPython session (approximately), even if the user typed in new and + # different code that should have also triggered the warning, leading + # to much confusion. + with tt.AssertPrints("UserWarning: asdf", channel="stderr"): + ip.run_cell("warnings.warn('asdf')") + finally: + ip.run_cell("del warnings")