From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id 8FE3A384D181; Wed, 15 Jun 2022 09:03:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8FE3A384D181 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/python: convert gdbpy_err_fetch to use gdbpy_ref X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 5fb28d2607a8325559b44a5dc0c8760236c81218 X-Git-Newrev: 8a0b60471a75ce81b8ea067f6e87457b3ed0c7a3 Message-Id: <20220615090332.8FE3A384D181@sourceware.org> Date: Wed, 15 Jun 2022 09:03:32 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jun 2022 09:03:32 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D8a0b60471a75= ce81b8ea067f6e87457b3ed0c7a3 commit 8a0b60471a75ce81b8ea067f6e87457b3ed0c7a3 Author: Andrew Burgess Date: Tue May 24 11:54:40 2022 +0100 gdb/python: convert gdbpy_err_fetch to use gdbpy_ref =20 Convert the gdbpy_err_fetch class to make use of gdbpy_ref, this removes the need for manual reference count management, and allows the destructor to be removed. =20 There should be no functional change after this commit. =20 I think this cleanup is worth doing on its own, however, in a later commit I will want to copy instances of gdbpy_err_fetch, and switching to using gdbpy_ref means that I can rely on the default copy constructor, without having to add one that handles the reference counts, so this is good preparation for that upcoming change. Diff: --- gdb/python/py-utils.c | 8 ++++---- gdb/python/python-internal.h | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 1bd7b477ecb..58c18c60e2c 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -194,10 +194,10 @@ gdbpy_err_fetch::to_string () const Using str (aka PyObject_Str) will fetch the error message from gdb.GdbError ("message"). */ =20 - if (m_error_value && m_error_value !=3D Py_None) - return gdbpy_obj_to_string (m_error_value); + if (m_error_value.get () !=3D nullptr && m_error_value.get () !=3D Py_No= ne) + return gdbpy_obj_to_string (m_error_value.get ()); else - return gdbpy_obj_to_string (m_error_type); + return gdbpy_obj_to_string (m_error_type.get ()); } =20 /* See python-internal.h. */ @@ -205,7 +205,7 @@ gdbpy_err_fetch::to_string () const gdb::unique_xmalloc_ptr gdbpy_err_fetch::type_to_string () const { - return gdbpy_obj_to_string (m_error_type); + return gdbpy_obj_to_string (m_error_type.get ()); } =20 /* Convert a GDB exception to the appropriate Python exception. diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 217bc15bb28..da2e79101a6 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -549,14 +549,12 @@ public: =20 gdbpy_err_fetch () { - PyErr_Fetch (&m_error_type, &m_error_value, &m_error_traceback); - } + PyObject *error_type, *error_value, *error_traceback; =20 - ~gdbpy_err_fetch () - { - Py_XDECREF (m_error_type); - Py_XDECREF (m_error_value); - Py_XDECREF (m_error_traceback); + PyErr_Fetch (&error_type, &error_value, &error_traceback); + m_error_type.reset (error_type); + m_error_value.reset (error_value); + m_error_traceback.reset (error_traceback); } =20 /* Call PyErr_Restore using the values stashed in this object. @@ -565,10 +563,9 @@ public: =20 void restore () { - PyErr_Restore (m_error_type, m_error_value, m_error_traceback); - m_error_type =3D nullptr; - m_error_value =3D nullptr; - m_error_traceback =3D nullptr; + PyErr_Restore (m_error_type.release (), + m_error_value.release (), + m_error_traceback.release ()); } =20 /* Return the string representation of the exception represented by @@ -587,12 +584,12 @@ public: =20 bool type_matches (PyObject *type) const { - return PyErr_GivenExceptionMatches (m_error_type, type); + return PyErr_GivenExceptionMatches (m_error_type.get (), type); } =20 private: =20 - PyObject *m_error_type, *m_error_value, *m_error_traceback; + gdbpy_ref<> m_error_type, m_error_value, m_error_traceback; }; =20 /* Called before entering the Python interpreter to install the