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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
42bed01
C traceback code
iritkatriel Oct 5, 2021
a2daa23
add ExceptionGroups support to traceback.py
iritkatriel Oct 8, 2021
261917a
remove 'with X sub-exceptions' line from tracebacks
iritkatriel Oct 24, 2021
7613b43
pass margin instead of margin_char
iritkatriel Oct 25, 2021
d98a72b
update news
iritkatriel Oct 25, 2021
d69916e
excs is tuple, use PyTuple apis. Change check to assertion.
iritkatriel Oct 25, 2021
f5cab69
remove redundant num_excs > 0 check (it is asserted above)
iritkatriel Oct 25, 2021
5170f00
remove cpython_only from exception group tests
iritkatriel Oct 25, 2021
2052c77
handle recursion errors (vert deeply nested EGs)
iritkatriel Oct 25, 2021
5097300
WRITE_INDENTED_MARGIN macro --> write_indented_margin function
iritkatriel Oct 26, 2021
dc21cf8
move new traceback utils to internal/
iritkatriel Oct 26, 2021
d4007b7
test improvements
iritkatriel Oct 26, 2021
169934e
pep7, improve error checking and clarity
iritkatriel Oct 26, 2021
aa4da45
add missing test to cover print_chained with/without parent_label
iritkatriel Oct 26, 2021
6ee84f7
compare the complete expected tb text
iritkatriel Oct 26, 2021
ac7f34c
Update Misc/NEWS.d/next/Core and Builtins/2021-09-26-18-18-50.bpo-452…
iritkatriel Oct 26, 2021
d0d4961
don't need the regex anymore
iritkatriel Oct 26, 2021
5c1015d
remove full-path labels
iritkatriel Oct 29, 2021
83abebd
int --> bool
iritkatriel Oct 29, 2021
16d077d
move code around
iritkatriel Oct 29, 2021
64fb164
Tweak the top-level of traceback box as suggested by Yury
iritkatriel Oct 31, 2021
88019f5
tidy up error handling
iritkatriel Nov 1, 2021
e963835
add limits for width and depth of formatted exception groups
iritkatriel Nov 2, 2021
e85510a
use _PyBaseExceptionGroup_Check macro
iritkatriel Nov 2, 2021
c15a7bd
remove redundant PyErr_Clear
iritkatriel Nov 2, 2021
d8cc6e8
minor tweak - move if out of loop
iritkatriel Nov 2, 2021
61fab3f
remove excess whitespace
iritkatriel Nov 3, 2021
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
Prev Previous commit
Next Next commit
remove full-path labels
  • Loading branch information
iritkatriel committed Oct 29, 2021
commit 5c1015d98d2df9a4016fee6e809529d20854e2c5
54 changes: 21 additions & 33 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,9 +1301,9 @@ def exc():
f' | raise EG("eg1", [ValueError(1), TypeError(2)])\n'
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
f' | ExceptionGroup: eg1\n'
f' +-+---------------- cause.1 ----------------\n'
f' +-+---------------- 1 ----------------\n'
f' | ValueError: 1\n'
f' +---------------- cause.2 ----------------\n'
f' +---------------- 2 ----------------\n'
f' | TypeError: 2\n'
f' +------------------------------------\n'
f'\n'
Expand Down Expand Up @@ -1343,9 +1343,9 @@ def exc():
f' | raise EG("eg1", [ValueError(1), TypeError(2)])\n'
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
f' | ExceptionGroup: eg1\n'
f' +-+---------------- context.context.1 ----------------\n'
f' +-+---------------- 1 ----------------\n'
f' | ValueError: 1\n'
f' +---------------- context.context.2 ----------------\n'
f' +---------------- 2 ----------------\n'
f' | TypeError: 2\n'
f' +------------------------------------\n'
f'\n'
Expand All @@ -1356,9 +1356,9 @@ def exc():
f' | raise EG("eg2", [ValueError(3), TypeError(4)])\n'
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
f' | ExceptionGroup: eg2\n'
f' +-+---------------- context.1 ----------------\n'
f' +-+---------------- 1 ----------------\n'
f' | ValueError: 3\n'
f' +---------------- context.2 ----------------\n'
f' +---------------- 2 ----------------\n'
f' | TypeError: 4\n'
f' +------------------------------------\n'
f'\n'
Expand Down Expand Up @@ -1395,20 +1395,20 @@ def exc():
f' | raise EG("eg", [VE(1), exc, VE(4)])\n'
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
f' | ExceptionGroup: eg\n'
f' +-+---------------- context.1 ----------------\n'
f' +-+---------------- 1 ----------------\n'
f' | ValueError: 1\n'
f' +---------------- context.2 ----------------\n'
f' +---------------- 2 ----------------\n'
f' | Traceback (most recent call last):\n'
f' | File "{__file__}", line {exc.__code__.co_firstlineno + 6}, in exc\n'
f' | raise EG("nested", [TE(2), TE(3)])\n'
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
f' | ExceptionGroup: nested\n'
f' +-+---------------- context.2.1 ----------------\n'
f' +-+---------------- 1 ----------------\n'
f' | TypeError: 2\n'
f' +---------------- context.2.2 ----------------\n'
f' +---------------- 2 ----------------\n'
f' | TypeError: 3\n'
f' +------------------------------------\n'
f' +---------------- context.3 ----------------\n'
f' +---------------- 3 ----------------\n'
f' | ValueError: 4\n'
f' +------------------------------------\n'
f'\n'
Expand Down Expand Up @@ -2169,53 +2169,41 @@ def test_exception_group_format(self):

