[binutils-gdb] gdb: add new helpers for retrieving a type's fully qualified name

Matthieu Longo via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e3998113f9b66cbd0806424ac4700d2aaf8d0f77

commit e3998113f9b66cbd0806424ac4700d2aaf8d0f77
Author: Matthieu Longo <[email protected]>
Date:   Wed Jan 28 13:09:51 2026 +0000

    gdb: add new helpers for retrieving a type's fully qualified name
    
    Py_TYPE (self)->tp_name is the traditional idiomatic way to get a Python
    type's fully qualified name. However, in the context of the Python
    limited API, PyTypeObject is opaque, so the 'tp_name' attribute is no
    longer accessible. Additionally, retrieving the type of a Python object
    requires Py_TYPE, which is only available as part of the stable API
    starting with Python 3.14.
    
    This patch increases minimal Python limited API version from 3.11 to 3.14.
    It also introduces two new helpers to retrieve a type's fully qualified
    name: gdb_py_tp_name() and gdbpy_py_obj_tp_name(), which extract the fully
    qualified name from a PyTypeObject and a PyObject, respectively. Ifdefery
    allows these wrappers to select the appropriate API depending on the Python
    version and whether the Python limited API is enabled. For any Python
    version less than 3.13, gdb_py_tp_name() fallbacks using __qualname__
    instead. However, the result may differ slightly in some cases, e.g. the
    module name may be missing.
    
    Finally, this patch adapts the existing code to use these wrappers, and
    adjusts some test expectations to use the fully qualified name (or
    __qualname__ for versions <= 3.13) where it was not previously used.
    Note that the corner case where the module name would be missing does not
    appear in the testsuite.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
    
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/Makefile.in                            |  1 +
 gdb/configure                              | 12 ++---
 gdb/configure.ac                           | 12 ++---
 gdb/python/py-arch.c                       |  3 +-
 gdb/python/py-block.c                      |  3 +-
 gdb/python/py-breakpoint.c                 |  9 ++--
 gdb/python/py-connection.c                 |  2 +-
 gdb/python/py-corefile.c                   |  2 +-
 gdb/python/py-disasm.c                     | 17 +++----
 gdb/python/py-frame.c                      |  4 +-
 gdb/python/py-infthread.c                  |  2 +-
 gdb/python/py-mi.c                         |  2 +-
 gdb/python/py-micmd.c                      |  2 +-
 gdb/python/py-obj-type.c                   | 74 ++++++++++++++++++++++++++++++
 gdb/python/py-obj-type.h                   | 29 ++++++++++++
 gdb/python/py-style.c                      | 14 +++---
 gdb/python/py-symbol.c                     |  3 +-
 gdb/python/py-type.c                       |  3 +-
 gdb/python/py-unwind.c                     |  8 ++--
 gdb/python/py-utils.c                      |  4 +-
 gdb/python/python-internal.h               |  6 ++-
 gdb/python/python.c                        |  2 +-
 gdb/testsuite/gdb.python/py-disasm.exp.tcl |  8 ++--
 gdb/testsuite/gdb.python/py-unwind.exp     |  2 +-
 24 files changed, 169 insertions(+), 55 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 2488d789580..81a601dc448 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -422,6 +422,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
+	python/py-obj-type.c \
 	python/py-param.c \
 	python/py-prettyprint.c \
 	python/py-progspace.c \
diff --git a/gdb/configure b/gdb/configure
index 14d0848a227..abac48b92b2 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -28765,8 +28765,8 @@ fi
 
 
 if test "$enable_py_limited_api" = yes; then
-  # The minimal Python limited API version is currently set to 3.11 for the
-  # support of PyBuffer_FillInfo and PyBuffer_Release.
+  # The minimal Python limited API version is currently set to 3.14 for the
+  # support of Py_TYPE().
   # The choice of the minimal version for the Python limited API won't be
   # frozen until the end of the migration.
   old_CFLAGS="$CFLAGS"
@@ -28781,8 +28781,8 @@ if test "$enable_py_limited_api" = yes; then
 #ifndef PY_VERSION_HEX
 #error "PY_VERSION_HEX is not defined"
 #endif
-#if PY_VERSION_HEX < 0x030b0000
-#error "Python limited API support requires at least Python version 3.11"
+#if PY_VERSION_HEX < 0x030e0000
+#error "Python limited API support requires at least Python version 3.14"
 #endif
 
 int
@@ -28795,12 +28795,12 @@ main ()
 _ACEOF
 if ac_fn_c_try_compile "$LINENO"; then :
 
-$as_echo "#define Py_LIMITED_API 0x030b0000" >>confdefs.h
+$as_echo "#define Py_LIMITED_API 0x030e0000" >>confdefs.h
 
 
 else
 
-      as_fn_error $? "Python limited API support requires at least Python version 3.11" "$LINENO" 5
+      as_fn_error $? "Python limited API support requires at least Python version 3.14" "$LINENO" 5
 
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
diff --git a/gdb/configure.ac b/gdb/configure.ac
index a6da4ac7b24..cad343ebe8b 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1078,8 +1078,8 @@ AC_ARG_ENABLE([py-limited-api],
 	      [enable_py_limited_api=no])
 
 if test "$enable_py_limited_api" = yes; then
-  # The minimal Python limited API version is currently set to 3.11 for the
-  # support of PyBuffer_FillInfo and PyBuffer_Release.
+  # The minimal Python limited API version is currently set to 3.14 for the
+  # support of Py_TYPE().
   # The choice of the minimal version for the Python limited API won't be
   # frozen until the end of the migration.
   old_CFLAGS="$CFLAGS"
@@ -1093,14 +1093,14 @@ if test "$enable_py_limited_api" = yes; then
 #ifndef PY_VERSION_HEX
 #error "PY_VERSION_HEX is not defined"
 #endif
-#if PY_VERSION_HEX < 0x030b0000
-#error "Python limited API support requires at least Python version 3.11"
+#if PY_VERSION_HEX < 0x030e0000
+#error "Python limited API support requires at least Python version 3.14"
 #endif
     ]],[[]])],
-    [AC_DEFINE(Py_LIMITED_API, 0x030b0000,
+    [AC_DEFINE(Py_LIMITED_API, 0x030e0000,
       [Define if GDB should be built against the Python limited C API.])
     ],[
-      AC_MSG_ERROR([Python limited API support requires at least Python version 3.11])
+      AC_MSG_ERROR([Python limited API support requires at least Python version 3.14])
     ])
   CFLAGS="$old_CFLAGS"
   CPPFLAGS="$old_CPPFLAGS"
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index 4031f925b04..ac0c40dcf45 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -341,7 +341,8 @@ archpy_repr (PyObject *self)
 
   auto arch_info = gdbarch_bfd_arch_info (gdbarch);
   return PyUnicode_FromFormat ("<%s arch_name=%s printable_name=%s>",
-			       Py_TYPE (self)->tp_name, arch_info->arch_name,
+			       gdbpy_py_obj_tp_name (self),
+			       arch_info->arch_name,
 			       arch_info->printable_name);
 }
 
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 6e4b662cc51..d9751d26378 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -522,7 +522,8 @@ blpy_repr (PyObject *self)
       if (++written_symbols < len)
 	str += ", ";
     }
-  return PyUnicode_FromFormat ("<%s %s {%s}>", Py_TYPE (self)->tp_name,
+  return PyUnicode_FromFormat ("<%s %s {%s}>",
+			       gdbpy_py_obj_tp_name (self),
 			       name, str.c_str ());
 }
 
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 03388812ff8..d96c67694b3 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1065,9 +1065,11 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
 static PyObject *
 bppy_repr (PyObject *self)
 {
+  const char *tp_name = gdbpy_py_obj_tp_name (self);
+
   const auto bp = (struct gdbpy_breakpoint_object*) self;
   if (bp->bp == nullptr)
-    return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+    return PyUnicode_FromFormat ("<%s (invalid)>", tp_name);
 
   std::string str = " ";
   if (bp->bp->thread != -1)
@@ -1079,7 +1081,7 @@ bppy_repr (PyObject *self)
   str.pop_back ();
 
   return PyUnicode_FromFormat ("<%s%s number=%d hits=%d%s>",
-			       Py_TYPE (self)->tp_name,
+			       tp_name,
 			       (bp->bp->enable_state == bp_enabled
 				? "" : " disabled"), bp->bp->number,
 			       bp->bp->hit_count, str.c_str ());
@@ -1771,7 +1773,8 @@ bplocpy_repr (PyObject *py_self)
       str += fn_name;
     }
 
-  return PyUnicode_FromFormat ("<%s %s>", Py_TYPE (self)->tp_name,
+  return PyUnicode_FromFormat ("<%s %s>",
+			       gdbpy_py_obj_tp_name (py_self),
 			       str.c_str ());
 }
 
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index 26cb6cdde57..a8bea4d832f 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -201,7 +201,7 @@ connpy_repr (PyObject *obj)
     return gdb_py_invalid_object_repr (obj);
 
   return PyUnicode_FromFormat ("<%s num=%d, what=\"%s\">",
-			       Py_TYPE (obj)->tp_name,
+			       gdbpy_py_obj_tp_name (obj),
 			       target->connection_number,
 			       make_target_connection_string (target).c_str ());
 }
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index d35838c7523..1daeabb5915 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -361,7 +361,7 @@ cfpy_repr (PyObject *self)
   bfd *core_bfd = get_inferior_core_bfd (obj->inferior);
   gdb_assert (core_bfd != nullptr);
   return PyUnicode_FromFormat ("<%s inferior=%d filename='%s'>",
-			       Py_TYPE (self)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       obj->inferior->num,
 			       bfd_get_filename (core_bfd));
 }
diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index 7635e45db56..1c5661dd307 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -304,7 +304,7 @@ disasmpy_info_repr (PyObject *self)
   const char *arch_name
     = (gdbarch_bfd_arch_info (obj->gdbarch))->printable_name;
   return PyUnicode_FromFormat ("<%s address=%s architecture=%s>",
-			       Py_TYPE (obj)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       core_addr_to_string_nz (obj->address),
 			       arch_name);
 }
@@ -995,7 +995,7 @@ disasmpy_result_init (PyObject *self, PyObject *args, PyObject *kwargs)
     {
       PyErr_Format (PyExc_ValueError,
 		    _("Cannot use 'string' and 'parts' when creating %s."),
-		    Py_TYPE (self)->tp_name);
+		    gdbpy_py_obj_tp_name (self));
       return -1;
     }
 
@@ -1079,7 +1079,7 @@ disasmpy_result_repr (PyObject *self)
   gdb_assert (obj->parts != nullptr);
 
   return PyUnicode_FromFormat ("<%s length=%d string=\"%U\">",
-			       Py_TYPE (obj)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       obj->length,
 			       disasmpy_result_str (self));
 }
@@ -1294,7 +1294,7 @@ gdbpy_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
       PyErr_Format
 	(PyExc_TypeError,
 	 _("Result from Disassembler must be gdb.DisassemblerResult, not %s."),
-	 Py_TYPE (result.get ())->tp_name);
+	 gdbpy_py_obj_tp_name (result.get ()));
       gdbpy_print_stack ();
       return std::optional<int> (-1);
     }
@@ -1381,8 +1381,9 @@ disasmpy_dealloc_result (PyObject *self)
 static int
 disasmpy_part_init (PyObject *self, PyObject *args, PyObject *kwargs)
 {
-  PyErr_SetString (PyExc_RuntimeError,
-		   _("Cannot create instances of DisassemblerPart."));
+  PyErr_Format (PyExc_RuntimeError,
+		_("Cannot create instances of %s."),
+		gdbpy_py_obj_tp_name (self));
   return -1;
 }
 
@@ -1419,7 +1420,7 @@ disasmpy_text_part_repr (PyObject *self)
   gdb_assert (obj->string != nullptr);
 
   return PyUnicode_FromFormat ("<%s string='%s', style='%s'>",
-			       Py_TYPE (obj)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       obj->string->c_str (),
 			       get_style_name (obj->style));
 }
@@ -1462,7 +1463,7 @@ disasmpy_addr_part_repr (PyObject *self)
   disasm_addr_part_object *obj = (disasm_addr_part_object *) self;
 
   return PyUnicode_FromFormat ("<%s address='%s'>",
-			       Py_TYPE (obj)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       core_addr_to_string_nz (obj->address));
 }
 
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 23d8eff661f..1420d2ac5b9 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -95,7 +95,7 @@ frapy_repr (PyObject *self)
 
   const frame_id &fid = frame_obj->frame_id;
   return PyUnicode_FromFormat ("<%s level=%d frame-id=%s>",
-			       Py_TYPE (self)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       frame_relative_level (f_info),
 			       fid.to_string ().c_str ());
 }
