From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id BE3CB3858C53; Fri, 12 Jan 2024 13:43:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE3CB3858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1705067024; bh=EzE35QM6vl4UZ9gqIek1VnSJKAgOtPvMb1ORitorkcU=; h=From:To:Subject:Date:From; b=HmX5pqXMuR0tPrrh4QIGaptmPm9RgpnTME0J1+tdazOnWzlDxgW40ZnA8R2wQfp/C YOK9zixGExKOTf61B7uJuGlxVNYPcX2M7ABJyoplPNmNfxBdm2R/663/VVRxQ3xC/D g/kiofh9K657PPZausMDmCgxswNQiUBpMvApppic= 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: hoist common invalid object repr code into py-utils.c X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 3b9ff5d9009d587aa76ec7bbaab4439e26a52c50 X-Git-Newrev: aef117b7374053099099600ded2f4eaa2c450327 Message-Id: <20240112134344.BE3CB3858C53@sourceware.org> Date: Fri, 12 Jan 2024 13:43:44 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Daef117b73740= 53099099600ded2f4eaa2c450327 commit aef117b7374053099099600ded2f4eaa2c450327 Author: Andrew Burgess Date: Thu Jan 4 10:07:48 2024 +0000 gdb/python: hoist common invalid object repr code into py-utils.c =20 Many object types now have a __repr__() function implementation. A common pattern is that, if an object is invalid, we print its representation as: . =20 I thought it might be a good idea to move the formatting of this specific representation into a utility function, and then update all of our existing code to call the new function. =20 The only place where I haven't made use of the new function is in unwind_infopy_repr, where we currently return a different string. This case is a little different as the UnwindInfo is invalid because it references a frame, and it is the frame itself which is invalid. That said, I think it would be fine to switch to using the standard format; if the UnwindInfo references an invalid frame, then the UnwindInfo is itself invalid. But changing this would be an actual change in behaviour, while all the other changes in this commit are just refactoring. =20 Approved-By: Tom Tromey Diff: --- gdb/python/py-arch.c | 2 +- gdb/python/py-block.c | 2 +- gdb/python/py-breakpoint.c | 4 ++-- gdb/python/py-connection.c | 2 +- gdb/python/py-inferior.c | 2 +- gdb/python/py-objfile.c | 2 +- gdb/python/py-symbol.c | 2 +- gdb/python/py-type.c | 3 +-- gdb/python/py-unwind.c | 2 +- gdb/python/py-utils.c | 8 ++++++++ gdb/python/python-internal.h | 9 +++++++++ 11 files changed, 27 insertions(+), 11 deletions(-) diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index ac519331f18..b7d861d27ad 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -326,7 +326,7 @@ archpy_repr (PyObject *self) { const auto gdbarch =3D arch_object_to_gdbarch (self); if (gdbarch =3D=3D nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name= ); + return gdb_py_invalid_object_repr (self); =20 auto arch_info =3D gdbarch_bfd_arch_info (gdbarch); return PyUnicode_FromFormat ("<%s arch_name=3D%s printable_name=3D%s>", diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index dd6d6d278a0..34be4664144 100644 --- a/gdb/python/py-block.c +++ b/gdb/python/py-block.c @@ -425,7 +425,7 @@ blpy_repr (PyObject *self) { const auto block =3D block_object_to_block (self); if (block =3D=3D nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name= ); + return gdb_py_invalid_object_repr (self); =20 const auto name =3D block->function () ? block->function ()->print_name () : ""; diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index 5155d41e675..9b5e023cb09 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -1750,8 +1750,8 @@ bplocpy_repr (PyObject *py_self) { const auto self =3D (gdbpy_breakpoint_location_object *) py_self; if (self->owner =3D=3D nullptr || self->owner->bp =3D=3D nullptr - || self->owner->bp !=3D self->bp_loc->owner) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name= ); + || self->owner->bp !=3D self->bp_loc->owner) + return gdb_py_invalid_object_repr (py_self); =20 const auto enabled =3D self->bp_loc->enabled ? "enabled" : "disabled"; =20 diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c index 3df12b435bb..d288a74cb2b 100644 --- a/gdb/python/py-connection.c +++ b/gdb/python/py-connection.c @@ -204,7 +204,7 @@ connpy_repr (PyObject *obj) process_stratum_target *target =3D self->target; =20 if (target =3D=3D nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (obj)->tp_name); + return gdb_py_invalid_object_repr (obj); =20 return PyUnicode_FromFormat ("<%s num=3D%d, what=3D\"%s\">", Py_TYPE (obj)->tp_name, diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index ed153d668ac..929d8bd17b5 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -800,7 +800,7 @@ infpy_repr (PyObject *obj) inferior *inf =3D self->inferior; =20 if (inf =3D=3D nullptr) - return PyUnicode_FromString (""); + return gdb_py_invalid_object_repr (obj); =20 return PyUnicode_FromFormat ("", inf->num, inf->pid); diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index bb5d0d92aba..4f5e5cda5e6 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -537,7 +537,7 @@ objfpy_repr (PyObject *self_) objfile *obj =3D self->objfile; =20 if (obj =3D=3D nullptr) - return PyUnicode_FromString (""); + return gdb_py_invalid_object_repr (self_); =20 return PyUnicode_FromFormat ("", objfile_name (obj)); diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 99724cfc95b..014442bf147 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -385,7 +385,7 @@ sympy_repr (PyObject *self) { const auto symbol =3D symbol_object_to_symbol (self); if (symbol =3D=3D nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name= ); + return gdb_py_invalid_object_repr (self); =20 return PyUnicode_FromFormat ("<%s print_name=3D%s>", Py_TYPE (self)->tp_= name, symbol->print_name ()); diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index bfaa6d24d94..27c7b78096b 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -1083,8 +1083,7 @@ typy_repr (PyObject *self) { const auto type =3D type_object_to_type (self); if (type =3D=3D nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", - Py_TYPE (self)->tp_name); + return gdb_py_invalid_object_repr (self); =20 const char *code =3D pyty_codes[type->code ()].name; string_file type_name; diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index f12485c22b7..70c33724cbc 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -425,7 +425,7 @@ pending_framepy_repr (PyObject *self) frame_info_ptr frame =3D pending_frame->frame_info; =20 if (frame =3D=3D nullptr) - return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name= ); + return gdb_py_invalid_object_repr (self); =20 const char *sp_str =3D nullptr; const char *pc_str =3D nullptr; diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index f1ca9ea0a5d..c29291004d2 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -597,3 +597,11 @@ gdbpy_fix_doc_string_indentation (gdb::unique_xmalloc_= ptr doc) =20 return doc; } + +/* See python-internal.h. */ + +PyObject * +gdb_py_invalid_object_repr (PyObject *self) +{ + return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); +} diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 14e15574685..8ff9af650c2 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -897,6 +897,15 @@ int gdb_pymodule_addobject (PyObject *module, const ch= ar *name, PyObject *object) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; =20 + +/* Return a Python string (str) object that represents SELF. SELF can be + any object type, but should be in an "invalid" state. What "invalid" + means is up to the caller. The returned string will take the form + "", without the quotes, and with TYPENAME replaced + with the type of SELF. */ + +PyObject *gdb_py_invalid_object_repr (PyObject *self); + struct varobj_iter; struct varobj; std::unique_ptr py_varobj_get_iterator