From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id DD3DA3857C4D; Sat, 9 Mar 2024 15:12:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD3DA3857C4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1709997159; bh=EYkArMsV+YkzFa+DVJN2Qd1BdZ4MIvQ6X6MPStv+R9A=; h=From:To:Subject:Date:From; b=h33dE/YZ2WnIFq3GWKmuZfD3Dv4vf+AgnlCMw/uMNKToR9RK0sdV9u+W2bAWqt/GV xQa9El8M/7nIYGbhMA/Mr9eFqKMBPHJWpNtxdOZ6NO3DRg0mMJYdlkEVu20VUh8a/5 uGwvnEuH7cXnjfylNGvN528qbaU2D4aAiDZVquPo= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/python] Normalize exceptions in gdbpy_err_fetch X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: b1abf8b1b93b5ccdcd400b925dd9046b80f0c32b X-Git-Newrev: 50ede76876981364d6b1a314bb79c0412980c435 Message-Id: <20240309151239.DD3DA3857C4D@sourceware.org> Date: Sat, 9 Mar 2024 15:12:39 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D50ede7687698= 1364d6b1a314bb79c0412980c435 commit 50ede76876981364d6b1a314bb79c0412980c435 Author: Tom de Vries Date: Sat Mar 9 16:13:10 2024 +0100 [gdb/python] Normalize exceptions in gdbpy_err_fetch =20 With python 3.12, I run into: ... (gdb) PASS: gdb.python/py-block.exp: check variable access python print (block['nonexistent'])^M Python Exception : 'nonexistent'^M Error occurred in Python: 'nonexistent'^M (gdb) FAIL: gdb.python/py-block.exp: check nonexistent variable ... =20 The problem is that that PyErr_Fetch returns a normalized exception, wh= ile the test-case matches the output for an unnormalized exception. =20 With python 3.6, PyErr_Fetch returns an unnormalized exception, and the test passes. =20 Fix this by: - updating the test-case to match the output for a normalized exception= , and - lazily forcing normalized exceptions using PyErr_NormalizeException. =20 Tested on aarch64-linux. =20 Approved-By: Tom Tromey Diff: --- gdb/python/python-internal.h | 15 ++++++++++++++- gdb/testsuite/gdb.python/py-block.exp | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 9ceb4aa7ed4..30802ae2480 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -683,6 +683,18 @@ public: =20 gdbpy_ref<> value () const { + if (!m_normalized) + { + PyObject *error_type, *error_value, *error_traceback; + error_type =3D m_error_type.release (); + error_value =3D m_error_value.release (); + error_traceback =3D m_error_traceback.release (); + PyErr_NormalizeException (&error_type, &error_value, &error_traceback); + m_error_type.reset (error_type); + m_error_value.reset (error_value); + m_error_traceback.reset (error_traceback); + m_normalized =3D true; + } return m_error_value; } =20 @@ -695,7 +707,8 @@ public: =20 private: =20 - gdbpy_ref<> m_error_type, m_error_value, m_error_traceback; + mutable gdbpy_ref<> m_error_type, m_error_value, m_error_traceback; + mutable bool m_normalized =3D false; }; =20 /* Called before entering the Python interpreter to install the diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.pyth= on/py-block.exp index 942611af305..99642a546a7 100644 --- a/gdb/testsuite/gdb.python/py-block.exp +++ b/gdb/testsuite/gdb.python/py-block.exp @@ -44,7 +44,7 @@ gdb_test "python print (block.function)" "None" "first an= onymous block" gdb_test "python print (block.start)" "${decimal}" "check start not None" gdb_test "python print (block.end)" "${decimal}" "check end not None" gdb_test "python print (block\['f'\].name =3D=3D 'f')" "True" "check varia= ble access" -gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: nonexisten= t.*" \ +gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexiste= nt'.*" \ "check nonexistent variable" gdb_test "python print (block\[42\])" ".*TypeError.*: Expected a string.*"= \ "check non-string key"