diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index 0023363832c9e9..4e2ca69e21b50d 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -584,6 +584,29 @@ def test_stderr_none(self): 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. + 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 test_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()) + @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 dd168f92593f47..ea8322e398d485 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -644,7 +644,6 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) if (module_globals) { static PyObject *get_source_name = NULL; - static PyObject *splitlines_name = NULL; PyObject *loader; PyObject *module_name; PyObject *source; @@ -657,11 +656,6 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) if (!get_source_name) return NULL; } - if (splitlines_name == NULL) { - splitlines_name = PyString_InternFromString("splitlines"); - if (!splitlines_name) - return NULL; - } /* Check/get the requisite pieces needed for the loader. */ loader = PyDict_GetItemString(module_globals, "__loader__"); @@ -684,8 +678,7 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) } /* Split the source into lines. */ - source_list = PyObject_CallMethodObjArgs(source, splitlines_name, - NULL); + source_list = PyUnicode_Splitlines(source, 0); Py_DECREF(source); if (!source_list) return NULL;