#include "defs.h"
#include "py-instruction.h"
-/* See py-instruction.h. */
+/* Python type object for the abstract gdb.Instruction class. This class
+ contains getters for four elements: "pc" (int), "data" (buffer), "decode"
+ (str) and "size" (int) that must be overridden by sub classes. */
PyTypeObject py_insn_type = {
PyVarObject_HEAD_INIT (NULL, 0)
{NULL}
};
+/* See py-instruction.h. */
+
+PyTypeObject *
+py_insn_get_insn_type ()
+{
+ if (py_insn_type.tp_new == nullptr)
+ {
+ py_insn_type.tp_new = PyType_GenericNew;
+ py_insn_type.tp_flags = Py_TPFLAGS_DEFAULT;
+ py_insn_type.tp_basicsize = sizeof (py_insn_obj);
+ py_insn_type.tp_name = "gdb.Instruction";
+ py_insn_type.tp_doc = "GDB instruction object";
+ py_insn_type.tp_getset = py_insn_getset;
+
+ if (PyType_Ready (&py_insn_type) < 0)
+ {
+ /* Reset the tp_new field so any subsequent calls to this
+ function will retry to make the type ready. */
+ py_insn_type.tp_new = nullptr;
+ return nullptr;
+ }
+ }
+
+ return &py_insn_type;
+}
+
/* Sets up the gdb.Instruction type. */
int
gdbpy_initialize_instruction (void)
{
- py_insn_type.tp_new = PyType_GenericNew;
- py_insn_type.tp_flags = Py_TPFLAGS_DEFAULT;
- py_insn_type.tp_basicsize = sizeof (py_insn_obj);
- py_insn_type.tp_name = "gdb.Instruction";
- py_insn_type.tp_doc = "GDB instruction object";
- py_insn_type.tp_getset = py_insn_getset;
-
- return PyType_Ready (&py_insn_type);
+ if (py_insn_get_insn_type () == nullptr)
+ return -1;
+ return 0;
}
#include "python-internal.h"
-/* Python type object for the abstract gdb.Instruction class. This class
- contains getters for four elements: "pc" (int), "data" (buffer), "decode"
- (str) and "size" (int) that must be overridden by sub classes. */
-extern PyTypeObject py_insn_type;
+/* Return a pointer to the py_insn_type object (see py-instruction.c), but
+ ensure that PyType_Ready has been called for the type first. If the
+ PyType_Ready call is successful then subsequent calls to this function
+ will not call PyType_Ready, the type pointer will just be returned.
+
+ If the PyType_Ready call is not successful then nullptr is returned and
+ subsequent calls to this function will call PyType_Ready again. */
+
+extern PyTypeObject *py_insn_get_insn_type ();
#endif /* PYTHON_PY_INSTRUCTION_H */
recpy_insn_type.tp_getset = recpy_insn_getset;
recpy_insn_type.tp_richcompare = recpy_element_richcompare;
recpy_insn_type.tp_hash = recpy_element_hash;
- recpy_insn_type.tp_base = &py_insn_type;
+ recpy_insn_type.tp_base = py_insn_get_insn_type ();
recpy_func_type.tp_new = PyType_GenericNew;
recpy_func_type.tp_flags = Py_TPFLAGS_DEFAULT;