expected = [
f' | Traceback (most recent call last):',
f' | File "{__file__}", line '
f'{lno_g+23}, in _get_exception_group',
f' | File "{__file__}", line {lno_g+23}, in _get_exception_group',
f' | raise ExceptionGroup("eg2", [exc3, exc4])',
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
f' | ExceptionGroup: eg2',
f' +-+---------------- 1 ----------------',
f' | Traceback (most recent call last):',
f' | File "{__file__}", '
f'line {lno_g+16}, in _get_exception_group',
f' | File "{__file__}", line {lno_g+16}, in _get_exception_group',
f' | raise ExceptionGroup("eg1", [exc1, exc2])',
f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
f' | ExceptionGroup: eg1',
f' +-+---------------- 1.1 ----------------',
f' +-+---------------- 1 ----------------',
f' | Traceback (most recent call last):',
f' | File '
f'"{__file__}", line {lno_g+9}, in '
f'_get_exception_group',
f' | File "{__file__}", line {lno_g+9}, in _get_exception_group',
f' | f()',
f' | ^^^',
f' | File '
f'"{__file__}", line {lno_f+1}, in '
f'f',
f' | File "{__file__}", line {lno_f+1}, in f',
f' | 1/0',
f' | ~^~',
f' | ZeroDivisionError: division by zero',
f' +---------------- 1.2 ----------------',
f' +---------------- 2 ----------------',
f' | Traceback (most recent call last):',
f' | File '
f'"{__file__}", line {lno_g+13}, in '
f'_get_exception_group',
f' | File "{__file__}", line {lno_g+13}, in _get_exception_group',
f' | g(42)',
f' | ^^^^^',
f' | File '
f'"{__file__}", line {lno_g+1}, in '
f'g',
f' | File "{__file__}", line {lno_g+1}, in g',
f' | raise ValueError(v)',
f' | ^^^^^^^^^^^^^^^^^^^',
f' | ValueError: 42',
f' +------------------------------------',
f' +---------------- 2 ----------------',
f' | Traceback (most recent call last):',
f' | File "{__file__}", '
f'line {lno_g+20}, in _get_exception_group',
f' | File "{__file__}", line {lno_g+20}, in _get_exception_group',
f' | g(24)',
f' | ^^^^^',
f' | File "{__file__}", '
f'line {lno_g+1}, in g',
f' | File "{__file__}", line {lno_g+1}, in g',
f' | raise ValueError(v)',
f' | ^^^^^^^^^^^^^^^^^^^',
f' | ValueError: 24',
Expand Down
24 changes: 4 additions & 20 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ class _ExceptionPrintContext:
def __init__(self):
self.seen = set()
self.exception_group_depth = 0
self.parent_label = None
self.need_close = False

def indent(self):
Expand Down Expand Up @@ -861,35 +860,26 @@ def format(self, *, chain=True, _ctx=None):

output = []
exc = self
label = _ctx.parent_label
while exc:
Comment thread
gvanrossum marked this conversation as resolved.
Outdated
if chain:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be lifted out of the loop.

if exc.__cause__ is not None:
chained_msg = _cause_message
chained_exc = exc.__cause__
chain_tag = 'cause'
elif (exc.__context__ is not None and
not exc.__suppress_context__):
chained_msg = _context_message
chained_exc = exc.__context__
chain_tag = 'context'
else:
chained_msg = None
chained_exc = None
chain_tag = None

output.append((chained_msg, exc, label))
output.append((chained_msg, exc))
exc = chained_exc
if chain_tag is not None:
if label is None:
label = chain_tag
else:
label = f'{label}.{chain_tag}'
else:
output.append((None, exc, label))
output.append((None, exc))
exc = None

for msg, exc, parent_label in reversed(output):
for msg, exc in reversed(output):
if msg is not None:
yield from _ctx.emit(msg)
if exc.exceptions is None:
Expand All @@ -911,20 +901,14 @@ def format(self, *, chain=True, _ctx=None):
if last_exc:
# The closing frame may be added by a recursive call
_ctx.need_close = True
if parent_label is not None:
label = f'{parent_label}.{i + 1}'
else:
label = f'{i + 1}'
yield (_ctx.get_indent() +
('+-' if i==0 else ' ') +
f'+---------------- {label} ----------------\n')
f'+---------------- {i + 1} ----------------\n')
_ctx.exception_group_depth += 1
_ctx.parent_label = label
try:
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx)
except RecursionError:
pass
_ctx.parent_label = parent_label
if last_exc and _ctx.need_close:
yield (_ctx.get_indent() +
"+------------------------------------\n")
Expand Down
36 changes: 4 additions & 32 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ struct exception_print_context
PyObject *file;
PyObject *seen; // Prevent cycles in recursion
int exception_group_depth; // nesting level of current exception group
PyObject *parent_label; // Unicode label of containing exception group
int need_close; // Need a closing bottom frame
};