@@ -544,7 +544,7 @@ frapy_read_var (PyObject *self, PyObject *args, PyObject *kw)
     {
       PyErr_Format (PyExc_TypeError,
 		    _("argument 1 must be gdb.Symbol or str, not %s"),
-		    Py_TYPE (sym_obj)->tp_name);
+		    gdbpy_py_obj_tp_name (sym_obj));
       return NULL;
     }
 
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index 4adfaa71ae2..672546e106d 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -354,7 +354,7 @@ thpy_repr (PyObject *self)
 
   thread_info *thr = thread_obj->thread;
   return PyUnicode_FromFormat ("<%s id=%s target-id=\"%s\">",
-			       Py_TYPE (self)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       print_full_thread_id (thr),
 			       target_pid_to_str (thr->ptid).c_str ());
 }
diff --git a/gdb/python/py-mi.c b/gdb/python/py-mi.c
index 5189bda944e..27c79d60636 100644
--- a/gdb/python/py-mi.c
+++ b/gdb/python/py-mi.c
@@ -379,7 +379,7 @@ gdbpy_notify_mi (PyObject *self, PyObject *args, PyObject *kwargs)
       PyErr_Format
 	(PyExc_ValueError,
 	 _("MI notification data must be either None or a dictionary, not %s"),
-	 Py_TYPE (data)->tp_name);
+	 gdbpy_py_obj_tp_name (data));
       return nullptr;
     }
 
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
index ab06e197d84..6edea04ccb5 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -509,7 +509,7 @@ micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
     {
       PyErr_Format (PyExc_TypeError,
 		    _("gdb.MICommand.installed must be set to a bool, not %s"),
-		    newvalue == Py_None ? "None" : Py_TYPE(newvalue)->tp_name);
+		    newvalue == Py_None ? "None" : gdbpy_py_obj_tp_name (newvalue));
       return -1;
     }
 
diff --git a/gdb/python/py-obj-type.c b/gdb/python/py-obj-type.c
new file mode 100644
index 00000000000..ea0b59a8447
--- /dev/null
+++ b/gdb/python/py-obj-type.c
@@ -0,0 +1,74 @@
+/* Helpers related to Python object type
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "python-internal.h"
+#include "py-obj-type.h"
+
+/* Return the type's fully qualified name from a PyTypeObject.  */
+const char *
+gdb_py_tp_name (PyTypeObject *py_type) noexcept
+{
+#if PY_VERSION_HEX >= 0x030d0000
+  /* Note: PyType_GetFullyQualifiedName() was added in version 3.13, and is
+     part of the stable ABI since version 3.13.  */
+  PyObject *fully_qualified_name = PyType_GetFullyQualifiedName (py_type);
+  if (fully_qualified_name == nullptr)
+    return nullptr;
+
+  return PyUnicode_AsUTF8AndSize (fully_qualified_name, nullptr);
+
+#else /* PY_VERSION_HEX < 0x030d0000 && ! defined (Py_LIMITED_API)  */
+  /* For non-heap types, the fully qualified name corresponds to tp_name.  */
+  if (! (PyType_GetFlags (py_type) & Py_TPFLAGS_HEAPTYPE))
+    return py_type->tp_name;
+
+  /* In the absence of PyType_GetFullyQualifiedName(), we fallback using
+     __qualname__ instead. However, the result may differ slightly in some
+     cases, e.g. the module name may be missing.  */
+
+# if PY_VERSION_HEX >= 0x030b0000
+  /* Note: PyType_GetQualName() was added in version 3.11.  */
+  PyObject *qualname = PyType_GetQualName (py_type);
+  if (qualname == nullptr)
+    return nullptr;
+
+  return PyUnicode_AsUTF8AndSize (qualname, nullptr);
+
+# else
+  /* In the absence of PyType_GetQualName(), fallback on using PyHeapTypeObject
+     which is not part of the public API.
+     Tested on 3.10 which is the oldest supported version at the time of this
+     writing, i.e. February 2026.  Hopefully, this workaround should go away
+     when the minimum supported Python version is increased above 3.10.  */
+  PyHeapTypeObject *ht = (PyHeapTypeObject *) py_type;
+  if (ht->ht_qualname == nullptr)
+    return nullptr;
+
+  return PyUnicode_AsUTF8AndSize (ht->ht_qualname, nullptr);
+# endif
+#endif
+}
+
+/* Return the type's fully qualified name from a PyObject.  */
+const char *
+gdbpy_py_obj_tp_name (PyObject *self) noexcept
+{
+  /* Note: Py_TYPE () is part of the stable ABI since version 3.14.  */
+  return gdb_py_tp_name (Py_TYPE (self));
+}
diff --git a/gdb/python/py-obj-type.h b/gdb/python/py-obj-type.h
new file mode 100644
index 00000000000..293647fabfb
--- /dev/null
+++ b/gdb/python/py-obj-type.h
@@ -0,0 +1,29 @@
+/* Helpers related to Python object type
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_PYTHON_PY_OBJ_TYPE_H
+#define GDB_PYTHON_PY_OBJ_TYPE_H
+
+/* Return the type's fully qualified name from a PyTypeObject.  */
+extern const char *gdb_py_tp_name (PyTypeObject *py_type) noexcept;
+
+/* Return the type's fully qualified name from a PyObject.  */
+extern const char *gdbpy_py_obj_tp_name (PyObject *self) noexcept;
+
+#endif /* GDB_PYTHON_PY_OBJ_TYPE_H */
diff --git a/gdb/python/py-style.c b/gdb/python/py-style.c
index d149d90aaa9..60a9ea4792f 100644
--- a/gdb/python/py-style.c
+++ b/gdb/python/py-style.c
@@ -267,7 +267,7 @@ stylepy_init_from_parts (PyObject *self, PyObject *fg, PyObject *bg,
       PyErr_Format
 	(PyExc_TypeError,
 	 _("'foreground' argument must be gdb.Color or None, not %s."),
-	 Py_TYPE (fg)->tp_name);
+	 gdbpy_py_obj_tp_name (fg));
       return -1;
     }
 
