]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Automatically add types to Python modules
authorTom Tromey <tromey@adacore.com>
Wed, 11 Sep 2024 16:35:20 +0000 (10:35 -0600)
committerTom Tromey <tromey@adacore.com>
Mon, 23 Sep 2024 19:44:59 +0000 (13:44 -0600)
PR python/32163 points out that various types provided by gdb are not
added to the gdb module, so they aren't available for interactive
inspection.  I think this is just an oversight.

This patch fixes the problem by introducing a new helper function that
both readies the type and then adds it to the appropriate module.  The
patch also poisons PyType_Ready, the idea being to avoid this bug in
the future.

v2:
* Fixed a bug in original patch in gdb.Architecture registration
* Added regression test for the types mentioned in the bug

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32163
Reviewed-By: Alexandra Petlanova Hajkova <ahajkova@redhat.com>
35 files changed:
gdb/python/py-arch.c
gdb/python/py-block.c
gdb/python/py-breakpoint.c
gdb/python/py-cmd.c
gdb/python/py-connection.c
gdb/python/py-disasm.c
gdb/python/py-event.c
gdb/python/py-event.h
gdb/python/py-evtregistry.c
gdb/python/py-finishbreakpoint.c
gdb/python/py-frame.c
gdb/python/py-function.c
gdb/python/py-inferior.c
gdb/python/py-infthread.c
gdb/python/py-instruction.c
gdb/python/py-lazy-string.c
gdb/python/py-linetable.c
gdb/python/py-membuf.c
gdb/python/py-micmd.c
gdb/python/py-objfile.c
gdb/python/py-param.c
gdb/python/py-prettyprint.c
gdb/python/py-progspace.c
gdb/python/py-record-btrace.c
gdb/python/py-record.c
gdb/python/py-registers.c
gdb/python/py-symbol.c
gdb/python/py-symtab.c
gdb/python/py-tui.c
gdb/python/py-type.c
gdb/python/py-unwind.c
gdb/python/py-value.c
gdb/python/python-internal.h
gdb/python/python.c
gdb/testsuite/gdb.python/python.exp

index 178efab4cee4e4136be0ca42d53b48e4224b8fb4..0eb4282b30e9b1c09b4929327937acd915c15749 100644 (file)
@@ -361,11 +361,7 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_arch (void)
 {
   arch_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&arch_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Architecture",
-                                (PyObject *) &arch_object_type);
+  return gdbpy_type_ready (&arch_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_arch);
index 62e93d55072d27b3746aade1b3def55fc71a32de..aeb9acb7260526b24b65adee3ff4b8f5131bbe95 100644 (file)
@@ -493,19 +493,14 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_blocks (void)
 {
   block_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&block_object_type) < 0)
+  if (gdbpy_type_ready (&block_object_type) < 0)
     return -1;
 
   block_syms_iterator_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&block_syms_iterator_object_type) < 0)
+  if (gdbpy_type_ready (&block_syms_iterator_object_type) < 0)
     return -1;
 
-  if (gdb_pymodule_addobject (gdb_module, "Block",
-                             (PyObject *) &block_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "BlockIterator",
-                                (PyObject *) &block_syms_iterator_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_blocks);
index 013c3fadabf1af8f7f2cec71d0d14ea2ccecadd7..a21ffac8392baa3aa16340a3c88e8ad217d9d913 100644 (file)
@@ -1114,7 +1114,7 @@ gdbpy_breakpoint_init_breakpoint_type ()
   if (breakpoint_object_type.tp_new == nullptr)
     {
       breakpoint_object_type.tp_new = PyType_GenericNew;
-      if (PyType_Ready (&breakpoint_object_type) < 0)
+      if (gdbpy_type_ready (&breakpoint_object_type) < 0)
        {
          /* Reset tp_new back to nullptr so future calls to this function
             will try calling PyType_Ready again.  */
@@ -1359,10 +1359,6 @@ gdbpy_initialize_breakpoints (void)
   if (!gdbpy_breakpoint_init_breakpoint_type ())
     return -1;
 
-  if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
-                             (PyObject *) &breakpoint_object_type) < 0)
-    return -1;
-
   gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created,
                                             "py-breakpoint");
   gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted,
