From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCHv2 1/8] gdb/python: hoist common invalid object repr code into py-utils.c
Date: Wed, 10 Jan 2024 15:54:37 +0000 [thread overview]
Message-ID: <791370ed269cee0ce3dc30f0a01307c00473878a.1704901918.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1704901918.git.aburgess@redhat.com>
Many object types now have a __repr__() function implementation. A
common pattern is that, if an object is invalid, we print its
representation as: <TYPENAME (invalid)>.
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.
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.
---
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 = arch_object_to_gdbarch (self);
if (gdbarch == nullptr)
- return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+ return gdb_py_invalid_object_repr (self);
auto arch_info = gdbarch_bfd_arch_info (gdbarch);
return PyUnicode_FromFormat ("<%s arch_name=%s printable_name=%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 = block_object_to_block (self);
if (block == nullptr)
- return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+ return gdb_py_invalid_object_repr (self);
const auto name = block->function () ?
block->function ()->print_name () : "<anonymous>";
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 = (gdbpy_breakpoint_location_object *) py_self;
if (self->owner == nullptr || self->owner->bp == nullptr
- || self->owner->bp != self->bp_loc->owner)
- return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+ || self->owner->bp != self->bp_loc->owner)
+ return gdb_py_invalid_object_repr (py_self);
const auto enabled = self->bp_loc->enabled ? "enabled" : "disabled";
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 = self->target;
if (target == nullptr)
- return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (obj)->tp_name);
+ return gdb_py_invalid_object_repr (obj);
return PyUnicode_FromFormat ("<%s num=%d, what=\"%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 = self->inferior;
if (inf == nullptr)
- return PyUnicode_FromString ("<gdb.Inferior (invalid)>");
+ return gdb_py_invalid_object_repr (obj);
return PyUnicode_FromFormat ("<gdb.Inferior num=%d, pid=%d>",
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 = self->objfile;
if (obj == nullptr)
- return PyUnicode_FromString ("<gdb.Objfile (invalid)>");
+ return gdb_py_invalid_object_repr (self_);
return PyUnicode_FromFormat ("<gdb.Objfile filename=%s>",
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 = symbol_object_to_symbol (self);
if (symbol == nullptr)
- return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+ return gdb_py_invalid_object_repr (self);
return PyUnicode_FromFormat ("<%s print_name=%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 = type_object_to_type (self);
if (type == nullptr)
- return PyUnicode_FromFormat ("<%s (invalid)>",
- Py_TYPE (self)->tp_name);
+ return gdb_py_invalid_object_repr (self);
const char *code = 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 = pending_frame->frame_info;
if (frame == nullptr)
- return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+ return gdb_py_invalid_object_repr (self);
const char *sp_str = nullptr;
const char *pc_str = 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<char> doc)
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 char *name,
PyObject *object)
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+
+/* 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
+ "<TYPENAME (invalid)>", 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<varobj_iter> py_varobj_get_iterator
--
2.25.4
next prev parent reply other threads:[~2024-01-10 15:55 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-05 11:48 [PATCH 0/6] Python __repr__() methods and new __dict__ attributes Andrew Burgess
2024-01-05 11:48 ` [PATCH 1/6] gdb/python: hoist common invalid object repr code into py-utils.c Andrew Burgess
2024-01-09 19:19 ` Tom Tromey
2024-01-05 11:48 ` [PATCH 2/6] gdb/python: add gdb.InferiorThread.__repr__() method Andrew Burgess
2024-01-05 11:48 ` [PATCH 3/6] gdb/python: add gdb.Frame.__repr__() method Andrew Burgess
2024-01-05 11:48 ` [PATCH 4/6] gdb/python: remove users ability to create gdb.Progspace objects Andrew Burgess
2024-01-05 13:27 ` Eli Zaretskii
2024-01-05 11:48 ` [PATCH 5/6] gdb/python: Add gdb.Inferior.__dict__ attribute Andrew Burgess
2024-01-05 13:33 ` Eli Zaretskii
2024-01-09 20:05 ` Tom Tromey
2024-01-05 11:48 ` [PATCH 6/6] gdb/python: Add gdb.InferiorThread.__dict__ attribute Andrew Burgess
2024-01-05 13:31 ` Eli Zaretskii
2024-01-09 20:11 ` Tom Tromey
2024-01-10 10:38 ` Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 0/8] Python __repr__() methods and new __dict__ attributes Andrew Burgess
2024-01-09 17:32 ` [PATCH] gdb/python: New InferiorThread.ptid_string attribute Andrew Burgess
2024-01-09 18:50 ` Eli Zaretskii
2024-01-09 19:10 ` Tom Tromey
2024-01-12 9:39 ` [PUSHED] " Andrew Burgess
2024-01-10 15:54 ` [PATCH] " Andrew Burgess
2024-01-10 15:54 ` Andrew Burgess [this message]
2024-01-10 15:54 ` [PATCHv2 2/8] gdb/python: add gdb.InferiorThread.__repr__() method Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 3/8] gdb/python: add gdb.Frame.__repr__() method Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 4/8] gdb/python: remove users ability to create gdb.Progspace objects Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 5/8] gdb/python: Add gdb.Inferior.__dict__ attribute Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 6/8] gdb/python: Add gdb.InferiorThread.__dict__ attribute Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 7/8] gdb/doc: add some notes on selecting suitable attribute names Andrew Burgess
2024-01-10 16:35 ` Eli Zaretskii
2024-01-11 10:48 ` Andrew Burgess
2024-01-11 10:56 ` Eli Zaretskii
2024-01-10 15:54 ` [PATCHv2 8/8] gdb/doc: update examples in gdb.Progspace and gdb.Objfile docs Andrew Burgess
2024-01-10 16:36 ` Eli Zaretskii
2024-01-10 18:08 ` [PATCHv2 0/8] Python __repr__() methods and new __dict__ attributes Tom Tromey
2024-01-12 13:44 ` Andrew Burgess
2024-01-12 14:57 ` Tom de Vries
2024-01-12 16:20 ` Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=791370ed269cee0ce3dc30f0a01307c00473878a.1704901918.git.aburgess@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox