]> granicus.if.org Git - python/commitdiff
[2.7] bpo-31285: Don't raise a SystemError in warnings.warn_explicit() in case __load...
authorOren Milman <orenmn@gmail.com>
Sat, 30 Sep 2017 14:06:55 +0000 (17:06 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 30 Sep 2017 14:06:55 +0000 (17:06 +0300)
(cherry picked from commit 91fb0af)

Lib/test/test_warnings.py
Python/_warnings.c

index 0023363832c9e98927535540938696a29d4a5bdd..ae081eeee3f75a857afbdbe9e19ba7366831a258 100644 (file)
@@ -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
index dd168f92593f4707225283c86e1f60f4889ed3f9..37875e56199af3e9d966ddf85fe88f54064ec21e 100644 (file)
@@ -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;