]> granicus.if.org Git - python/commitdiff
Issue #6415: Fixed warnings.warn sagfault on bad formatted string.
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Fri, 17 Jul 2009 06:20:46 +0000 (06:20 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Fri, 17 Jul 2009 06:20:46 +0000 (06:20 +0000)
Lib/test/test_warnings.py
Python/_warnings.c

index ca2751961f072a6cc5d6a1582f8133d3c70e4708..d09399432c5512b12c66eed4273b6b3a6530bb8b 100644 (file)
@@ -327,6 +327,19 @@ class WarnTests(unittest.TestCase):
                             self.module.warn_explicit,
                             None, Warning, None, 1, registry=42)
 
+    def test_bad_str(self):
+        # issue 6415
+        # Warnings instance with a bad format string for __str__ should not
+        # trigger a bus error.
+        class BadStrWarning(Warning):
+            """Warning with a bad format string for __str__."""
+            def __str__(self):
+                return ("A bad formatted string %(err)" %
+                        {"err" : "there is no %(err)s"})
+
+        with self.assertRaises(ValueError):
+            self.module.warn(BadStrWarning())
+
 
 class CWarnTests(BaseTest, WarnTests):
     module = c_warnings
index 219cfc6ee1909d99b557437f1416469f41005420..18fcdac7957ca227148a30f9bd38a464f347fe25 100644 (file)
@@ -317,6 +317,8 @@ warn_explicit(PyObject *category, PyObject *message,
     }
     if (rc == 1) {
         text = PyObject_Str(message);
+        if (text == NULL)
+            goto cleanup;
         category = (PyObject*)message->ob_type;
     }
     else {