From 29495d1d1be7a150b00f3d9678da157d643a24c3 Mon Sep 17 00:00:00 2001 From: apaz-cli Date: Fri, 17 Nov 2023 15:12:04 -0600 Subject: [PATCH 1/4] Add check to call result for do_raise() where cause is a type. --- Python/ceval.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Python/ceval.c b/Python/ceval.c index d684c72cc9e302..a4f27124ee4a42 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1916,6 +1916,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)) { From 8d6bcfd7c08bc0a88b65a8b95b374864cef242aa Mon Sep 17 00:00:00 2001 From: apaz-cli Date: Fri, 17 Nov 2023 16:38:18 -0600 Subject: [PATCH 2/4] Added test. --- Lib/test/test_raise.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py index 5936d7535edd5f..6d26a61bee4292 100644 --- a/Lib/test/test_raise.py +++ b/Lib/test/test_raise.py @@ -185,6 +185,20 @@ def test_class_cause(self): 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: From 8c91f04d5b09776c5abc01cd474f787c343093e2 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:55:31 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst 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 00000000000000..1795cc931b62fc --- /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 the ``__cause__`` returned from calling the type T in ``raise from T``. From f1a7c22f13bc2fd77f7fa79af364e55c9a17e4b4 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:51:27 +0000 Subject: [PATCH 4/4] tweak news --- .../2023-11-27-18-55-30.gh-issue-112217.SwFLMj.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 1795cc931b62fc..d4efbab6b2d128 100644 --- 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 @@ -1 +1 @@ -Add check for the type of the ``__cause__`` returned from calling the type T in ``raise from T``. +Add check for the type of ``__cause__`` returned from calling the type ``T`` in ``raise from T``.