diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-09-11 18:18:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-11 18:18:34 (GMT) |
commit | ecd21a629a2a30bcae89902f7cad5670e9441e2c (patch) | |
tree | a3b8ddac3bdb8867c63da9cb15e83e10680562c9 /Python | |
parent | de5f8f7d13c0bbc723eaea83284dc78b37be54b4 (diff) | |
download | cpython-ecd21a629a2a30bcae89902f7cad5670e9441e2c.zip cpython-ecd21a629a2a30bcae89902f7cad5670e9441e2c.tar.gz cpython-ecd21a629a2a30bcae89902f7cad5670e9441e2c.tar.bz2 |
gh-109179: Fix traceback display for SyntaxErrors with notes (#109197)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pythonrun.c | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ddd8951..e3d03a8 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1125,21 +1125,16 @@ error: } static int -print_exception_notes(struct exception_print_context *ctx, PyObject *value) +print_exception_notes(struct exception_print_context *ctx, PyObject *notes) { PyObject *f = ctx->file; - if (!PyExceptionInstance_Check(value)) { + if (notes == NULL) { return 0; } - PyObject *notes; - int res = PyObject_GetOptionalAttr(value, &_Py_ID(__notes__), ¬es); - if (res <= 0) { - return res; - } if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) { - res = 0; + int res = 0; if (write_indented_margin(ctx, f) < 0) { res = -1; } @@ -1152,7 +1147,6 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value) res = PyFile_WriteObject(s, f, Py_PRINT_RAW); Py_DECREF(s); } - Py_DECREF(notes); if (PyFile_WriteString("\n", f) < 0) { res = -1; } @@ -1197,17 +1191,16 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value) } } - Py_DECREF(notes); return 0; error: Py_XDECREF(lines); - Py_DECREF(notes); return -1; } static int print_exception(struct exception_print_context *ctx, PyObject *value) { + PyObject *notes = NULL; PyObject *f = ctx->file; if (!PyExceptionInstance_Check(value)) { @@ -1221,8 +1214,11 @@ print_exception(struct exception_print_context *ctx, PyObject *value) goto error; } - /* grab the type now because value can change below */ + /* grab the type and notes now because value can change below */ PyObject *type = (PyObject *) Py_TYPE(value); + if (PyObject_GetOptionalAttr(value, &_Py_ID(__notes__), ¬es) < 0) { + goto error; + } if (print_exception_file_and_line(ctx, &value) < 0) { goto error; @@ -1236,14 +1232,16 @@ print_exception(struct exception_print_context *ctx, PyObject *value) if (PyFile_WriteString("\n", f) < 0) { goto error; } - if (print_exception_notes(ctx, value) < 0) { + if (print_exception_notes(ctx, notes) < 0) { goto error; } + Py_XDECREF(notes); Py_DECREF(value); assert(!PyErr_Occurred()); return 0; error: + Py_XDECREF(notes); Py_DECREF(value); return -1; } |