Commits

Benjamin Peterson committed 8d05f69

catch nasty exception classes with __new__ that doesn't return a exception (closes #11627)

Patch from Andreas Stührk.

  • Participants
  • Parent commits ed90c1c
  • Branches 3.2

Comments (0)

Files changed (3)

File Lib/test/test_raise.py

         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):
 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

File Python/ceval.c

         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;