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
Next Next commit
C traceback code
  • Loading branch information
iritkatriel committed Oct 25, 2021
commit 42bed01c31e03432873955bd66303054a2fa2483
2 changes: 1 addition & 1 deletion Include/cpython/traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ typedef struct _traceback {
int tb_lineno;
} PyTracebackObject;

PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int, int *, PyObject **);
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int, int, char, int *, PyObject **);
PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
4 changes: 4 additions & 0 deletions Include/traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ extern "C" {
PyAPI_FUNC(int) PyTraceBack_Here(PyFrameObject *);
PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);

int PyTraceBack_Print_Indented(PyObject *, int, char, PyObject *);
int _Py_WriteIndentedMargin(int, char, PyObject *);
int _Py_WriteIndent(int, PyObject *);

/* Reveal traceback type so we can typecheck traceback objects */
PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
#define PyTraceBack_Check(v) Py_IS_TYPE(v, &PyTraceBack_Type)
Expand Down
172 changes: 170 additions & 2 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,12 @@ def __eq__(self, other):
"\nDuring handling of the above exception, "
"another exception occurred:\n\n")

boundaries = re.compile(
'(%s|%s)' % (re.escape(cause_message), re.escape(context_message)))
nested_exception_header_re = r'[\+-]?\+[-{4} ]+[ \d\.?]+[ -{4}]+'
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
Comment thread
iritkatriel marked this conversation as resolved.
Outdated

boundaries = re.compile(
'(%s|%s|%s)' % (re.escape(cause_message),
re.escape(context_message),
nested_exception_header_re))

class BaseExceptionReportingTests:

Expand Down Expand Up @@ -1263,6 +1266,171 @@ def get_report(self, e):
exception_print(e)
return s.getvalue()

# TODO: once traceback.py supports this format, move the
# following tests to the superclass

def check_exception_group(self, exc_func, expected):
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
report = self.get_report(exc_func)
blocks = boundaries.split(report)
self.assertEqual(len(blocks), len(expected))

for i, block in enumerate(blocks):
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
for line in expected[i]:
self.assertIn(f"{line}", block)
# check indentation
self.assertNotIn(f" {line}", block)

for line in report:
# at most one margin char per line
self.assertLessEqual(line.count('|'), 1)
Comment thread
iritkatriel marked this conversation as resolved.
Outdated

@cpython_only
def test_exception_group_basic(self):
def exc():
raise ExceptionGroup("eg", [ValueError(1), TypeError(2)])

expected = [
[' | raise ExceptionGroup("eg", [ValueError(1), TypeError(2)])',
' | ExceptionGroup: eg',
' | with 2 sub-exceptions:'
],
['-+---------------- 1 ----------------'],
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
[' | ValueError: 1'],
['+---------------- 2 ----------------'],
[' | TypeError: 2'],
]

self.check_exception_group(exc, expected)

@cpython_only
def test_exception_group_context(self):
def exc():
try:
raise ExceptionGroup("eg1", [ValueError(1), TypeError(2)])
except:
raise ExceptionGroup("eg2", [ValueError(3), TypeError(4)])

expected = [
[' | raise ExceptionGroup("eg1", [ValueError(1), TypeError(2)])',
' | ExceptionGroup: eg1',
' | with 2 sub-exceptions:'
],
['-+---------------- 1 ----------------'],
[' | ValueError: 1'],
['+---------------- 2 ----------------'],
[' | TypeError: 2'],
[ context_message ],
[' | raise ExceptionGroup("eg2", [ValueError(3), TypeError(4)])',
' | ExceptionGroup: eg2',
' | with 2 sub-exceptions:'
],
['-+---------------- 1 ----------------'],
[' | ValueError: 3'],
['+---------------- 2 ----------------'],
[' | TypeError: 4'],
]
self.check_exception_group(exc, expected)

@cpython_only
def test_exception_group_context(self):
def exc():
EG = ExceptionGroup
try:
raise EG("eg1", [ValueError(1), TypeError(2)])
except:
raise EG("eg2", [ValueError(3), TypeError(4)])

expected = [
[' | raise EG("eg1", [ValueError(1), TypeError(2)])',
' | ExceptionGroup: eg1',
' | with 2 sub-exceptions:'
],
['-+---------------- context.1 ----------------'],
[' | ValueError: 1'],
['+---------------- context.2 ----------------'],
[' | TypeError: 2'],
[ context_message ],
[' | raise EG("eg2", [ValueError(3), TypeError(4)])',
' | ExceptionGroup: eg2',
' | with 2 sub-exceptions:'
],
['-+---------------- 1 ----------------'],
[' | ValueError: 3'],
['+---------------- 2 ----------------'],
[' | TypeError: 4'],
]
self.check_exception_group(exc, expected)

def test_exception_group_cause(self):
Comment thread
iritkatriel marked this conversation as resolved.
def exc():
EG = ExceptionGroup
try:
raise EG("eg1", [ValueError(1), TypeError(2)])
except Exception as e:
raise EG("eg2", [ValueError(3), TypeError(4)]) from e

expected = [
[' | raise EG("eg1", [ValueError(1), TypeError(2)])',
' | ExceptionGroup: eg1',
' | with 2 sub-exceptions:'
],
['-+---------------- cause.1 ----------------'],
[' | ValueError: 1'],
['+---------------- cause.2 ----------------'],
[' | TypeError: 2'],
[ cause_message ],
[' | raise EG("eg2", [ValueError(3), TypeError(4)])',
' | ExceptionGroup: eg2',
' | with 2 sub-exceptions:'
],
['-+---------------- 1 ----------------'],
[' | ValueError: 3'],
['+---------------- 2 ----------------'],
[' | TypeError: 4'],
]
self.check_exception_group(exc, expected)

@cpython_only
def test_exception_group_nested(self):
def exc():
EG = ExceptionGroup
VE = ValueError
TE = TypeError
try:
try:
raise EG("nested", [TE(2), TE(3)])
except Exception as e:
exc = e
raise EG("eg", [VE(1), exc, VE(4)])
except:
raise EG("top", [VE(5)])

expected = [
[' | raise EG("eg", [VE(1), exc, VE(4)])',
' | ExceptionGroup: eg',
' | with 3 sub-exceptions:'
],
['-+---------------- context.1 ----------------'],
[' | ValueError: 1'],
['+---------------- context.2 ----------------'],
[' | ExceptionGroup: nested',
' | with 2 sub-exceptions'
],
['-+---------------- context.2.1 ----------------'],
[' | TypeError: 2'],
['+---------------- context.2.2 ----------------'],
[' | TypeError: 3'],
['+---------------- context.3 ----------------'],
[' | ValueError: 4'],
[ context_message ],
[' | raise EG("top", [VE(5)])',
' | ExceptionGroup: top'
],
['-+---------------- 1 ----------------'],
[' | ValueError: 5']
]
self.check_exception_group(exc, expected)


class LimitTests(unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text,
PyFile_WriteString("\n", f_stderr);
}
else {
_Py_DisplaySourceLine(f_stderr, filename, lineno, 2, NULL, NULL);
_Py_DisplaySourceLine(f_stderr, filename, lineno, 2, 0, '\0', NULL, NULL);
}

error:
Expand Down
Loading