@@ -276,7 +276,7 @@ stylepy_init_from_parts (PyObject *self, PyObject *fg, PyObject *bg,
       PyErr_Format
 	(PyExc_TypeError,
 	 _("'background' argument must be gdb.Color or None, not %s."),
-	 Py_TYPE (bg)->tp_name);
+	 gdbpy_py_obj_tp_name (bg));
       return -1;
     }
 
@@ -483,7 +483,7 @@ stylepy_set_foreground (PyObject *self, PyObject *newvalue, void *closure)
   if (!gdbpy_is_color (newvalue))
     {
       PyErr_Format (PyExc_TypeError, _("value must be gdb.Color, not %s"),
-		    Py_TYPE (newvalue)->tp_name);
+		    gdbpy_py_obj_tp_name (newvalue));
       return -1;
     }
 
@@ -541,7 +541,7 @@ stylepy_set_background (PyObject *self, PyObject *newvalue, void *closure)
   if (!gdbpy_is_color (newvalue))
     {
       PyErr_Format (PyExc_TypeError, _("value must be gdb.Color, not %s"),
-		    Py_TYPE (newvalue)->tp_name);
+		    gdbpy_py_obj_tp_name (newvalue));
       return -1;
     }
 
@@ -623,7 +623,7 @@ stylepy_set_intensity (PyObject *self, PyObject *newvalue, void *closure)
       PyErr_Format
 	(PyExc_TypeError,
 	 _("value must be a Long (a gdb.INTENSITY constant), not %s"),
-	 Py_TYPE (newvalue)->tp_name);
+	 gdbpy_py_obj_tp_name (newvalue));
       return -1;
     }
 
@@ -733,12 +733,12 @@ stylepy_repr (PyObject *self)
 
   if (style_obj->style_name == nullptr)
     return PyUnicode_FromFormat ("<%s fg=%s, bg=%s, intensity=%s>",
-				 Py_TYPE (self)->tp_name,
+				 gdbpy_py_obj_tp_name (self),
 				 fg_str.get (), bg_str.get (),
 				 intensity_str);
   else
     return PyUnicode_FromFormat ("<%s name='%s', fg=%s, bg=%s, intensity=%s>",
-				 Py_TYPE (self)->tp_name,
+				 gdbpy_py_obj_tp_name (self),
 				 style_obj->style_name, fg_str.get (),
 				 bg_str.get (), intensity_str);
 }
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index fe4d6dac000..e74c8e4b368 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -384,7 +384,8 @@ sympy_repr (PyObject *self)
   if (symbol == nullptr)
     return gdb_py_invalid_object_repr (self);
 
