]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
catch nasty exception classes with __new__ that doesn't return a exception (closes...
authorBenjamin Peterson <benjamin@python.org>
Fri, 15 Jul 2011 19:09:26 +0000 (14:09 -0500)
committerBenjamin Peterson <benjamin@python.org>
Fri, 15 Jul 2011 19:09:26 +0000 (14:09 -0500)
Patch from Andreas Stührk.

Lib/test/test_raise.py
Misc/NEWS
Python/ceval.c

index d120dc1afa900a18bf0ceb285d0a2e37bd7ea452..e02c1af1314d3dc51bc99b9fd24a41c08ca23040 100644 (file)
@@ -121,6 +121,15 @@ class TestRaise(unittest.TestCase):
         else:
             self.fail("No exception raised")
 
+    def test_new_returns_invalid_instance(self):
+        # See issue #11627.
+        class MyException(Exception):
+            def __new__(cls, *args):
+                return object()
+
+        with self.assertRaises(TypeError):
+            raise MyException
+
 
 class TestCause(unittest.TestCase):
     def test_invalid_cause(self):
index 18ee9d30b2f0bcb8e4af2475f92d65c1c551e12d..cba337ac3c8396853fbe3390a8e080eb9571f8f6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.2?
 Core and Builtins
 -----------------
 
+- Issue #11627: Fix segfault when __new__ on a exception returns a non-exception
+  class.
+
 - Issue #12149: Update the method cache after a type's dictionnary gets
   cleared by the garbage collector.  This fixes a segfault when an instance
   and its type get caught in a reference cycle, and the instance's
index 5c3bb832cda50bb473513a453afa603b4fdef656..f0ea7c90dcac82f1fb78be38b440cc54c08fd0d2 100644 (file)
@@ -3413,6 +3413,13 @@ do_raise(PyObject *exc, PyObject *cause)
         value = PyObject_CallObject(exc, NULL);
         if (value == NULL)
             goto raise_error;
+        if (!PyExceptionInstance_Check(value)) {
+            PyErr_Format(PyExc_TypeError,
+                         "calling %R should have returned an instance of "
+                         "BaseException, not %R",
+                         type, Py_TYPE(value));
+            goto raise_error;
+        }
     }
     else if (PyExceptionInstance_Check(exc)) {
         value = exc;