Skip to content

Commit

Permalink
simplify traceback code (no need to special case note which is a string)
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Mar 21, 2022
1 parent 614378e commit f240e71
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
3 changes: 1 addition & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,7 @@ def format_exception_only(self):
yield from self._format_syntax_error(stype)
if isinstance(self.__notes__, collections.abc.Sequence):
for note in self.__notes__:
if not isinstance(note, str):
note = _safe_string(note, 'note')
note = _safe_string(note, 'note')
yield from [l + '\n' for l in note.split('\n')]
elif self.__notes__ is not None:
yield _safe_string(self.__notes__, '__notes__', func=repr)
Expand Down
43 changes: 15 additions & 28 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -1165,9 +1165,19 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
PyObject *lines = NULL;
for (Py_ssize_t ni = 0; ni < num_notes; ni++) {
PyObject *note = PySequence_GetItem(notes, ni);
if (PyUnicode_Check(note)) {
lines = PyUnicode_Splitlines(note, 1);
Py_DECREF(note);
PyObject *note_str = PyObject_Str(note);
Py_DECREF(note);

if (note_str == NULL) {
PyErr_Clear();
if (PyFile_WriteString("<note str() failed>", f) < 0) {
goto error;
}
}
else {
lines = PyUnicode_Splitlines(note_str, 1);
Py_DECREF(note_str);

if (lines == NULL) {
goto error;
}
Expand All @@ -1183,33 +1193,10 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
goto error;
}
}
if (PyFile_WriteString("\n", f) < 0) {
goto error;
}
Py_CLEAR(lines);
}
else {
int res = 0;
if (write_indented_margin(ctx, f) < 0) {
res = -1;
}
PyObject *s = PyObject_Str(note);
if (s == NULL) {
PyErr_Clear();
res = PyFile_WriteString("<note str() failed>", f);
}
else {
res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
Py_DECREF(s);
}
Py_DECREF(note);
if (res < 0) {
goto error;
}
if (PyFile_WriteString("\n", f) < 0) {
goto error;
}

if (PyFile_WriteString("\n", f) < 0) {
goto error;
}
}

Expand Down

0 comments on commit f240e71

Please sign in to comment.