Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5afa03a

Browse files
committed
catch nasty exception classes with __new__ that doesn't return a exception (closes #11627)
Patch from Andreas Stührk.
1 parent 1f0ccfa commit 5afa03a

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/test/test_raise.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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

125134
class TestCause(unittest.TestCase):
126135
def test_invalid_cause(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.2?
1010
Core 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

Python/ceval.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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;

0 commit comments

Comments
 (0)