Skip to content

Commit 8311931

Browse files
committed
gh-109179: Fix traceback display for SyntaxErrors with notes
1 parent 75cd865 commit 8311931

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

Lib/test/test_traceback.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,27 +1599,27 @@ def __repr__(self):
15991599
err_msg = "b'please do not show me as numbers'"
16001600
self.assertEqual(self.get_report(e), vanilla + err_msg + '\n')
16011601

1602-
def test_exception_with_note_with_multiple_notes(self):
1603-
e = ValueError(42)
1604-
vanilla = self.get_report(e)
1605-
1606-
e.add_note('Note 1')
1607-
e.add_note('Note 2')
1608-
e.add_note('Note 3')
1609-
1610-
self.assertEqual(
1611-
self.get_report(e),
1612-
vanilla + 'Note 1\n' + 'Note 2\n' + 'Note 3\n')
1613-
1614-
del e.__notes__
1615-
e.add_note('Note 4')
1616-
del e.__notes__
1617-
e.add_note('Note 5')
1618-
e.add_note('Note 6')
1619-
1620-
self.assertEqual(
1621-
self.get_report(e),
1622-
vanilla + 'Note 5\n' + 'Note 6\n')
1602+
def test_exception_with_multiple_notes(self):
1603+
for e in [ValueError(42), SyntaxError('bad syntax')]:
1604+
vanilla = self.get_report(e)
1605+
1606+
e.add_note('Note 1')
1607+
e.add_note('Note 2')
1608+
e.add_note('Note 3')
1609+
1610+
self.assertEqual(
1611+
self.get_report(e),
1612+
vanilla + 'Note 1\n' + 'Note 2\n' + 'Note 3\n')
1613+
1614+
del e.__notes__
1615+
e.add_note('Note 4')
1616+
del e.__notes__
1617+
e.add_note('Note 5')
1618+
e.add_note('Note 6')
1619+
1620+
self.assertEqual(
1621+
self.get_report(e),
1622+
vanilla + 'Note 5\n' + 'Note 6\n')
16231623

16241624
def test_exception_qualname(self):
16251625
class A:

Python/pythonrun.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,21 +1125,16 @@ print_exception_suggestions(struct exception_print_context *ctx,
11251125
}
11261126

11271127
static int
1128-
print_exception_notes(struct exception_print_context *ctx, PyObject *value)
1128+
print_exception_notes(struct exception_print_context *ctx, PyObject *notes)
11291129
{
11301130
PyObject *f = ctx->file;
11311131

1132-
if (!PyExceptionInstance_Check(value)) {
1132+
if (notes == NULL) {
11331133
return 0;
11341134
}
11351135

1136-
PyObject *notes;
1137-
int res = PyObject_GetOptionalAttr(value, &_Py_ID(__notes__), &notes);
1138-
if (res <= 0) {
1139-
return res;
1140-
}
11411136
if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) {
1142-
res = 0;
1137+
int res = 0;
11431138
if (write_indented_margin(ctx, f) < 0) {
11441139
res = -1;
11451140
}
@@ -1152,7 +1147,6 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11521147
res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
11531148
Py_DECREF(s);
11541149
}
1155-
Py_DECREF(notes);
11561150
if (PyFile_WriteString("\n", f) < 0) {
11571151
res = -1;
11581152
}
@@ -1197,17 +1191,16 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11971191
}
11981192
}
11991193

1200-
Py_DECREF(notes);
12011194
return 0;
12021195
error:
12031196
Py_XDECREF(lines);
1204-
Py_DECREF(notes);
12051197
return -1;
12061198
}
12071199

12081200
static int
12091201
print_exception(struct exception_print_context *ctx, PyObject *value)
12101202
{
1203+
PyObject *notes = NULL;
12111204
PyObject *f = ctx->file;
12121205

12131206
if (!PyExceptionInstance_Check(value)) {
@@ -1221,8 +1214,11 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
12211214
goto error;
12221215
}
12231216

1224-
/* grab the type now because value can change below */
1217+
/* grab the type and notes now because value can change below */
12251218
PyObject *type = (PyObject *) Py_TYPE(value);
1219+
if (PyObject_GetOptionalAttr(value, &_Py_ID(__notes__), &notes) < 0) {
1220+
goto error;
1221+
}
12261222

12271223
if (print_exception_file_and_line(ctx, &value) < 0) {
12281224
goto error;
@@ -1236,14 +1232,16 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
12361232
if (PyFile_WriteString("\n", f) < 0) {
12371233
goto error;
12381234
}
1239-
if (print_exception_notes(ctx, value) < 0) {
1235+
if (print_exception_notes(ctx, notes) < 0) {
12401236
goto error;
12411237
}
12421238

1239+
Py_XDECREF(notes);
12431240
Py_DECREF(value);
12441241
assert(!PyErr_Occurred());
12451242
return 0;
12461243
error:
1244+
Py_XDECREF(notes);
12471245
Py_DECREF(value);
12481246
return -1;
12491247
}

0 commit comments

Comments
 (0)