diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-27 04:38:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-27 04:38:03 (GMT) |
commit | 90fe25a051bd8cf64d52be533c9b60cad9bdd7fb (patch) | |
tree | 48e970eec5fd39ccd8f26e576fdeb3a1a0fae3f9 | |
parent | bdb215b18a42360b6a9c82876fa71f19ca1a416d (diff) | |
download | cpython-90fe25a051bd8cf64d52be533c9b60cad9bdd7fb.zip cpython-90fe25a051bd8cf64d52be533c9b60cad9bdd7fb.tar.gz cpython-90fe25a051bd8cf64d52be533c9b60cad9bdd7fb.tar.bz2 |
[3.6] bpo-31285: Fix an assertion failure and a SystemError in warnings.warn_explicit. (GH-3219) (#3775)
(cherry picked from commit 91fb0afe181986b48abfc6092dcca912b39de51d)
-rw-r--r-- | Lib/test/test_warnings/__init__.py | 36 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2017-08-27-21-18-30.bpo-31285.7lzaKV.rst | 3 | ||||
-rw-r--r-- | Python/_warnings.c | 4 |
3 files changed, 40 insertions, 3 deletions
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index a07886c..ea81d11 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 diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-08-27-21-18-30.bpo-31285.7lzaKV.rst b/Misc/NEWS.d/next/Core and Builtins/2017-08-27-21-18-30.bpo-31285.7lzaKV.rst new file mode 100644 index 0000000..61f2c4e --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-08-27-21-18-30.bpo-31285.7lzaKV.rst @@ -0,0 +1,3 @@ +Fix an assertion failure in `warnings.warn_explicit`, when the return value +of the received loader's get_source() has a bad splitlines() method. Patch +by Oren Milman. diff --git a/Python/_warnings.c b/Python/_warnings.c index b074788..ef6ed00 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -900,9 +900,7 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) } /* Split the source into lines. */ - source_list = PyObject_CallMethodObjArgs(source, - PyId_splitlines.object, - NULL); + source_list = PyUnicode_Splitlines(source, 0); Py_DECREF(source); if (!source_list) return NULL; |