From 46458c893a224c6bae9cbf58284ad85ff94cb7a1 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 13 Dec 2021 15:35:12 +0000 Subject: [PATCH 1/4] 45615: Add missing test for printing traceback for non-exception. Fix traceback.py to raise TypeError instead of AttributeError --- Lib/test/test_traceback.py | 15 +++++++++++++++ Lib/traceback.py | 6 +++++- Modules/_testcapimodule.c | 12 ++++++------ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 97bd9bae1d58e4..3a0514aa29d72a 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1060,6 +1060,21 @@ def test_exception_group_deep_recursion_traceback(self): self.assertIn('ExceptionGroup', output) self.assertLessEqual(output.count('ExceptionGroup'), LIMIT) + @cpython_only + def test_print_exception_bad_type_capi(self): + from _testcapi import exception_print + with captured_output("stderr") as stderr: + exception_print(42) + self.assertEqual( + stderr.getvalue(), + ('TypeError: print_exception(): ' + 'Exception expected for value, int found\n') + ) + + def test_print_exception_bad_type_python(self): + with self.assertRaises(TypeError): + traceback.print_exception(42) + cause_message = ( "\nThe above exception was the direct cause " diff --git a/Lib/traceback.py b/Lib/traceback.py index b244750fd016ea..05f1fffef0d3b0 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -98,7 +98,11 @@ def _parse_value_tb(exc, value, tb): raise ValueError("Both or neither of value and tb must be given") if value is tb is _sentinel: if exc is not None: - return exc, exc.__traceback__ + if isinstance(exc, BaseException): + return exc, exc.__traceback__ + + raise TypeError(f'Exception expected for value, ' + f'{type(exc).__name__} found') else: return None, None return value, tb diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 6116365b2c0f71..be40d68b40b17c 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3513,17 +3513,17 @@ static PyObject * exception_print(PyObject *self, PyObject *args) { PyObject *value; - PyObject *tb; + PyObject *tb = NULL; if (!PyArg_ParseTuple(args, "O:exception_print", - &value)) - return NULL; - if (!PyExceptionInstance_Check(value)) { - PyErr_Format(PyExc_TypeError, "an exception instance is required"); + &value)) { return NULL; } - tb = PyException_GetTraceback(value); + if (PyExceptionInstance_Check(value)) { + tb = PyException_GetTraceback(value); + } + PyErr_Display((PyObject *) Py_TYPE(value), value, tb); Py_XDECREF(tb); From 7d45bef9a7883acc8bc65ccebf6c9cd67d2cd2f8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 13 Dec 2021 15:51:17 +0000 Subject: [PATCH 2/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 --- .../NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst diff --git a/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst b/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst new file mode 100644 index 00000000000000..65b5b3c009bc5f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst @@ -0,0 +1 @@ +Functions in the :mod:`traceback` module raise :exc:`TypeError` rather than :exc:`AttributeError` when an exception arg is not of type :exc:`BaseException`. \ No newline at end of file From 50b66435060569c867e1d6ab60fe66ee0e7f43bd Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Sat, 1 Jan 2022 23:58:11 +0000 Subject: [PATCH 3/4] check exception message in test --- Lib/test/test_traceback.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 3a0514aa29d72a..a0e4656d3d9eaa 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1072,7 +1072,8 @@ def test_print_exception_bad_type_capi(self): ) def test_print_exception_bad_type_python(self): - with self.assertRaises(TypeError): + msg = "Exception expected for value, int found" + with self.assertRaisesRegex(TypeError, msg): traceback.print_exception(42) From 6036b54ee3e2da5fcbd97c0cce16b97be501f9ed Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Sat, 1 Jan 2022 23:58:45 +0000 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst Co-authored-by: Erlend Egeberg Aasland --- .../next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst b/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst index 65b5b3c009bc5f..f8cd911ea63651 100644 --- a/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst +++ b/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst @@ -1 +1 @@ -Functions in the :mod:`traceback` module raise :exc:`TypeError` rather than :exc:`AttributeError` when an exception arg is not of type :exc:`BaseException`. \ No newline at end of file +Functions in the :mod:`traceback` module raise :exc:`TypeError` rather than :exc:`AttributeError` when an exception argument is not of type :exc:`BaseException`. \ No newline at end of file