From 2f4dcbc4bf52cbb8039957c7333e04bf7d29118e Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 11 Sep 2023 19:18:34 +0100 Subject: [PATCH] gh-109179: Fix traceback display for SyntaxErrors with notes (#109197) (cherry picked from commit ecd21a629a2a30bcae89902f7cad5670e9441e2c) --- Lib/test/test_traceback.py | 51 +++++++++++-------- ...-09-09-21-17-18.gh-issue-109179.ZR8qs2.rst | 1 + Python/pythonrun.c | 29 ++++++----- 3 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-09-09-21-17-18.gh-issue-109179.ZR8qs2.rst diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 544b24c01b37d8a..80ef8748fedb94d 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1546,27 +1546,36 @@ def __repr__(self): err_msg = '' self.assertEqual(self.get_report(e), vanilla + err_msg + '\nFinal Note\n') - def test_exception_with_note_with_multiple_notes(self): - e = ValueError(42) - vanilla = self.get_report(e) - - e.add_note('Note 1') - e.add_note('Note 2') - e.add_note('Note 3') - - self.assertEqual( - self.get_report(e), - vanilla + 'Note 1\n' + 'Note 2\n' + 'Note 3\n') - - del e.__notes__ - e.add_note('Note 4') - del e.__notes__ - e.add_note('Note 5') - e.add_note('Note 6') - - self.assertEqual( - self.get_report(e), - vanilla + 'Note 5\n' + 'Note 6\n') + e.__notes__ = "please do not explode me" + err_msg = "'please do not explode me'" + self.assertEqual(self.get_report(e), vanilla + err_msg + '\n') + + e.__notes__ = b"please do not show me as numbers" + err_msg = "b'please do not show me as numbers'" + self.assertEqual(self.get_report(e), vanilla + err_msg + '\n') + + def test_exception_with_multiple_notes(self): + for e in [ValueError(42), SyntaxError('bad syntax')]: + with self.subTest(e=e): + vanilla = self.get_report(e) + + e.add_note('Note 1') + e.add_note('Note 2') + e.add_note('Note 3') + + self.assertEqual( + self.get_report(e), + vanilla + 'Note 1\n' + 'Note 2\n' + 'Note 3\n') + + del e.__notes__ + e.add_note('Note 4') + del e.__notes__ + e.add_note('Note 5') + e.add_note('Note 6') + + self.assertEqual( + self.get_report(e), + vanilla + 'Note 5\n' + 'Note 6\n') def test_exception_qualname(self): class A: diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-09-21-17-18.gh-issue-109179.ZR8qs2.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-09-21-17-18.gh-issue-109179.ZR8qs2.rst new file mode 100644 index 000000000000000..dd95a8ec7920aa7 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-09-09-21-17-18.gh-issue-109179.ZR8qs2.rst @@ -0,0 +1 @@ +Fix bug where the C traceback display drops notes from :exc:`SyntaxError`. diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 03e366a8d095dd8..f33474bee7377cf 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1130,21 +1130,16 @@ print_exception_suggestions(struct exception_print_context *ctx, } 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; + if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) { + int res = 0; if (write_indented_margin(ctx, f) < 0) { res = -1; } @@ -1157,7 +1152,9 @@ 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; + } return res; } Py_ssize_t num_notes = PySequence_Length(notes); @@ -1199,17 +1196,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 +1219,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; @@ -1238,14 +1237,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; }