From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 62B1E3858C31; Thu, 14 Mar 2024 15:13:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 62B1E3858C31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1710429208; bh=vrwylA6K0mcwyVANpvusuTrvx4U5yfaN63JuImsU1aU=; h=From:To:Subject:Date:From; b=aTD85KIPyEoNiRk43myCPJgjSctTtDlnbBZWbrHuu3VYvdiEMi6ucB8uikPeXVEte iTH+jNuGpXuT5tZRSDStHkZpTjyHupRiz7Keyd7H1n567MTuOSNWSCw3wiXMT5Qd9n ikUe6gOgpql7A2uv3PsF/ITaLVoHsS0oycYGjM04= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Remove 'if' from GDB_PY_HANDLE_EXCEPTION X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 9a03f2185347bd8f20da9bf535bc68a8d0f18ce8 X-Git-Newrev: b0e7d28eae1fa1a91932e47d8cf5decf96c2bc59 Message-Id: <20240314151328.62B1E3858C31@sourceware.org> Date: Thu, 14 Mar 2024 15:13:28 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Db0e7d28eae1f= a1a91932e47d8cf5decf96c2bc59 commit b0e7d28eae1fa1a91932e47d8cf5decf96c2bc59 Author: Tom Tromey Date: Fri Dec 22 11:43:26 2023 -0700 Remove 'if' from GDB_PY_HANDLE_EXCEPTION =20 This removes the embedded 'if' from GDB_PY_HANDLE_EXCEPTION and GDB_PY_SET_HANDLE_EXCEPTION. I believe this 'if' was necessary with the old gdb try/catch macros, but it no longer is: these should only ever be called from a 'catch' block, where it's already known that an exception was thrown. =20 Simon pointed out, though, that in a few spots, these were in facts called outside of 'catch' blocks. This patch cleans up these spots. I also found one spot where a redundant 'return nullptr' could be removed. Diff: --- gdb/python/py-breakpoint.c | 10 ++-------- gdb/python/py-inferior.c | 10 ++-------- gdb/python/py-infthread.c | 1 - gdb/python/py-value.c | 16 +++++----------- gdb/python/python-internal.h | 22 ++++++++-------------- 5 files changed, 17 insertions(+), 42 deletions(-) diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index 0e43a001cff..95782444c4a 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -589,7 +589,6 @@ bppy_set_condition (PyObject *self, PyObject *newvalue,= void *closure) gdb::unique_xmalloc_ptr exp_holder; const char *exp =3D NULL; gdbpy_breakpoint_object *self_bp =3D (gdbpy_breakpoint_object *) self; - struct gdb_exception except; =20 BPPY_SET_REQUIRE_VALID (self_bp); =20 @@ -615,11 +614,9 @@ bppy_set_condition (PyObject *self, PyObject *newvalue= , void *closure) } catch (gdb_exception &ex) { - except =3D std::move (ex); + GDB_PY_SET_HANDLE_EXCEPTION (ex); } =20 - GDB_PY_SET_HANDLE_EXCEPTION (except); - return 0; } =20 @@ -657,7 +654,6 @@ static int bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure) { gdbpy_breakpoint_object *self_bp =3D (gdbpy_breakpoint_object *) self; - struct gdb_exception except; =20 BPPY_SET_REQUIRE_VALID (self_bp); =20 @@ -684,11 +680,9 @@ bppy_set_commands (PyObject *self, PyObject *newvalue,= void *closure) } catch (gdb_exception &ex) { - except =3D std::move (ex); + GDB_PY_SET_HANDLE_EXCEPTION (ex); } =20 - GDB_PY_SET_HANDLE_EXCEPTION (except); - return 0; } =20 diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index caf6c1bdd53..c25519c8dd9 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -586,7 +586,6 @@ static PyObject * infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) { inferior_object *inf =3D (inferior_object *) self; - struct gdb_exception except; Py_ssize_t buf_len; const gdb_byte *buffer; CORE_ADDR addr, length; @@ -625,11 +624,9 @@ infpy_write_memory (PyObject *self, PyObject *args, Py= Object *kw) } catch (gdb_exception &ex) { - except =3D std::move (ex); + GDB_PY_HANDLE_EXCEPTION (ex); } =20 - GDB_PY_HANDLE_EXCEPTION (except); - Py_RETURN_NONE; } =20 @@ -645,7 +642,6 @@ static PyObject * infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) { inferior_object *inf =3D (inferior_object *) self; - struct gdb_exception except; CORE_ADDR start_addr, length; static const char *keywords[] =3D { "address", "length", "pattern", NULL= }; PyObject *start_addr_obj, *length_obj; @@ -702,11 +698,9 @@ infpy_search_memory (PyObject *self, PyObject *args, P= yObject *kw) } catch (gdb_exception &ex) { - except =3D std::move (ex); + GDB_PY_HANDLE_EXCEPTION (ex); } =20 - GDB_PY_HANDLE_EXCEPTION (except); - if (found) return gdb_py_object_from_ulongest (found_addr).release (); else diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index 65c4c6c12cd..995397e7f0d 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -214,7 +214,6 @@ thpy_get_ptid_string (PyObject *self, void *closure) catch (const gdb_exception &except) { GDB_PY_HANDLE_EXCEPTION (except); - return nullptr; } } =20 diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 89ca0982e59..ff7943d80c3 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1046,7 +1046,6 @@ get_field_type (PyObject *field) static PyObject * valpy_getitem (PyObject *self, PyObject *key) { - struct gdb_exception except; value_object *self_value =3D (value_object *) self; gdb::unique_xmalloc_ptr field; struct type *base_class_type =3D NULL, *field_type =3D NULL; @@ -1178,11 +1177,9 @@ valpy_getitem (PyObject *self, PyObject *key) } catch (gdb_exception &ex) { - except =3D std::move (ex); + GDB_PY_HANDLE_EXCEPTION (ex); } =20 - GDB_PY_HANDLE_EXCEPTION (except); - return result; } =20 @@ -1678,7 +1675,6 @@ valpy_absolute (PyObject *self) static int valpy_nonzero (PyObject *self) { - struct gdb_exception except; value_object *self_value =3D (value_object *) self; struct type *type; int nonzero =3D 0; /* Appease GCC warning. */ @@ -1698,14 +1694,12 @@ valpy_nonzero (PyObject *self) } catch (gdb_exception &ex) { - except =3D std::move (ex); + /* This is not documented in the Python documentation, but if + this function fails, return -1 as slot_nb_nonzero does (the + default Python nonzero function). */ + GDB_PY_SET_HANDLE_EXCEPTION (ex); } =20 - /* This is not documented in the Python documentation, but if this - function fails, return -1 as slot_nb_nonzero does (the default - Python nonzero function). */ - GDB_PY_SET_HANDLE_EXCEPTION (except); - return nonzero; } =20 diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d603b3a1b85..5132ec15ba6 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -847,26 +847,20 @@ private: PyGILState_STATE m_state; }; =20 -/* Use this after a TRY_EXCEPT to throw the appropriate Python - exception. */ +/* Use this in a 'catch' block to convert the exception to a Python + exception and return nullptr. */ #define GDB_PY_HANDLE_EXCEPTION(Exception) \ do { \ - if (Exception.reason < 0) \ - { \ - gdbpy_convert_exception (Exception); \ - return NULL; \ - } \ + gdbpy_convert_exception (Exception); \ + return nullptr; \ } while (0) =20 -/* Use this after a TRY_EXCEPT to throw the appropriate Python - exception. This macro is for use inside setter functions. */ +/* Use this in a 'catch' block to convert the exception to a Python + exception and return -1. */ #define GDB_PY_SET_HANDLE_EXCEPTION(Exception) \ do { \ - if (Exception.reason < 0) \ - { \ - gdbpy_convert_exception (Exception); \ - return -1; \ - } \ + gdbpy_convert_exception (Exception); \ + return -1; \ } while (0) =20 int gdbpy_print_python_errors_p (void);