8000 simplify traceback code (no need to special case note which is a string) · python/cpython@f240e71 · GitHub
[go: up one dir, main page]

Skip to content

Commit f240e71

Browse files
committed
simplify traceback code (no need to special case note which is a string)
1 parent 614378e commit f240e71

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

Lib/traceback.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,7 @@ def format_exception_only(self):
824824
yield from self._format_syntax_error(stype)
825825
if isinstance(self.__notes__, collections.abc.Sequence):
826826
for note in self.__notes__:
827-
if not isinstance(note, str):
828-
note = _safe_string(note, 'note')
827+
note = _safe_string(note, 'note')
829828
yield from [l + '\n' for l in note.split('\n')]
830829
elif self.__notes__ is not None:
831830
yield _safe_string(self.__notes__, '__notes__', func=repr)

Python/pythonrun.c

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,9 +1165,19 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11651165
PyObject *lines = NULL;
11661166
for (Py_ssize_t ni = 0; ni < num_notes; ni++) {
11671167
PyObject *note = PySequence_GetItem(notes, ni);
1168-
if (PyUnicode_Check(note)) {
1169-
lines = PyUnicode_Splitlines(note, 1);
1170-
Py_DECREF(note);
1168+
PyObject *note_str = PyObject_Str(note);
1169+
Py_DECREF(note);
1170+
1171+
if (note_str == NULL) {
1172+
PyErr_Clear();
1173+
if (PyFile_WriteString("<note str() failed>", f) < 0) {
1174+
goto error;
1175+
}
1176+
}
1177+
else {
1178+
lines = PyUnicode_Splitlines(note_str, 1);
1179+
Py_DECREF(note_str);
1180+
11711181
if (lines == NULL) {
11721182
goto error;
11731183
}
@@ -1183,33 +1193,10 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11831193
goto error;
11841194
}
11851195
}
1186-
if (PyFile_WriteString("\n", f) < 0) {
1187-
goto error;
1188-
}
11891196
Py_CLEAR(lines);
11901197
}
1191-
else {
< EE1B /td>
1192-
int res = 0;
1193-
if (write_indented_margin(ctx, f) < 0) {
1194-
res = -1;
1195-
}
1196-
PyObject *s = PyObject_Str(note);
1197-
if (s == NULL) {
1198-
PyErr_Clear();
1199-
res = PyFile_WriteString("<note str() failed>", f);
1200-
}
1201-
else {
1202-
res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1203-
Py_DECREF(s);
1204-
}
1205-
Py_DECREF(note);
1206-
if (res < 0) {
1207-
goto error;
1208-
}
1209-
if (PyFile_WriteString("\n", f) < 0) {
1210-
goto error;
1211-
}
1212-
1198+
if (PyFile_WriteString("\n", f) < 0) {
1199+
goto error;
12131200
}
12141201
}
12151202

0 commit comments

Comments
 (0)
0