Skip to content

Commit 91fb0af

Browse files
orenmnserhiy-storchaka
authored andcommitted
bpo-31285: Fix an assertion failure and a SystemError in warnings.warn_explicit. (#3219)
1 parent 8337239 commit 91fb0af

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,42 @@ def test_stderr_none(self):
794794
self.assertNotIn(b'Warning!', stderr)
795795
self.assertNotIn(b'Error', stderr)
796796

797+
def test_issue31285(self):
798+
# warn_explicit() should neither raise a SystemError nor cause an
799+
# assertion failure, in case the return value of get_source() has a
800+
# bad splitlines() method.
801+
def get_bad_loader(splitlines_ret_val):
802+
class BadLoader:
803+
def get_source(self, fullname):
804+
class BadSource(str):
805+
def splitlines(self):
806+
return splitlines_ret_val
807+
return BadSource('spam')
808+
return BadLoader()
809+
810+
wmod = self.module
811+
with original_warnings.catch_warnings(module=wmod):
812+
wmod.filterwarnings('default', category=UserWarning)
813+
814+
with support.captured_stderr() as stderr:
815+
wmod.warn_explicit(
816+
'foo', UserWarning, 'bar', 1,
817+
module_globals={'__loader__': get_bad_loader(42),
818+
'__name__': 'foobar'})
819+
self.assertIn('UserWarning: foo', stderr.getvalue())
820+
821+
show = wmod._showwarnmsg
822+
try:
823+
del wmod._showwarnmsg
824+
with support.captured_stderr() as stderr:
825+
wmod.warn_explicit(
826+
'eggs', UserWarning, 'bar', 1,
827+
module_globals={'__loader__': get_bad_loader([42]),
828+
'__name__': 'foobar'})
829+
self.assertIn('UserWarning: eggs', stderr.getvalue())
830+
finally:
831+
wmod._showwarnmsg = show
832+
797833
@support.cpython_only
798834
def test_issue31411(self):
799835
# warn_explicit() shouldn't raise a SystemError in case
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an assertion failure in `warnings.warn_explicit`, when the return value
2+
of the received loader's get_source() has a bad splitlines() method. Patch
3+
by Oren Milman.

Python/_warnings.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,7 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
893893
}
894894

895895
/* Split the source into lines. */
896-
source_list = PyObject_CallMethodObjArgs(source,
897-
PyId_splitlines.object,
898-
NULL);
896+
source_list = PyUnicode_Splitlines(source, 0);
899897
Py_DECREF(source);
900898
if (!source_list)
901899
return NULL;

0 commit comments

Comments
 (0)