From: Hirokazu Yamamoto Date: Fri, 17 Jul 2009 06:20:46 +0000 (+0000) Subject: Issue #6415: Fixed warnings.warn sagfault on bad formatted string. X-Git-Tag: v2.7a1~755 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e78e5d2e515167df8fdf0a551e8333c31c6a1ba3;p=python Issue #6415: Fixed warnings.warn sagfault on bad formatted string. --- diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index ca2751961f..d09399432c 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -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 diff --git a/Python/_warnings.c b/Python/_warnings.c index 219cfc6ee1..18fcdac795 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -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 {