Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,27 @@ 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.
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
Expand Down
5 changes: 3 additions & 2 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down