-  return PyUnicode_FromFormat ("<%s print_name=%s>", Py_TYPE (self)->tp_name,
+  return PyUnicode_FromFormat ("<%s print_name=%s>",
+			       gdbpy_py_obj_tp_name (self),
 			       symbol->print_name ());
 }
 
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index e1c57928b08..263d48a9365 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -1081,7 +1081,8 @@ typy_repr (PyObject *self)
   auto py_typename = PyUnicode_Decode (type_name.c_str (), type_name.size (),
 				       host_charset (), NULL);
 
-  return PyUnicode_FromFormat ("<%s code=%s name=%U>", Py_TYPE (self)->tp_name,
+  return PyUnicode_FromFormat ("<%s code=%s name=%U>",
+			       gdbpy_py_obj_tp_name (self),
 			       code, py_typename);
 }
 
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 9ffa382d093..dcf86f7db3d 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -247,7 +247,7 @@ unwind_infopy_repr (PyObject *self)
 
   if (frame == nullptr)
     return PyUnicode_FromFormat ("<%s for an invalid frame>",
-				 Py_TYPE (self)->tp_name);
+				 gdbpy_py_obj_tp_name (self));
 
   std::string saved_reg_names;
   struct gdbarch *gdbarch = pending_frame->gdbarch;
@@ -262,7 +262,7 @@ unwind_infopy_repr (PyObject *self)
     }
 
   return PyUnicode_FromFormat ("<%s frame #%d, saved_regs=(%s)>",
-			       Py_TYPE (self)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       frame_relative_level (frame),
 			       saved_reg_names.c_str ());
 }
@@ -456,7 +456,7 @@ pending_framepy_repr (PyObject *self)
     }
 
   return PyUnicode_FromFormat ("<%s level=%d, sp=%s, pc=%s>",
-			       Py_TYPE (self)->tp_name,
+			       gdbpy_py_obj_tp_name (self),
 			       frame_relative_level (frame),
 			       sp_str,
 			       pc_str);
@@ -924,7 +924,7 @@ frame_unwind_python::sniff (const frame_info_ptr &this_frame,
   gdb_assert (pyo_unwind_info != nullptr);
   if (!PyObject_TypeCheck (pyo_unwind_info, &unwind_info_object_type))
     error (_("an Unwinder should return gdb.UnwindInfo, not %s."),
-	   Py_TYPE (pyo_unwind_info)->tp_name);
+	   gdbpy_py_obj_tp_name (pyo_unwind_info));
 
   {
     unwind_info_object *unwind_info =
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 484fc4611b7..96f1cb1ee31 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -362,7 +362,7 @@ gdb_py_generic_getattro (PyObject *self, PyObject *attr)
      Therefore, we must explicitly raise an AttributeError in this case.  */
   PyErr_Format (PyExc_AttributeError,
 		"'%s' object has no attribute '%s'",
-		Py_TYPE (self)->tp_name,
+		gdbpy_py_obj_tp_name (self),
 		PyUnicode_AsUTF8AndSize (attr, nullptr));
   return nullptr;
 }
@@ -700,5 +700,5 @@ gdbpy_fix_doc_string_indentation (gdb::unique_xmalloc_ptr<char> doc)
 PyObject *
 gdb_py_invalid_object_repr (PyObject *self)
 {
-  return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
+  return PyUnicode_FromFormat ("<%s (invalid)>", gdbpy_py_obj_tp_name (self));
 }
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index b24b9314994..37bc37691fe 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -59,6 +59,7 @@
 #include <Python.h>
 #include <frameobject.h>
 #include "py-ref.h"
+#include "py-obj-type.h"
 
 static_assert (PY_VERSION_HEX >= 0x03040000);
 
@@ -1126,12 +1127,13 @@ gdbpy_type_ready (PyTypeObject *type, PyObject *mod = nullptr)
 {
   if (PyType_Ready (type) < 0)
     return -1;
+  const char *tp_name = gdb_py_tp_name (type);
   if (mod == nullptr)
     {
-      gdb_assert (startswith (type->tp_name, "gdb."));
+      gdb_assert (startswith (tp_name, "gdb."));
       mod = gdb_module;
     }
-  const char *dot = strrchr (type->tp_name, '.');
+  const char *dot = strrchr (tp_name, '.');
   gdb_assert (dot != nullptr);
   return gdb_pymodule_addobject (mod, dot + 1, (PyObject *) type);
 }
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 6e13ab5bb3f..4182c699cb5 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1579,7 +1579,7 @@ gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
       PyErr_Format
 	(PyExc_TypeError,
 	 _("'style' argument must be gdb.Style or None, not %s."),
-	 Py_TYPE (style_obj)->tp_name);
+	 gdbpy_py_obj_tp_name (style_obj));
       return nullptr;
     }
 
