From aef117b7374053099099600ded2f4eaa2c450327 Mon Sep 17 00:00:00 2001
From: Andrew Burgess <aburgess@redhat.com>
Date: Thu, 4 Jan 2024 10:07:48 +0000
Subject: gdb/python: hoist common invalid object repr code into py-utils.c

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-block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'gdb/python/py-block.c')

diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index dd6d6d2..34be466 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>";
-- 
cgit v1.1