@@ -1394,14 +1390,7 @@ gdbpy_initialize_breakpoints (void)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_breakpoint_locations ()
 {
-  if (PyType_Ready (&breakpoint_location_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "BreakpointLocation",
-                             (PyObject *) &breakpoint_location_object_type)
-      < 0)
-    return -1;
-  return 0;
+  return gdbpy_type_ready (&breakpoint_location_object_type);
 }
 
 \f
index e042f20fa0d9698244b938d6aa2f0ee1f24fce82..731a94d965583e55e6717ca0ff2394b331bf569c 100644 (file)
@@ -557,7 +557,7 @@ gdbpy_initialize_commands (void)
   int i;
 
   cmdpy_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&cmdpy_object_type) < 0)
+  if (gdbpy_type_ready (&cmdpy_object_type) < 0)
     return -1;
 
   /* Note: alias and user are special.  */
@@ -587,10 +587,6 @@ gdbpy_initialize_commands (void)
        return -1;
     }
 
-  if (gdb_pymodule_addobject (gdb_module, "Command",
-                             (PyObject *) &cmdpy_object_type) < 0)
-    return -1;
-
   invoke_cst = PyUnicode_FromString ("invoke");
   if (invoke_cst == NULL)
     return -1;
index 79e27677442fd01c5525f8bad129e94cc5e1ab54..7f5cce3d48350b8ca6d16e3794eb1b8bdc75347b 100644 (file)
@@ -287,18 +287,10 @@ connpy_get_connection_details (PyObject *self, void *closure)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_connection (void)
 {
-  if (PyType_Ready (&connection_object_type) < 0)
+  if (gdbpy_type_ready (&connection_object_type) < 0)
     return -1;
 
-  if (gdb_pymodule_addobject (gdb_module, "TargetConnection",
-                             (PyObject *) &connection_object_type) < 0)
-    return -1;
-
-  if (PyType_Ready (&remote_connection_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "RemoteTargetConnection",
-                             (PyObject *) &remote_connection_object_type) < 0)
+  if (gdbpy_type_ready (&remote_connection_object_type) < 0)
     return -1;
 
   return 0;
index 9b9b509748d963abd8a9686a2bd5547f2bb6b5ae..8adf28ca025dfafdf179a3646e981409c358bc25 100644 (file)
@@ -1665,45 +1665,23 @@ gdbpy_initialize_disasm ()
     }
 
   disasm_info_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&disasm_info_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassembleInfo",
-                             (PyObject *) &disasm_info_object_type) < 0)
+  if (gdbpy_type_ready (&disasm_info_object_type, gdb_disassembler_module) < 0)
     return -1;
 
   disasm_result_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&disasm_result_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassemblerResult",
-                             (PyObject *) &disasm_result_object_type) < 0)
+  if (gdbpy_type_ready (&disasm_result_object_type, gdb_disassembler_module) < 0)
     return -1;
 
   disasm_part_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&disasm_part_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassemblerPart",
-                             (PyObject *) &disasm_part_object_type) < 0)
+  if (gdbpy_type_ready (&disasm_part_object_type, gdb_disassembler_module) < 0)
     return -1;
 
   disasm_addr_part_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&disasm_addr_part_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_disassembler_module,
-                             "DisassemblerAddressPart",
-                             (PyObject *) &disasm_addr_part_object_type) < 0)
+  if (gdbpy_type_ready (&disasm_addr_part_object_type, gdb_disassembler_module) < 0)
     return -1;
 
   disasm_text_part_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&disasm_text_part_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_disassembler_module,
-                             "DisassemblerTextPart",
-                             (PyObject *) &disasm_text_part_object_type) < 0)
+  if (gdbpy_type_ready (&disasm_text_part_object_type, gdb_disassembler_module) < 0)
     return -1;
 
   return 0;
index 47a2997a09a30ea5198a5694f488eb10e0bf8040..a918136cccaaf1f3a7e28caf4c86aecb0bfdacf7 100644 (file)
@@ -56,25 +56,9 @@ evpy_add_attribute (PyObject *event, const char *name, PyObject *attr)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_event (void)
 {
-  return gdbpy_initialize_event_generic (&event_object_type,
-                                        "Event");
+  return gdbpy_type_ready (&event_object_type);
 }
 
