summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_warnings
diff options
context:
space:
mode:
authorOren Milman <orenmn@gmail.com>2017-09-24 18:27:12 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-09-24 18:27:12 (GMT)
commit91fb0afe181986b48abfc6092dcca912b39de51d (patch)
tree39c1f5061e7e3184cd52f5aab3ca56c37cf14e3d /Lib/test/test_warnings
parent8337239d792bc2098c592def5fc3af84c3b0dfd1 (diff)
downloadcpython-91fb0afe181986b48abfc6092dcca912b39de51d.zip
cpython-91fb0afe181986b48abfc6092dcca912b39de51d.tar.gz
cpython-91fb0afe181986b48abfc6092dcca912b39de51d.tar.bz2
bpo-31285: Fix an assertion failure and a SystemError in warnings.warn_explicit. (#3219)
Diffstat (limited to 'Lib/test/test_warnings')
-rw-r--r--Lib/test/test_warnings/__init__.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 2d0c46f..f27ee6e 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -794,6 +794,42 @@ class _WarningsTests(BaseTest, unittest.TestCase):
self.assertNotIn(b'Warning!', stderr)
self.assertNotIn(b'Error', stderr)
+ def test_issue31285(self):
+ # warn_explicit() should neither raise a SystemError nor cause an
+ # assertion failure, in case the return value of get_source() has a
+ # bad splitlines() method.
+ def get_bad_loader(splitlines_ret_val):
+ class BadLoader:
+ def get_source(self, fullname):
+ class BadSource(str):
+ def splitlines(self):
+ return splitlines_ret_val
+ return BadSource('spam')
+ return BadLoader()
+
+ wmod = self.module
+ with original_warnings.catch_warnings(module=wmod):
+ wmod.filterwarnings('default', category=UserWarning)
+
+ with support.captured_stderr() as stderr:
+ wmod.warn_explicit(
+ 'foo', UserWarning, 'bar', 1,
+ module_globals={'__loader__': get_bad_loader(42),
+ '__name__': 'foobar'})
+ self.assertIn('UserWarning: foo', stderr.getvalue())
+
+ show = wmod._showwarnmsg
+ try:
+ del wmod._showwarnmsg
+ with support.captured_stderr() as stderr:
+ wmod.warn_explicit(
+ 'eggs', UserWarning, 'bar', 1,
+ module_globals={'__loader__': get_bad_loader([42]),
+ '__name__': 'foobar'})
+ self.assertIn('UserWarning: eggs', stderr.getvalue())
+ finally:
+ wmod._showwarnmsg = show
+
@support.cpython_only
def test_issue31411(self):
# warn_explicit() shouldn't raise a SystemError in case