summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_warnings.py14
-rw-r--r--Misc/NEWS2
-rw-r--r--Python/_warnings.c2
3 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index 4bcc210..292c018 100644
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -338,6 +338,20 @@ 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/Misc/NEWS b/Misc/NEWS
index 0009115..2f9c12a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,8 @@ C-API
Library
-------
+- Issue #6415: Fixed warnings.warn sagfault on bad formatted string.
+
- Issue #6358: The exit status of a command started with os.popen() was
reported differently than it did with python 2.x.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index da52e35..ef7f373 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -320,6 +320,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 {