Skip to content

bpo-45615: Add missing test for printing traceback for non-exception. Fix… #30091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,22 @@ 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):
msg = "Exception expected for value, int found"
with self.assertRaisesRegex(TypeError, msg):
traceback.print_exception(42)


cause_message = (
"\nThe above exception was the direct cause "
Expand Down
6 changes: 5 additions & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Functions in the :mod:`traceback` module raise :exc:`TypeError` rather than :exc:`AttributeError` when an exception argument is not of type :exc:`BaseException`.
12 changes: 6 additions & 6 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down