[Python-checkins] cpython (merge 3.2 -> default): merge 3.2 (#11627)

benjamin.peterson python-checkins at python.org
Fri Jul 15 21:07:03 CEST 2011


http://hg.python.org/cpython/rev/bc1fbd6f667a
changeset:   71350:bc1fbd6f667a
parent:      71343:cdb0ae616d58
parent:      71349:8d05f697acd4
user:        Benjamin Peterson <benjamin at python.org>
date:        Fri Jul 15 14:10:35 2011 -0500
summary:
  merge 3.2 (#11627)

files:
  Lib/test/test_raise.py |  9 +++++++++
  Misc/NEWS              |  3 +++
  Python/ceval.c         |  7 +++++++
  3 files changed, 19 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py
--- a/Lib/test/test_raise.py
+++ b/Lib/test/test_raise.py
@@ -121,6 +121,15 @@
         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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -3494,6 +3494,13 @@
         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;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list