gh-103895: Improve how invalid Exception.__notes__ are displayed (#103897)

This commit is contained in:
Carey Metcalfe 2023-05-01 01:32:04 -06:00 committed by GitHub
parent 93107aa2a4
commit 487f55d580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View file

@ -1107,7 +1107,7 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
if (notes == NULL) {
return -1;
}
if (!PySequence_Check(notes)) {
if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) {
int res = 0;
if (write_indented_margin(ctx, f) < 0) {
res = -1;
@ -1122,6 +1122,9 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
Py_DECREF(s);
}
Py_DECREF(notes);
if (PyFile_WriteString("\n", f) < 0) {
res = -1;
}
return res;
}
Py_ssize_t num_notes = PySequence_Length(notes);