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
pass margin instead of margin_char
  • Loading branch information
iritkatriel committed Oct 25, 2021
commit 7613b43c3d6621c4d3491d172d8e52d7ada8589a
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, char, int *, PyObject **);
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int, int *, PyObject **);
PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
4 changes: 2 additions & 2 deletions Include/traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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 PyTraceBack_Print_Indented(PyObject *, int, const char*, PyObject *);
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
int _Py_WriteIndentedMargin(int, const char*, PyObject *);
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
int _Py_WriteIndent(int, PyObject *);

/* Reveal traceback type so we can typecheck traceback objects */
Expand Down
12 changes: 5 additions & 7 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,19 +612,17 @@ def __init__(self):
def indent(self):
return 2 * self.exception_group_depth

def margin_char(self):
return '|' if self.exception_group_depth else None
def margin(self):
return '| ' if self.exception_group_depth else ''

def get_indent(self):
return ' ' * self.indent()

def get_fancy_indent(self):
margin_char = self.margin_char()
margin = (margin_char + ' ') if margin_char is not None else ''
return self.get_indent() + margin
def get_indented_margin(self):
return self.get_indent() + self.margin()

def emit(self, text_gen):
indent_str = self.get_fancy_indent()
indent_str = self.get_indented_margin()
if isinstance(text_gen, str):
yield textwrap.indent(text_gen, indent_str, lambda line: True)
else:
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, 0, '\0', NULL, NULL);
_Py_DisplaySourceLine(f_stderr, filename, lineno, 2, NULL, NULL);
}

error:
Expand Down
6 changes: 3 additions & 3 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,10 @@ struct exception_print_context
int need_close; // Need a closing bottom frame
};

#define EXC_MARGIN_CHAR(ctx) ((ctx)->exception_group_depth ? '|' : '\0')
#define EXC_MARGIN(ctx) ((ctx)->exception_group_depth ? "| " : "")
#define EXC_INDENT(ctx) (2 * (ctx)->exception_group_depth)

#define WRITE_INDENTED_MARGIN(ctx, f) _Py_WriteIndentedMargin(EXC_INDENT(ctx), EXC_MARGIN_CHAR(ctx), (f))
#define WRITE_INDENTED_MARGIN(ctx, f) _Py_WriteIndentedMargin(EXC_INDENT(ctx), EXC_MARGIN(ctx), (f))
Comment thread
iritkatriel marked this conversation as resolved.
Outdated

static void
print_exception(struct exception_print_context *ctx, PyObject *value)
Expand All @@ -924,7 +924,7 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
type = (PyObject *) Py_TYPE(value);
tb = PyException_GetTraceback(value);
if (tb && tb != Py_None)
err = PyTraceBack_Print_Indented(tb, EXC_INDENT(ctx), EXC_MARGIN_CHAR(ctx), f);
err = PyTraceBack_Print_Indented(tb, EXC_INDENT(ctx), EXC_MARGIN(ctx), f);
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
if (err == 0 &&
(err = _PyObject_LookupAttrId(value, &PyId_print_file_and_line, &tmp)) > 0)
{
Expand Down
51 changes: 31 additions & 20 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,21 @@ _Py_WriteIndent(int indent, PyObject *f) {
return 0;
}

/* Writes indent spaces, followed by the margin if margin_char is not `\0`.
/* Writes indent spaces, followed by the margin if it is not `\0`.
*/
int
_Py_WriteIndentedMargin(int indent, char margin_char, PyObject *f) {
_Py_WriteIndentedMargin(int indent, const char *margin, PyObject *f) {
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
int err = 0;
char margin[] = {margin_char, ' ', '\0' };
err |= _Py_WriteIndent(indent, f);
err |= PyFile_WriteString(margin, f);
if (margin) {
err |= PyFile_WriteString(margin, f);
}
return err;
}

int
_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent,
int margin_indent, char margin_char, int *truncation, PyObject **line)
static int
display_source_line_with_margin(PyObject *f, PyObject *filename, int lineno, int indent,
int margin_indent, const char *margin, int *truncation, PyObject **line)
{
int err = 0;
int fd;
Expand Down Expand Up @@ -537,7 +538,7 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent,
*truncation = i - indent;
}

err |= _Py_WriteIndentedMargin(margin_indent, margin_char, f);
err |= _Py_WriteIndentedMargin(margin_indent, margin, f);
/* Write some spaces before the line */
err |= _Py_WriteIndent(indent, f);

Expand All @@ -550,6 +551,16 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent,
return err;
}

int
_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent,
int *truncation, PyObject **line)
{
return display_source_line_with_margin(
f, filename, lineno, indent,
0, NULL, /* no margin */
truncation, line);
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
}