Expand Down Expand Up @@ -1083,16 +1082,7 @@ print_chained(struct exception_print_context* ctx, PyObject *value,
const char * message, const char *tag)
{
PyObject *f = ctx->file;
PyObject *parent_label = ctx->parent_label;
PyObject *label = NULL;
int need_close = ctx->need_close;
if (parent_label) {
label = PyUnicode_FromFormat("%U.%s", parent_label, tag);
}
else {
label = PyUnicode_FromString(tag);
}
ctx->parent_label = label;

int err = Py_EnterRecursiveCall(" in print_chained");
if (!err) {
Expand All @@ -1111,9 +1101,7 @@ print_chained(struct exception_print_context* ctx, PyObject *value,
}

ctx->need_close = need_close;
ctx->parent_label = parent_label;

Py_XDECREF(label);
return err;
}

Expand Down Expand Up @@ -1185,7 +1173,6 @@ print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
Py_ssize_t num_excs = PyTuple_GET_SIZE(excs);
assert(num_excs > 0);

PyObject *parent_label = ctx->parent_label;
PyObject *f = ctx->file;

ctx->need_close = 0;
Expand All @@ -1195,20 +1182,10 @@ print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
// The closing frame may be added in a recursive call
ctx->need_close = 1;
}
PyObject *label;
if (parent_label) {
label = PyUnicode_FromFormat("%U.%d",
parent_label, i + 1);
}
else {
label = PyUnicode_FromFormat("%d", i + 1);
}
PyObject *line = NULL;
if (label) {
line = PyUnicode_FromFormat(
"%s+---------------- %U ----------------\n",
(i == 0) ? "+-" : " ", label);
}
PyObject *line = PyUnicode_FromFormat(
"%s+---------------- %zd ----------------\n",
(i == 0) ? "+-" : " ", i + 1);

if (line) {
err |= _Py_WriteIndent(EXC_INDENT(ctx), f);
PyErr_Clear();
Expand All @@ -1221,8 +1198,6 @@ print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
}

ctx->exception_group_depth += 1;
ctx->parent_label = label; /* transfer ref ownership */
label = NULL;
PyObject *exc = PyTuple_GET_ITEM(excs, i);

if (!Py_EnterRecursiveCall(" in print_exception_recursive")) {
Expand All @@ -1233,8 +1208,6 @@ print_exception_recursive(struct exception_print_context* ctx, PyObject *value)
err = -1;
PyErr_Clear();
}
Py_XDECREF(ctx->parent_label);
ctx->parent_label = parent_label;

if (last_exc && ctx->need_close) {
err |= _Py_WriteIndent(EXC_INDENT(ctx), f);
Expand Down Expand Up @@ -1270,7 +1243,6 @@ _PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *t
struct exception_print_context ctx;
ctx.file = file;
ctx.exception_group_depth = 0;
ctx.parent_label = 0;

/* We choose to ignore seen being possibly NULL, and report
at least the main exception (it could be a MemoryError).
Expand Down