[Python-checkins] r74040 - in python/trunk: Lib/test/test_warnings.py Python/_warnings.c

hirokazu.yamamoto python-checkins at python.org
Fri Jul 17 08:20:46 CEST 2009


Author: hirokazu.yamamoto
Date: Fri Jul 17 08:20:46 2009
New Revision: 74040

Log:
Issue #6415: Fixed warnings.warn sagfault on bad formatted string.

Modified:
   python/trunk/Lib/test/test_warnings.py
   python/trunk/Python/_warnings.c

Modified: python/trunk/Lib/test/test_warnings.py
==============================================================================
--- python/trunk/Lib/test/test_warnings.py	(original)
+++ python/trunk/Lib/test/test_warnings.py	Fri Jul 17 08:20:46 2009
@@ -327,6 +327,19 @@
                             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

Modified: python/trunk/Python/_warnings.c
==============================================================================
--- python/trunk/Python/_warnings.c	(original)
+++ python/trunk/Python/_warnings.c	Fri Jul 17 08:20:46 2009
@@ -317,6 +317,8 @@
     }
     if (rc == 1) {
         text = PyObject_Str(message);
+        if (text == NULL)
+            goto cleanup;
         category = (PyObject*)message->ob_type;
     }
     else {


More information about the Python-checkins mailing list