]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: hoist common invalid object repr code into py-utils.c
authorAndrew Burgess <aburgess@redhat.com>
Thu, 4 Jan 2024 10:07:48 +0000 (10:07 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 12 Jan 2024 11:21:25 +0000 (11:21 +0000)
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.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/python/py-arch.c
gdb/python/py-block.c
gdb/python/py-breakpoint.c
gdb/python/py-connection.c
gdb/python/py-inferior.c
gdb/python/py-objfile.c
gdb/python/py-symbol.c
gdb/python/py-type.c
gdb/python/py-unwind.c
gdb/python/py-utils.c
gdb/python/python-internal.h

index ac519331f18d5c02f4b4dec9553657cc21054e94..b7d861d27ad670a47332fe8e2070ad943c3d9b03 100644 (file)
@@ -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>",
index dd6d6d278a037db7376d9f0ae1fa9be97ff947f1..34be46641449267dcf3ec43884017273ca087b9b 100644 (file)
@@ -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>";
index 5155d41e675c2d81f8ad839b0b432864d0863b78..9b5e023cb096a4e0de26b9ce1434b4c7f55eaff5 100644 (file)
@@ -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";
 
index 3df12b435bb1ebcfd9c8cc6a3614e63f6c93f0fb..d288a74cb2b9009fcd4f708b4758198bfe47db6f 100644 (file)
@@ -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,
index ed153d668acf8c578f2425269e91201a84365e98..929d8bd17b5efb5aa0c33551634235595a40a2ba 100644 (file)
@@ -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);
index bb5d0d92aba847e8d5c7dee609f44196099ce8c2..4f5e5cda5e6a0449b2a9c42667278cd327e966cc 100644 (file)
@@ -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));
index 99724cfc95b8d972aa4819045a1b363c8283b21b..014442bf147438481f293e41bb18408eb449975f 100644 (file)
@@ -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 ());
index bfaa6d24d94dc34bd5ce39a04846f229db4785b0..27c7b78096b00be256ae3f362f9e35729df03121 100644 (file)
@@ -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;
index f12485c22b77e4635a4f481a88c0635d35aeb7b8..70c33724cbc33a1f957af3f88f099e6903cbfcea 100644 (file)
@@ -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;
index f1ca9ea0a5dbeaa4ab473aa1eba108f33ce4936d..c29291004d27cc797a79f16caa6ac3237ca15598 100644 (file)
@@ -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);
+}
index 14e1557468519fd6e08d224c11cc4d10b6632491..8ff9af650c2160e799ce57b9f8fe806b10a8e300 100644 (file)
@@ -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