From 40d736bcf40c10aa5fc7d6cc305a6641afd3cd0e Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sat, 30 Sep 2017 17:06:55 +0300 Subject: [PATCH] [2.7] bpo-31285: Don't raise a SystemError in warnings.warn_explicit() in case __loader__.get_source() has a bad splitlines() method. (GH-3219) (#3823) (cherry picked from commit 91fb0af) --- Lib/test/test_warnings.py | 21 +++++++++++++++++++++ Python/_warnings.c | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index 0023363832..ae081eeee3 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -584,6 +584,27 @@ class _WarningsTests(BaseTest): self.assertNotIn(b'Warning!', stderr) self.assertNotIn(b'Error', stderr) + def test_issue31285(self): + # warn_explicit() shouldn't raise a SystemError in case the return + # value of get_source() has a bad splitlines() method. + class BadLoader: + def get_source(self, fullname): + class BadSource(str): + def splitlines(self): + return 42 + return BadSource('spam') + + wmod = self.module + with original_warnings.catch_warnings(module=wmod): + wmod.filterwarnings('default', category=UserWarning) + + with test_support.captured_stderr() as stderr: + wmod.warn_explicit( + 'foo', UserWarning, 'bar', 1, + module_globals={'__loader__': BadLoader(), + '__name__': 'foobar'}) + self.assertIn('UserWarning: foo', stderr.getvalue()) + @test_support.cpython_only def test_issue31411(self): # warn_explicit() shouldn't raise a SystemError in case diff --git a/Python/_warnings.c b/Python/_warnings.c index dd168f9259..37875e5619 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -684,8 +684,9 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) } /* Split the source into lines. */ - source_list = PyObject_CallMethodObjArgs(source, splitlines_name, - NULL); + source_list = PyObject_CallMethodObjArgs((PyObject *)&PyString_Type, + splitlines_name, source, + NULL); Py_DECREF(source); if (!source_list) return NULL; -- 2.50.1