-/* Initialize the given event type.  If BASE is not NULL it will
-  be set as the types base.
-  Returns 0 if initialization was successful -1 otherwise.  */
-
-int
-gdbpy_initialize_event_generic (PyTypeObject *type,
-                               const char *name)
-{
-  if (PyType_Ready (type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, name, (PyObject *) type);
-}
-
-
 /* Notify the list of listens that the given EVENT has occurred.
    returns 0 if emit is successful -1 otherwise.  */
 
index 388c513e5e9b5f2b2172c9657f5df7a40fc4adc7..a7238241f6842454e326487c83a56d3642198035 100644 (file)
@@ -84,7 +84,5 @@ extern void evpy_dealloc (PyObject *self);
 extern int evpy_add_attribute (PyObject *event,
                               const char *name, PyObject *attr)
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
-int gdbpy_initialize_event_generic (PyTypeObject *type, const char *name)
-  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 #endif /* PYTHON_PY_EVENT_H */
index 1f486e28c92e551abac967cff1baf4bd9ea1d7a7..7ae3997ec9ed6c8082b5699be36179656cad5cb4 100644 (file)
@@ -104,11 +104,7 @@ evregpy_dealloc (PyObject *self)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_eventregistry (void)
 {
-  if (PyType_Ready (&eventregistry_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "EventRegistry",
-                                (PyObject *) &eventregistry_object_type);
+  return gdbpy_type_ready (&eventregistry_object_type);
 }
 
 /* Return the number of listeners currently connected to this
index ed3c4a5d90e6b29814554918a6a69ad14b422b4b..d9f7c895fe7dd83ab15eb45814c6240bcf93abf3 100644 (file)
@@ -439,11 +439,7 @@ gdbpy_initialize_finishbreakpoints (void)
   if (!gdbpy_breakpoint_init_breakpoint_type ())
     return -1;
 
-  if (PyType_Ready (&finish_breakpoint_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
-                             (PyObject *) &finish_breakpoint_object_type) < 0)
+  if (gdbpy_type_ready (&finish_breakpoint_object_type) < 0)
     return -1;
 
   gdb::observers::normal_stop.attach (bpfinishpy_handle_stop,
index 7f617fa5cead3df2b21e6e11d23a364074e3a685..fef4640d4edfe8d2660accb8b9dbd00bbf13a756 100644 (file)
@@ -761,7 +761,7 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_frames (void)
 {
   frame_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&frame_object_type) < 0)
+  if (gdbpy_type_ready (&frame_object_type) < 0)
     return -1;
 
   /* Note: These would probably be best exposed as class attributes of
@@ -785,8 +785,7 @@ gdbpy_initialize_frames (void)
 #include "unwind_stop_reasons.def"
 #undef SET
 
-  return gdb_pymodule_addobject (gdb_module, "Frame",
-                                (PyObject *) &frame_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_frames);
index 2bbfb9d3a6dc96973c678539e1012bc1b5f8d32b..58ae0d058e2bbb43239da770a4efbcddb4fa6e8f 100644 (file)
@@ -137,11 +137,7 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_functions (void)
 {
   fnpy_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&fnpy_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Function",
-                                (PyObject *) &fnpy_object_type);
+  return gdbpy_type_ready (&fnpy_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_functions);
index ccc3e881702ab628bd8e676d9643fe39aba4bff7..e5a74c153a89dbf85f9cab05f27ec9633a9a27f6 100644 (file)
@@ -1009,11 +1009,7 @@ gdbpy_selected_inferior (PyObject *self, PyObject *args)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_inferior (void)
 {
-  if (PyType_Ready (&inferior_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "Inferior",
-                             (PyObject *) &inferior_object_type) < 0)
+  if (gdbpy_type_ready (&inferior_object_type) < 0)
     return -1;
 
   gdb::observers::new_thread.attach (add_thread_object, "py-inferior");
index a17f25ed49853ee2716c1290b9868cff5e901341..09f493abeecddf18f8e3ac770d057a414d988c56 100644 (file)
@@ -412,11 +412,7 @@ gdbpy_selected_thread (PyObject *self, PyObject *args)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_thread (void)
 {
-  if (PyType_Ready (&thread_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "InferiorThread",
-                                (PyObject *) &thread_object_type);
+  return gdbpy_type_ready (&thread_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread);
index bc3945af2cf0d23de72285927457b5d020aa8ba6..7d775721ad6266c8f77c17e8b7c511acedd599a5 100644 (file)
@@ -66,7 +66,7 @@ py_insn_get_insn_type ()
       py_insn_type.tp_doc = "GDB instruction object";
       py_insn_type.tp_getset = py_insn_getset;
 
-      if (PyType_Ready (&py_insn_type) < 0)
+      if (gdbpy_type_ready (&py_insn_type) < 0)
        {
          /* Reset the tp_new field so any subsequent calls to this
             function will retry to make the type ready.  */
index 8779716c4b914e17ae461d0b5f18dce80733cb0e..67b7a33dab28a65e413be00ea13e54a0c6d62f6a 100644 (file)
@@ -236,11 +236,7 @@ gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_lazy_string (void)
 {
-  if (PyType_Ready (&lazy_string_object_type) < 0)
-    return -1;
-
-  Py_INCREF (&lazy_string_object_type);
-  return 0;
+  return gdbpy_type_ready (&lazy_string_object_type);
 }
 
 /* Determine whether the printer object pointed to by OBJ is a
index e3e71f9e43672208e57823140680dc27e844ae17..8c0a6cc91fcd37498087f1fb4d499389ca969b5b 100644 (file)
@@ -287,27 +287,11 @@ ltpy_dealloc (PyObject *self)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_linetable (void)
 {
-  if (PyType_Ready (&linetable_object_type) < 0)
+  if (gdbpy_type_ready (&linetable_object_type) < 0)
     return -1;
-  if (PyType_Ready (&linetable_entry_object_type) < 0)
+  if (gdbpy_type_ready (&linetable_entry_object_type) < 0)
     return -1;
-  if (PyType_Ready (&ltpy_iterator_object_type) < 0)
-    return -1;
-
-  Py_INCREF (&linetable_object_type);
-  Py_INCREF (&linetable_entry_object_type);
-  Py_INCREF (&ltpy_iterator_object_type);
-
-  if (gdb_pymodule_addobject (gdb_module, "LineTable",
-                             (PyObject *) &linetable_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "LineTableEntry",
-                             (PyObject *) &linetable_entry_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "LineTableIterator",
-                             (PyObject *) &ltpy_iterator_object_type) < 0)
+  if (gdbpy_type_ready (&ltpy_iterator_object_type) < 0)
     return -1;
 
   return 0;
index af48d01b9edd172bb0ff04b79290e8fcf3ac3a14..25ebc99bb221f639679f39bb99290b277c2fd9a5 100644 (file)
@@ -102,11 +102,7 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_membuf (void)
 {
   membuf_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&membuf_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Membuf",
-                                (PyObject *) &membuf_object_type);
+  return gdbpy_type_ready (&membuf_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_membuf);
index 54427d4633b192b5d73965a7c66f44e813f894c3..f4abf2b1cd0baecdc9fd9523f3a035159878a196 100644 (file)
@@ -447,12 +447,7 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_micommands ()
 {
   micmdpy_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&micmdpy_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "MICommand",
-                             (PyObject *) &micmdpy_object_type)
-      < 0)
+  if (gdbpy_type_ready (&micmdpy_object_type) < 0)
     return -1;
 
   invoke_cst = PyUnicode_FromString ("invoke");
index 6e8d5b57692a0a07decac3632dd3f367f4e0c6c5..d8c7631ee15917d3fe67166f44eed347769801bf 100644 (file)
@@ -709,11 +709,7 @@ objfile_to_objfile_object (struct objfile *objfile)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_objfile (void)
 {
-  if (PyType_Ready (&objfile_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Objfile",
-                                (PyObject *) &objfile_object_type);
+  return gdbpy_type_ready (&objfile_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_objfile);
index e7032f7758c15ddb50d916992927152eb19054f5..2567061cad2d5263e55f12d27c953787eddadc4c 100644 (file)
@@ -909,7 +909,7 @@ gdbpy_initialize_parameters (void)
   int i;
 
   parmpy_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&parmpy_object_type) < 0)
+  if (gdbpy_type_ready (&parmpy_object_type) < 0)
     return -1;
 
   set_doc_cst = PyUnicode_FromString ("set_doc");
@@ -927,8 +927,7 @@ gdbpy_initialize_parameters (void)
        return -1;
     }
 
-  return gdb_pymodule_addobject (gdb_module, "Parameter",
-                                (PyObject *) &parmpy_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_parameters);
index 368b3a3c793f4899c385d837fdddf15916e9c5ae..e061ea1b361459d22a972d9790a841976c276c04 100644 (file)
@@ -836,10 +836,7 @@ PyTypeObject printer_object_type =
 static int
 gdbpy_initialize_prettyprint ()
 {
-  if (PyType_Ready (&printer_object_type) < 0)
-    return -1;
-  return gdb_pymodule_addobject (gdb_module, "ValuePrinter",
-                                (PyObject *) &printer_object_type);
+  return gdbpy_type_ready (&printer_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_prettyprint);
index 5bc0015d7288dbe3e1abb5ece6fb2320882ca4ca..081d6320cc1b58d0c1d6ea7aba4134c9f33ca6a4 100644 (file)
@@ -747,11 +747,10 @@ gdbpy_initialize_pspace (void)
   gdb::observers::free_program_space.attach (gdbpy_free_program_space_event,
                                             "py-progspace");
 
-  if (PyType_Ready (&pspace_object_type) < 0)
+  if (gdbpy_type_ready (&pspace_object_type) < 0)
     return -1;
 
-  return gdb_pymodule_addobject (gdb_module, "Progspace",
-                                (PyObject *) &pspace_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_pspace);
index 30d0e8305db9bd069dabdcc25cfeb634fb355153..55ee67efd81305d26a331f539d876cb145f12aa7 100644 (file)
@@ -1006,7 +1006,7 @@ gdbpy_initialize_btrace (void)
 
   btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
 
-  return PyType_Ready (&btpy_list_type);
+  return gdbpy_type_ready (&btpy_list_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_btrace);
index 759bf3049cb82f09dc9ef41c23598c58998b3b46..83cfa66ad660174c5a246e8be1d4d2616eaf7c06 100644 (file)
@@ -653,11 +653,11 @@ gdbpy_initialize_record (void)
   recpy_aux_type.tp_richcompare = recpy_element_richcompare;
   recpy_aux_type.tp_hash = recpy_element_hash;
 
-  if (PyType_Ready (&recpy_record_type) < 0
-      || PyType_Ready (&recpy_insn_type) < 0
-      || PyType_Ready (&recpy_func_type) < 0
-      || PyType_Ready (&recpy_gap_type) < 0
-      || PyType_Ready (&recpy_aux_type) < 0)
+  if (gdbpy_type_ready (&recpy_record_type) < 0
+      || gdbpy_type_ready (&recpy_insn_type) < 0
+      || gdbpy_type_ready (&recpy_func_type) < 0
+      || gdbpy_type_ready (&recpy_gap_type) < 0
+      || gdbpy_type_ready (&recpy_aux_type) < 0)
     return -1;
   else
     return 0;
index f03274c308a160ec46e0358cf604233399a2d7a0..229dd62a826aac2beb2bf65770e6bd0c1ba73a87 100644 (file)
@@ -430,35 +430,22 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_registers ()
 {
   register_descriptor_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&register_descriptor_object_type) < 0)
-    return -1;
-  if (gdb_pymodule_addobject
-      (gdb_module, "RegisterDescriptor",
-       (PyObject *) &register_descriptor_object_type) < 0)
+  if (gdbpy_type_ready (&register_descriptor_object_type) < 0)
     return -1;
 
   reggroup_iterator_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&reggroup_iterator_object_type) < 0)
-    return -1;
-  if (gdb_pymodule_addobject
-      (gdb_module, "RegisterGroupsIterator",
-       (PyObject *) &reggroup_iterator_object_type) < 0)
+  if (gdbpy_type_ready (&reggroup_iterator_object_type) < 0)
     return -1;
 
   reggroup_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&reggroup_object_type) < 0)
-    return -1;
-  if (gdb_pymodule_addobject
-      (gdb_module, "RegisterGroup",
-       (PyObject *) &reggroup_object_type) < 0)
+  if (gdbpy_type_ready (&reggroup_object_type) < 0)
     return -1;
 
   register_descriptor_iterator_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&register_descriptor_iterator_object_type) < 0)
+  if (gdbpy_type_ready (&register_descriptor_iterator_object_type) < 0)
     return -1;
-  return (gdb_pymodule_addobject
-         (gdb_module, "RegisterDescriptorIterator",
-          (PyObject *) &register_descriptor_iterator_object_type));
+
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_registers);
index 754420f3b5643c4e75e55d34c151b87ef7cb1258..1c3a67546b5696d7980c96346652206066ea10e6 100644 (file)
@@ -640,7 +640,7 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_symbols (void)
 {
-  if (PyType_Ready (&symbol_object_type) < 0)
+  if (gdbpy_type_ready (&symbol_object_type) < 0)
     return -1;
 
   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
@@ -685,8 +685,7 @@ gdbpy_initialize_symbols (void)
 #include "sym-domains.def"
 #undef SYM_DOMAIN
 
-  return gdb_pymodule_addobject (gdb_module, "Symbol",
-                                (PyObject *) &symbol_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_symbols);
index 7290b8564789640067eaab6cbcda56372380d451..99a5094ba6075d913dd90861014164434adbf980 100644 (file)
@@ -512,19 +512,14 @@ static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_symtabs (void)
 {
   symtab_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&symtab_object_type) < 0)
+  if (gdbpy_type_ready (&symtab_object_type) < 0)
     return -1;
 
   sal_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&sal_object_type) < 0)
+  if (gdbpy_type_ready (&sal_object_type) < 0)
     return -1;
 
-  if (gdb_pymodule_addobject (gdb_module, "Symtab",
-                             (PyObject *) &symtab_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Symtab_and_line",
-                                (PyObject *) &sal_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_symtabs);
index 8ad2b7d973337bdeea0009f0869ab436350f35c8..3be0fb12d9c394334a7300580564cff26ae56ac2 100644 (file)
@@ -625,7 +625,7 @@ gdbpy_initialize_tui ()
 {
 #ifdef TUI
   gdbpy_tui_window_object_type.tp_new = PyType_GenericNew;
-  if (PyType_Ready (&gdbpy_tui_window_object_type) < 0)
+  if (gdbpy_type_ready (&gdbpy_tui_window_object_type) < 0)
     return -1;
 #endif /* TUI */
 
index c13b8610d37bc8ddee60e613989554409a5a7ac8..5e00b947e5692e976d781295daa3864bdb80f881 100644 (file)
@@ -1526,11 +1526,11 @@ gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_types (void)
 {
-  if (PyType_Ready (&type_object_type) < 0)
+  if (gdbpy_type_ready (&type_object_type) < 0)
     return -1;
-  if (PyType_Ready (&field_object_type) < 0)
+  if (gdbpy_type_ready (&field_object_type) < 0)
     return -1;
-  if (PyType_Ready (&type_iterator_object_type) < 0)
+  if (gdbpy_type_ready (&type_iterator_object_type) < 0)
     return -1;
 
   for (const auto &item : pyty_codes)
@@ -1539,16 +1539,7 @@ gdbpy_initialize_types (void)
        return -1;
     }
 
-  if (gdb_pymodule_addobject (gdb_module, "Type",
-                             (PyObject *) &type_object_type) < 0)
-    return -1;
-
-  if (gdb_pymodule_addobject (gdb_module, "TypeIterator",
-                             (PyObject *) &type_iterator_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Field",
-                                (PyObject *) &field_object_type);
+  return 0;
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_types);
index e36768ecd12b65dfdf36e1b59ada4f4bb19c612d..a10a7585cd750dc3d8be63bf9b7ef3f53aad38ad 100644 (file)
@@ -1002,17 +1002,13 @@ gdbpy_initialize_unwind (void)
 {
   gdb::observers::new_architecture.attach (pyuw_on_new_gdbarch, "py-unwind");
 
-  if (PyType_Ready (&pending_frame_object_type) < 0)
+  if (gdbpy_type_ready (&pending_frame_object_type) < 0)
     return -1;
-  int rc = gdb_pymodule_addobject (gdb_module, "PendingFrame",
-                                  (PyObject *) &pending_frame_object_type);
-  if (rc != 0)
-    return rc;
 
-  if (PyType_Ready (&unwind_info_object_type) < 0)
+  if (gdbpy_type_ready (&unwind_info_object_type) < 0)
     return -1;
-  return gdb_pymodule_addobject (gdb_module, "UnwindInfo",
-      (PyObject *) &unwind_info_object_type);
+
+  return 0;
 }
 
 void _initialize_py_unwind ();
index 37d5716b6d836c0faa14f1c2db0b36e11a4cbf60..98b82a9d295603d3776138a647fd97b6cbd69b57 100644 (file)
@@ -2215,11 +2215,7 @@ gdbpy_is_value_object (PyObject *obj)
 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
 gdbpy_initialize_values (void)
 {
-  if (PyType_Ready (&value_object_type) < 0)
-    return -1;
-
-  return gdb_pymodule_addobject (gdb_module, "Value",
-                                (PyObject *) &value_object_type);
+  return gdbpy_type_ready (&value_object_type);
 }
 
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_values);
index bf3ab67ea744dcc98dc5969c54518628de41a2a2..82680cdac0a84317831250ffef25b6e9e741f013 100644 (file)
@@ -1119,4 +1119,34 @@ extern std::optional<int> gdbpy_print_insn (struct gdbarch *gdbarch,
                                            CORE_ADDR address,
                                            disassemble_info *info);
 
+/* A wrapper for PyType_Ready that also automatically registers the
+   type in the appropriate module.  Returns 0 on success, -1 on error.
+   If MOD is supplied, then the type is added to that module.  If MOD
+   is not supplied, the type name (tp_name field) must be of the form
+   "gdb.Mumble", and the type will be added to the gdb module.  */
+
+static inline int
+gdbpy_type_ready (PyTypeObject *type, PyObject *mod = nullptr)
+{
+  if (PyType_Ready (type) < 0)
+    return -1;
+  if (mod == nullptr)
+    {
+      gdb_assert (startswith (type->tp_name, "gdb."));
+      mod = gdb_module;
+    }
+  const char *dot = strrchr (type->tp_name, '.');
+  gdb_assert (dot != nullptr);
+  return gdb_pymodule_addobject (mod, dot + 1, (PyObject *) type);
+}
+
+/* Poison PyType_Ready.  Only gdbpy_type_ready should be used, to
+   avoid forgetting to register the type.  See PR python/32163.  */
+#undef PyType_Ready
+#ifdef __GNUC__
+# pragma GCC poison PyType_Ready
+#else
+# define PyType_Ready POISONED_PyType_Ready
+#endif
+
 #endif /* PYTHON_PYTHON_INTERNAL_H */
index a2ce1f6545ae7d974de242c01c9916afbfdaeebd..405dc8281a93707537a2bf985d3d14576cd5ece9 100644 (file)
@@ -2316,7 +2316,7 @@ init_done:
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)     \
-  if (gdbpy_initialize_event_generic (&name##_event_object_type, py_name) < 0) \
+  if (gdbpy_type_ready (&name##_event_object_type) < 0) \
     return false;
 #include "py-event-types.def"
 #undef GDB_PY_DEFINE_EVENT_TYPE
index 175a6de7ca070a14a72ab9d0b486ed250abe2af5..e8eb9ec727bf65c262a52dc74564a5e4439adfe8 100644 (file)
@@ -561,3 +561,12 @@ if { [use_gdb_stub] == 0 } {
        }
     }
 }
+
+# Regression test for PR python/32163: several types were not
+# previously registered with the module, so could not be inspected
+# directly.
+foreach type {Instruction LazyString Membuf Record RecordFunctionSegment \
+                 RecordGap RecordInstruction TuiWindow} {
+    gdb_test "python print(type(gdb.$type))" "<class 'type'>" \
+       "gdb.$type is registered"
+}