File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -121,6 +121,15 @@ def __init__(self):
121121 else :
122122 self .fail ("No exception raised" )
123123
124+ def test_new_returns_invalid_instance (self ):
125+ # See issue #11627.
126+ class MyException (Exception ):
127+ def __new__ (cls , * args ):
128+ return object ()
129+
130+ with self .assertRaises (TypeError ):
131+ raise MyException
132+
124133
125134class TestCause (unittest .TestCase ):
126135 def test_invalid_cause (self ):
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ What's New in Python 3.2.2?
1010Core and Builtins
1111-----------------
1212
13+ - Issue #11627: Fix segfault when __new__ on a exception returns a non-exception
14+ class.
15+
1316- Issue #12149: Update the method cache after a type's dictionnary gets
1417 cleared by the garbage collector. This fixes a segfault when an instance
1518 and its type get caught in a reference cycle, and the instance's
Original file line number Diff line number Diff line change @@ -3413,6 +3413,13 @@ do_raise(PyObject *exc, PyObject *cause)
34133413 value = PyObject_CallObject (exc , NULL );
34143414 if (value == NULL )
34153415 goto raise_error ;
3416+ if (!PyExceptionInstance_Check (value )) {
3417+ PyErr_Format (PyExc_TypeError ,
3418+ "calling %R should have returned an instance of "
3419+ "BaseException, not %R" ,
3420+ type , Py_TYPE (value ));
3421+ goto raise_error ;
3422+ }
34163423 }
34173424 else if (PyExceptionInstance_Check (exc )) {
34183425 value = exc ;
You can’t perform that action at this time.
0 commit comments