diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-09-12 09:57:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-12 09:57:28 (GMT) |
commit | 1e8696133c6c83323e448238924fc94d462e36f0 (patch) | |
tree | d14d712d64129089bd12b22ca2f06778e9a998f4 /Python/pythonrun.c | |
parent | 0e2d67457ba245442457432d15115d1dc1b0ecb5 (diff) | |
download | cpython-1e8696133c6c83323e448238924fc94d462e36f0.zip cpython-1e8696133c6c83323e448238924fc94d462e36f0.tar.gz cpython-1e8696133c6c83323e448238924fc94d462e36f0.tar.bz2 |
[3.11] gh-109179: Fix traceback display for SyntaxErrors with notes (#109197) (#109283)
gh-109179: Fix traceback display for SyntaxErrors with notes (#109197)
(cherry picked from commit ecd21a629a2a30bcae89902f7cad5670e9441e2c)
Diffstat (limited to 'Python/pythonrun.c')
-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 03e366a..91c2ad3 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1130,21 +1130,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_LookupAttr(value, &_Py_ID(__notes__), ¬es); - if (res <= 0) { - return res; - } if (!PySequence_Check(notes)) { - res = 0; + int res = 0; if (write_indented_margin(ctx, f) < 0) { res = -1; } @@ -1157,7 +1152,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); return res; } Py_ssize_t num_notes = PySequence_Length(notes); @@ -1199,17 +1193,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)) { @@ -1223,8 +1216,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_LookupAttr(value, &_Py_ID(__notes__), ¬es) < 0) { + goto error; + } if (print_exception_file_and_line(ctx, &value) < 0) { goto error; @@ -1238,14 +1234,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; } |