diff --git a/Lib/traceback.py b/Lib/traceback.py index 838637722d7e89..c011c9f683c161 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -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) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 6c347705863780..9a3e9efa2c7862 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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("", f) < 0) { + goto error; + } + } + else { + lines = PyUnicode_Splitlines(note_str, 1); + Py_DECREF(note_str); + if (lines == NULL) { goto error; } @@ -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("", 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; } }