diff --git a/gdb/testsuite/gdb.python/py-disasm.exp.tcl b/gdb/testsuite/gdb.python/py-disasm.exp.tcl
index 07131a44bff..e4391fa59ce 100644
--- a/gdb/testsuite/gdb.python/py-disasm.exp.tcl
+++ b/gdb/testsuite/gdb.python/py-disasm.exp.tcl
@@ -124,7 +124,7 @@ set test_plans \
 	 [list "" "${base_pattern}\r\n.*"] \
 	 [list "GlobalNullDisassembler" "${base_pattern}\r\n.*"] \
 	 [list "ShowInfoRepr" "${base_pattern}\\s+## <gdb.disassembler.DisassembleInfo address=$hex architecture=\[^>\]+>\r\n.*"] \
-	 [list "ShowInfoSubClassRepr" "${base_pattern}\\s+## <MyInfo address=$hex architecture=\[^>\]+>\r\n.*"] \
+	 [list "ShowInfoSubClassRepr" "${base_pattern}\\s+## <ShowInfoSubClassRepr.MyInfo address=$hex architecture=\[^>\]+>\r\n.*"] \
 	 [list "ShowResultRepr" "${base_pattern}\\s+## <gdb.disassembler.DisassemblerResult length=$decimal string=\"\[^\r\n\]+\">\r\n.*"] \
 	 [list "ShowResultStr" "${base_pattern}\\s+## ${nop}\r\n.*"] \
 	 [list "GlobalPreInfoDisassembler" "${base_pattern}\\s+## ad = $hex, ar = ${curr_arch}\r\n.*"] \
@@ -154,10 +154,10 @@ set test_plans \
 		   "Buffer returned from read_memory is sized $decimal instead of the expected $decimal"]] \
 	 [list "ResultOfWrongType" \
 	      [make_exception_pattern "TypeError" \
-		   "Result from Disassembler must be gdb.DisassemblerResult, not Blah."]] \
+		   "Result from Disassembler must be gdb.DisassemblerResult, not ResultOfWrongType.Blah."]] \
 	 [list "ResultOfVeryWrongType" \
 	      [make_exception_pattern "TypeError" \
-		   "Result from Disassembler must be gdb.DisassemblerResult, not Blah."]] \
+		   "Result from Disassembler must be gdb.DisassemblerResult, not ResultOfVeryWrongType.Blah."]] \
 	 [list "ErrorCreatingTextPart_NoArgs" \
 	      [make_exception_pattern "TypeError" \
 		   [missing_arg_pattern "style" 1]]] \
@@ -337,7 +337,7 @@ foreach len {0 -1} {
 foreach type {DisassemblerTextPart DisassemblerAddressPart} {
     gdb_test "python result = gdb.disassembler.${type}()" \
 	[multi_line \
-	     "RuntimeError.*: Cannot create instances of DisassemblerPart\\." \
+	     "RuntimeError.*: Cannot create instances of gdb.disassembler.${type}\\." \
 	     "Error occurred in Python.*"] \
 	 "try to create an instance of ${type}"
 }
diff --git a/gdb/testsuite/gdb.python/py-unwind.exp b/gdb/testsuite/gdb.python/py-unwind.exp
index 8c90da50a1d..784d9dfcb64 100644
--- a/gdb/testsuite/gdb.python/py-unwind.exp
+++ b/gdb/testsuite/gdb.python/py-unwind.exp
@@ -249,7 +249,7 @@ with_test_prefix "bad object unwinder" {
     gdb_test_no_output "python obj = bad_object_unwinder(\"bad-object\")"
     gdb_test_no_output "python gdb.unwinder.register_unwinder(None, obj, replace=True)"
     gdb_test "backtrace" \
-	"Python Exception <class 'gdb.error'>: an Unwinder should return gdb.UnwindInfo, not Blah\\.\r\n.*"
+	"Python Exception <class 'gdb.error'>: an Unwinder should return gdb.UnwindInfo, not bad_object_unwinder.+Blah\\.\r\n.*"
 }
 
 # Gather information about every frame.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.