/* AST based Traceback Specialization
*
* When displaying a new traceback line, for certain syntactical constructs
Expand Down Expand Up @@ -718,7 +729,7 @@ print_error_location_carets(PyObject *f, int offset, Py_ssize_t start_offset, Py

static int
tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int lineno,
PyFrameObject *frame, PyObject *name, int margin_indent, char margin_char)
PyFrameObject *frame, PyObject *name, int margin_indent, const char *margin)
{
int err;
PyObject *line;
Expand All @@ -729,17 +740,17 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
filename, lineno, name);
if (line == NULL)
return -1;
err = _Py_WriteIndentedMargin(margin_indent, margin_char, f);
err = _Py_WriteIndentedMargin(margin_indent, margin, f);
err |= PyFile_WriteObject(line, f, Py_PRINT_RAW);
Py_DECREF(line);
if (err != 0)
return err;
Comment on lines +747 to 760
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.

Didn't someone advise last time not to combine error checks like this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Didn't someone advise last time not to combine error checks like this?

Yes, about PyList_Append calls which can crash. Here and in pythonrun.c (in main) the code seems to assume that PyFile_WriteXX calls can be called any number of times after failure (is this true?). There is a PyErr_Clear() call before each PyFile_WriteObject because that asserts that there is no error set, but the call is not avoided.

There is another PyErr_Clear at the end (these function don't have a return value and don't raise exceptions).

See this comment

/* Can't be bothered to check all those

Copy link
Copy Markdown
Member

@gvanrossum gvanrossum Oct 27, 2021

Choose a reason for hiding this comment

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

More specifically, there are calls to PyErr_occurred() in PyFile_WriteString() that are clearly designed to make this safe. But AFAICT only there, not in PyFile_WriteObject().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a mess, so how about I make a PR to clean up the current code's error checking, we merge that, then I recreate this one.

In the meantime we can sort out the last decisions:

(1) Do we want the "full path" labels? (I think they don't add that much on top of index + indent, and they complicate the code. Let me know if you disagree.)

(2) The box around the topmost EG's traceback?

(3) We definitely need to limit the nested depth we display (to something like 10 or 15). Also the number of exceptions per EG, so we don't spam?

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.

my 2c:

(2) The box around the topmost EG's traceback?

#29207 (comment)

(3) We definitely need to limit the nested depth we display (to something like 10 or 15). Also the number of exceptions per EG, so we don't spam?

I think it's a great idea. Perhaps we should have an env var to set the limits.

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.

It's a mess, so how about I make a PR to clean up the current code's error checking, we merge that, then I recreate this one.

Why not keep this PR but merge from main once the cleanup PR has landed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is only a limit on the number of traceback frames, which applies to chained exceptions too.
It seems to be 1000 by default in C:

#define PyTraceBack_LIMIT 1000

and 'unlimited' in python:

:param limit: None to include all frames or the number of frames to

There is no limit on the number of chained exceptions being printed (which is why we needed the loop to avoid recursion in traceback.py).

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.

There is no limit on the number of chained exceptions being printed (which is why we needed the loop to avoid recursion in traceback.py).

That sounds like a design bug. That limit has been around since long before we had chaining; presumably nobody thought of adding a limit. Unlimited output seems unhelpful. A parameter that limits things seems useful though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a mess, so how about I make a PR to clean up the current code's error checking, we merge that, then I recreate this one.

Change of plans - I found that its more delicate than I originally thought (see bpo-45635). I don't want to hold up this PR until that's sorted, so I'll make sure I'm not adding more error checking issues in the code I'm adding here, but leave the rest for later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added two limits max_group_depth and max_group_width. If we roll everything into one limit I think we will get a constant stream of bug reports about edge cases where you got the wrong part of the output. Though it would probably be rare that it makes a difference in practice.

I didn't add a limit on the length of context/cause chain, because that's outside the scope of this PR. I created bpo-45694 for that.


int truncation = _TRACEBACK_SOURCE_LINE_INDENT;
PyObject* source_line = NULL;
if (_Py_DisplaySourceLine(f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT,
margin_indent, margin_char,
&truncation, &source_line) != 0 || !source_line) {
if (display_source_line_with_margin(
f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT,
margin_indent, margin, &truncation, &source_line) != 0 || !source_line) {
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
/* ignore errors since we can't report them, can we? */
err = ignore_source_errors();
goto done;
Expand Down Expand Up @@ -824,7 +835,7 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
end_offset = i + 1;
}

err = _Py_WriteIndentedMargin(margin_indent, margin_char, f);
err = _Py_WriteIndentedMargin(margin_indent, margin, f);
err |= print_error_location_carets(f, truncation, start_offset, end_offset,
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
right_start_offset, left_end_offset,
primary_error_char, secondary_error_char);
Expand Down Expand Up @@ -855,7 +866,7 @@ tb_print_line_repeated(PyObject *f, long cnt)

static int
tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
int indent, char margin_char)
int indent, const char *margin)
{
int err = 0;
Py_ssize_t depth = 0;
Expand Down Expand Up @@ -889,7 +900,7 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
cnt++;
if (err == 0 && cnt <= TB_RECURSIVE_CUTOFF) {
err = tb_displayline(tb, f, code->co_filename, tb->tb_lineno,
tb->tb_frame, code->co_name, indent, margin_char);
tb->tb_frame, code->co_name, indent, margin);
if (err == 0) {
err = PyErr_CheckSignals();
}
Expand All @@ -906,7 +917,7 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
#define PyTraceBack_LIMIT 1000

int
PyTraceBack_Print_Indented(PyObject *v, int indent, char margin_char, PyObject *f)
PyTraceBack_Print_Indented(PyObject *v, int indent, const char *margin, PyObject *f)
{
int err;
PyObject *limitv;
Expand All @@ -929,17 +940,17 @@ PyTraceBack_Print_Indented(PyObject *v, int indent, char margin_char, PyObject *
return 0;
}
}
err = _Py_WriteIndentedMargin(indent, margin_char, f);
err = _Py_WriteIndentedMargin(indent, margin, f);
err |= PyFile_WriteString("Traceback (most recent call last):\n", f);
if (!err)
err = tb_printinternal((PyTracebackObject *)v, f, limit, indent, margin_char);
err = tb_printinternal((PyTracebackObject *)v, f, limit, indent, margin);
Comment thread
iritkatriel marked this conversation as resolved.
return err;
}

int
PyTraceBack_Print(PyObject *v, PyObject *f)
{
return PyTraceBack_Print_Indented(v, 0, '\0', f);
return PyTraceBack_Print_Indented(v, 0, NULL, f);
}

/* Format an integer in range [0; 0xffffffff] to decimal and write it
Expand Down