Skip to content

[3.11] gh-109179: Fix traceback display for SyntaxErrors with notes (#109197) #109283

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 1 commit into from
Sep 12, 2023
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
43 changes: 22 additions & 21 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,27 +1546,28 @@ def __repr__(self):
err_msg = '<note str() failed>'
self.assertEqual(self.get_report(e), vanilla + err_msg + '\nFinal Note\n')

def test_exception_with_note_with_multiple_notes(self):
e = ValueError(42)
vanilla = self.get_report(e)

e.add_note('Note 1')
e.add_note('Note 2')
e.add_note('Note 3')

self.assertEqual(
self.get_report(e),
vanilla + 'Note 1\n' + 'Note 2\n' + 'Note 3\n')

del e.__notes__
e.add_note('Note 4')
del e.__notes__
e.add_note('Note 5')
e.add_note('Note 6')

self.assertEqual(
self.get_report(e),
vanilla + 'Note 5\n' + 'Note 6\n')
def test_exception_with_multiple_notes(self):
for e in [ValueError(42), SyntaxError('bad syntax')]:
with self.subTest(e=e):
vanilla = self.get_report(e)

e.add_note('Note 1')
e.add_note('Note 2')
e.add_note('Note 3')

self.assertEqual(
self.get_report(e),
vanilla + 'Note 1\n' + 'Note 2\n' + 'Note 3\n')

del e.__notes__
e.add_note('Note 4')
del e.__notes__
e.add_note('Note 5')
e.add_note('Note 6')

self.assertEqual(
self.get_report(e),
vanilla + 'Note 5\n' + 'Note 6\n')

def test_exception_qualname(self):
class A:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where the C traceback display drops notes from :exc:`SyntaxError`.
24 changes: 11 additions & 13 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -1130,21 +1130,16 @@ print_exception_suggestions(struct exception_print_context *ctx,
}

static int
print_exception_notes(struct exception_print_context *ctx, PyObject *value)
print_exception_notes(struct exception_print_context *ctx, PyObject *notes)
{
PyObject *f = ctx->file;

if (!PyExceptionInstance_Check(value)) {
if (notes == NULL) {
return 0;
}

PyObject *notes;
int res = _PyObject_LookupAttr(value, &_Py_ID(__notes__), &notes);
if (res <= 0) {
return res;
}
if (!PySequence_Check(notes)) {
res = 0;
int res = 0;
if (write_indented_margin(ctx, f) < 0) {
res = -1;
}
Expand All @@ -1157,7 +1152,6 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
Py_DECREF(s);
}
Py_DECREF(notes);
return res;
}
Py_ssize_t num_notes = PySequence_Length(notes);
Expand Down Expand Up @@ -1199,17 +1193,16 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
}
}

Py_DECREF(notes);
return 0;
error:
Py_XDECREF(lines);
Py_DECREF(notes);
return -1;
}

static int
print_exception(struct exception_print_context *ctx, PyObject *value)
{
PyObject *notes = NULL;
PyObject *f = ctx->file;

if (!PyExceptionInstance_Check(value)) {
Expand All @@ -1223,8 +1216,11 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
goto error;
}

/* grab the type now because value can change below */
/* grab the type and notes now because value can change below */
PyObject *type = (PyObject *) Py_TYPE(value);
if (_PyObject_LookupAttr(value, &_Py_ID(__notes__), &notes) < 0) {
goto error;
}

if (print_exception_file_and_line(ctx, &value) < 0) {
goto error;
Expand All @@ -1238,14 +1234,16 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
if (PyFile_WriteString("\n", f) < 0) {
goto error;
}
if (print_exception_notes(ctx, value) < 0) {
if (print_exception_notes(ctx, notes) < 0) {
goto error;
}

Py_XDECREF(notes);
Py_DECREF(value);
assert(!PyErr_Occurred());
return 0;
error:
Py_XDECREF(notes);
Py_DECREF(value);
return -1;
}
Expand Down