summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_raise.py14
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst1
-rw-r--r--Python/ceval.c7
3 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py
index 5936d75..6d26a61 100644
--- a/Lib/test/test_raise.py
+++ b/Lib/test/test_raise.py
@@ -185,6 +185,20 @@ class TestCause(unittest.TestCase):
else:
self.fail("No exception raised")
+ def test_class_cause_nonexception_result(self):
+ class ConstructsNone(BaseException):
+ @classmethod
+ def __new__(*args, **kwargs):
+ return None
+ try:
+ raise IndexError from ConstructsNone
+ except TypeError as e:
+ self.assertIn("should have returned an instance of BaseException", str(e))
+ except IndexError:
+ self.fail("Wrong kind of exception raised")
+ else:
+ self.fail("No exception raised")
+
def test_instance_cause(self):
cause = KeyError()
try:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst
new file mode 100644
index 0000000..d4efbab
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst
@@ -0,0 +1 @@
+Add check for the type of ``__cause__`` returned from calling the type ``T`` in ``raise from T``.
diff --git a/Python/ceval.c b/Python/ceval.c
index 76ab5df..def75fd 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1920,6 +1920,13 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
fixed_cause = _PyObject_CallNoArgs(cause);
if (fixed_cause == NULL)
goto raise_error;
+ if (!PyExceptionInstance_Check(fixed_cause)) {
+ _PyErr_Format(tstate, PyExc_TypeError,
+ "calling %R should have returned an instance of "
+ "BaseException, not %R",
+ cause, Py_TYPE(fixed_cause));
+ goto raise_error;
+ }
Py_DECREF(cause);
}
else if (PyExceptionInstance_Check(cause)) {