Skip to content

Commit 40d736b

Browse files
orenmnserhiy-storchaka
authored andcommitted
[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)
1 parent 8b83687 commit 40d736b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Lib/test/test_warnings.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,27 @@ def test_stderr_none(self):
584584
self.assertNotIn(b'Warning!', stderr)
585585
self.assertNotIn(b'Error', stderr)
586586

587+
def test_issue31285(self):
588+
# warn_explicit() shouldn't raise a SystemError in case the return
589+
# value of get_source() has a bad splitlines() method.
590+
class BadLoader:
591+
def get_source(self, fullname):
592+
class BadSource(str):
593+
def splitlines(self):
594+
return 42
595+
return BadSource('spam')
596+
597+
wmod = self.module
598+
with original_warnings.catch_warnings(module=wmod):
599+
wmod.filterwarnings('default', category=UserWarning)
600+
601+
with test_support.captured_stderr() as stderr:
602+
wmod.warn_explicit(
603+
'foo', UserWarning, 'bar', 1,
604+
module_globals={'__loader__': BadLoader(),
605+
'__name__': 'foobar'})
606+
self.assertIn('UserWarning: foo', stderr.getvalue())
607+
587608
@test_support.cpython_only
588609
def test_issue31411(self):
589610
# warn_explicit() shouldn't raise a SystemError in case

Python/_warnings.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
684684
}
685685

686686
/* Split the source into lines. */
687-
source_list = PyObject_CallMethodObjArgs(source, splitlines_name,
688-
NULL);
687+
source_list = PyObject_CallMethodObjArgs((PyObject *)&PyString_Type,
688+
splitlines_name, source,
689+
NULL);
689690
Py_DECREF(source);
690691
if (!source_list)
691692
return NULL;

0 commit comments

Comments
 (0)