/* A gdb.Corefile object. */
-struct corefile_object
+struct corefile_object : public gdbpy_dict_wrapper
{
- PyObject_HEAD
-
/* The inferior this core file is attached to. This will be set to NULL
when the inferior is deleted, or if a different core file is loaded
for the inferior. When this is NULL the gdb.Corefile object is
considered invalid.*/
struct inferior *inferior;
- /* Dictionary holding user-added attributes. This is the __dict__
- attribute of the object. This is an owning reference. */
- PyObject *dict;
-
/* A Tuple of gdb.CorefileMappedFile objects. This tuple is only created
the first time the user calls gdb.Corefile.mapped_files(), the result
is cached here. If this pointer is not NULL then this is an owning
static gdb_PyGetSetDef corefile_getset[] =
{
- { "__dict__", gdb_py_generic_dict, nullptr,
- "The __dict__ for the gdb.Corefile.", &corefile_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, nullptr,
+ "The __dict__ for the gdb.Corefile.", nullptr },
{ "filename", cfpy_get_filename, nullptr,
"The filename of a valid Corefile object.", nullptr },
{ nullptr }
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
+ gdb_py_generic_getattro, /*tp_getattro*/
+ gdb_py_generic_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB corefile object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (corefile_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
static gdb_PyGetSetDef event_object_getset[] =
{
- { "__dict__", gdb_py_generic_dict, NULL,
- "The __dict__ for this event.", &event_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, NULL,
+ "The __dict__ for this event.", NULL },
{ NULL }
};
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
+ gdb_py_generic_getattro, /* tp_getattro */
+ gdb_py_generic_setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
"GDB event object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (event_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0 /* tp_alloc */
};
#include "py-event-types.def"
#undef GDB_PY_DEFINE_EVENT_TYPE
-struct event_object
-{
- PyObject_HEAD
-
- PyObject *dict;
-};
+struct event_object : public gdbpy_dict_wrapper
+{};
extern int emit_continue_event (ptid_t ptid);
extern int emit_exited_event (const LONGEST *exit_code, struct inferior *inf);
#include "progspace-and-thread.h"
#include "gdbsupport/unordered_map.h"
-using thread_map_t
- = gdb::unordered_map<thread_info *, gdbpy_ref<thread_object>>;
-
-struct inferior_object
-{
- PyObject_HEAD
-
- /* The inferior we represent. */
- struct inferior *inferior;
-
- /* thread_object instances under this inferior. This owns a
- reference to each object it contains. */
- thread_map_t *threads;
-
- /* Dictionary holding user-added attributes.
- This is the __dict__ attribute of the object. */
- PyObject *dict;
-};
-
extern PyTypeObject inferior_object_type;
/* Deleter to clean up when an inferior is removed. */
static gdb_PyGetSetDef inferior_object_getset[] =
{
- { "__dict__", gdb_py_generic_dict, nullptr,
- "The __dict__ for this inferior.", &inferior_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, nullptr,
+ "The __dict__ for this inferior.", nullptr },
{ "arguments", infpy_get_args, infpy_set_args,
"Arguments to this program.", nullptr },
{ "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL },
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
+ gdb_py_generic_getattro, /* tp_getattro */
+ gdb_py_generic_setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"GDB inferior object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (inferior_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0 /* tp_alloc */
};
static gdb_PyGetSetDef thread_object_getset[] =
{
- { "__dict__", gdb_py_generic_dict, nullptr,
- "The __dict__ for this thread.", &thread_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, nullptr,
+ "The __dict__ for this thread.", nullptr },
{ "name", thpy_get_name, thpy_set_name,
"The name of the thread, as set by the user or the OS.", NULL },
{ "details", thpy_get_details, NULL,
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
+ gdb_py_generic_getattro, /*tp_getattro*/
+ gdb_py_generic_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB thread object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (thread_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0 /* tp_alloc */
};
#include "python.h"
#include "inferior.h"
-struct objfile_object
+struct objfile_object : public gdbpy_dict_wrapper
{
- PyObject_HEAD
-
/* The corresponding objfile. */
struct objfile *objfile;
- /* Dictionary holding user-added attributes.
- This is the __dict__ attribute of the object. */
- PyObject *dict;
-
/* The pretty-printer list of functions. */
PyObject *printers;
static gdb_PyGetSetDef objfile_getset[] =
{
- { "__dict__", gdb_py_generic_dict, NULL,
- "The __dict__ for this objfile.", &objfile_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, NULL,
+ "The __dict__ for this objfile.", NULL },
{ "filename", objfpy_get_filename, NULL,
"The objfile's filename, or None.", NULL },
{ "username", objfpy_get_username, NULL,
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
+ gdb_py_generic_getattro, /*tp_getattro*/
+ gdb_py_generic_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB objfile object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (objfile_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
objfpy_new, /* tp_new */
#include "observable.h"
#include "inferior.h"
-struct pspace_object
+struct pspace_object : public gdbpy_dict_wrapper
{
- PyObject_HEAD
-
/* The corresponding pspace. */
struct program_space *pspace;
- /* Dictionary holding user-added attributes.
- This is the __dict__ attribute of the object. */
- PyObject *dict;
-
/* The pretty-printer list of functions. */
PyObject *printers;
static gdb_PyGetSetDef pspace_getset[] =
{
- { "__dict__", gdb_py_generic_dict, NULL,
- "The __dict__ for this progspace.", &pspace_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, NULL,
+ "The __dict__ for this progspace.", NULL },
{ "filename", pspy_get_filename, NULL,
"The filename of the progspace's main symbol file, or None.", nullptr },
{ "symbol_file", pspy_get_symbol_file, nullptr,
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
+ gdb_py_generic_getattro, /*tp_getattro*/
+ gdb_py_generic_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB progspace object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (pspace_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
template<typename T = PyObject> using gdbpy_ref
= gdb::ref_ptr<T, gdbpy_ref_policy<T>>;
+/* A wrapper class for Python extension objects that have a __dict__ attribute.
+
+ Any Python C object extension needing __dict__ should inherit from this
+ class. Given that the C extension object must also be convertible to
+ PyObject, this wrapper class publicly inherits from PyObject as well.
+
+ Access to the dict requires a custom getter defined via PyGetSetDef.
+ gdb_PyGetSetDef my_object_getset[] =
+ {
+ { "__dict__", gdb_py_generic_dict_getter, nullptr,
+ "The __dict__ for this object.", nullptr },
+ ...
+ { nullptr }
+ };
+
+ It is also important to note that __dict__ is used during the attribute
+ look-up. Since this dictionary is not managed by Python and is not exposed
+ via tp_dictoffset, custom attribute getter (tp_getattro) and setter
+ (tp_setattro) are required to correctly redirect attribute access to the
+ dictionary:
+ - gdb_py_generic_getattro (), assigned to tp_getattro for static types,
+ or Py_tp_getattro for heap-allocated types.
+ - gdb_py_generic_setattro (), assigned to tp_setattro for static types,
+ or Py_tp_setattro for heap-allocated types. */
+struct gdbpy_dict_wrapper : public PyObject
+{
+ /* Dictionary holding user-added attributes.
+ This is the __dict__ attribute of the object. */
+ PyObject *dict;
+
+ /* Compute the address of the __dict__ attribute for the given PyObject. */
+ static PyObject **compute_addr (PyObject *self)
+ {
+ auto *wrapper = reinterpret_cast<gdbpy_dict_wrapper *> (self);
+ return &wrapper->dict;
+ }
+};
+
#endif /* GDB_PYTHON_PY_REF_H */
extern PyTypeObject type_object_type;
/* A Field object. */
-struct field_object
-{
- PyObject_HEAD
-
- /* Dictionary holding our attributes. */
- PyObject *dict;
-};
+struct field_object : public gdbpy_dict_wrapper
+{};
extern PyTypeObject field_object_type;
static gdb_PyGetSetDef field_object_getset[] =
{
- { "__dict__", gdb_py_generic_dict, NULL,
- "The __dict__ for this field.", &field_object_type },
+ { "__dict__", gdb_py_generic_dict_getter, NULL,
+ "The __dict__ for this field.", NULL },
{ NULL }
};
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
+ gdb_py_generic_getattro, /*tp_getattro*/
+ gdb_py_generic_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB field object", /* tp_doc */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
- offsetof (field_object, dict), /* tp_dictoffset */
+ 0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
\f
-/* Generic implementation of the __dict__ attribute for objects that
- have a dictionary. The CLOSURE argument should be the type object.
- This only handles positive values for tp_dictoffset. */
+/* Generic implementation of the getter for the __dict__ attribute for objects
+ having a dictionary. The CLOSURE argument is unused. */
PyObject *
-gdb_py_generic_dict (PyObject *self, void *closure)
+gdb_py_generic_dict_getter (PyObject *self,
+ void *closure ATTRIBUTE_UNUSED)
{
- PyObject *result;
- PyTypeObject *type_obj = (PyTypeObject *) closure;
- char *raw_ptr;
+ PyObject **py_dict_ptr = gdbpy_dict_wrapper::compute_addr (self);
+ PyObject *py_dict = *py_dict_ptr;
+ if (py_dict == nullptr)
+ {
+ PyErr_SetString (PyExc_AttributeError,
+ "This object has no __dict__");
+ return nullptr;
+ }
+ return Py_NewRef (py_dict);
+}
- raw_ptr = (char *) self + type_obj->tp_dictoffset;
- result = * (PyObject **) raw_ptr;
+/* Generic attribute getter function similar to PyObject_GenericGetAttr () but
+ that should be used when the object has a dictionary __dict__. */
+PyObject *
+gdb_py_generic_getattro (PyObject *self, PyObject *attr)
+{
+ PyObject *value = PyObject_GenericGetAttr (self, attr);
+ if (value != nullptr)
+ return value;
+
+ if (! PyErr_ExceptionMatches (PyExc_AttributeError))
+ return nullptr;
+
+ gdbpy_ref<> dict (gdb_py_generic_dict_getter (self, nullptr));
+ if (dict == nullptr)
+ return nullptr;
+
+ /* Clear previous AttributeError set by PyObject_GenericGetAttr when it
+ did not find the attribute, and try to get the attribute from __dict__. */
+ PyErr_Clear();
+
+ value = PyDict_GetItemWithError (dict.get (), attr);
+ if (value != nullptr)
+ return Py_NewRef (value);
+
+ /* If PyDict_GetItemWithError() returns NULL because an error occurred, it
+ sets an exception. Propagate it by returning NULL. */
+ if (PyErr_Occurred () != nullptr)
+ return nullptr;
+
+ /* If the key is not found, PyDict_GetItemWithError() returns NULL without
+ setting an exception. Failing to set one here would later result in:
+ <class 'SystemError'>: error return without exception set
+ 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,
+ PyUnicode_AsUTF8AndSize (attr, nullptr));
+ return nullptr;
+}
- Py_INCREF (result);
- return result;
+/* Generic attribute setter function similar to PyObject_GenericSetAttr () but
+ that should be used when the object has a dictionary __dict__. */
+int
+gdb_py_generic_setattro (PyObject *self, PyObject *attr, PyObject *value)
+{
+ if (PyObject_GenericSetAttr (self, attr, value) == 0)
+ return 0;
+
+ if (! PyErr_ExceptionMatches (PyExc_AttributeError))
+ return -1;
+
+ gdbpy_ref<> dict (gdb_py_generic_dict_getter (self, nullptr));
+ if (dict == nullptr)
+ return -1;
+
+ /* Clear previous AttributeError set by PyObject_GenericGetAttr() when it
+ did not find the attribute, and try to set the attribute into __dict__. */
+ PyErr_Clear();
+
+ /* Set the new value.
+ Note: the old value is managed by PyDict_SetItem(), so no need to get
+ a borrowed reference on it and decrement its reference counter before
+ setting a new value. */
+ return PyDict_SetItem (dict.get (), attr, value);
}
+\f
+
/* Like PyModule_AddObject, but does not steal a reference to
OBJECT. */
#endif /* HAVE_LONG_LONG */
+#if PY_VERSION_HEX < 0x030a0000
+static inline PyObject *
+Py_NewRef (PyObject *obj)
+{
+ Py_INCREF (obj);
+ return obj;
+}
+#endif
+
/* A template variable holding the format character (as for
Py_BuildValue) for a given type. */
template<typename T>
extern gdbpy_breakpoint_object *bppy_pending_object;
-struct thread_object
+struct thread_object : public gdbpy_dict_wrapper
{
- PyObject_HEAD
-
/* The thread we represent. */
struct thread_info *thread;
/* The Inferior object to which this thread belongs. */
PyObject *inf_obj;
-
- /* Dictionary holding user-added attributes. This is the __dict__
- attribute of the object. */
- PyObject *dict;
};
-struct inferior_object;
+using thread_map_t
+ = gdb::unordered_map<thread_info *, gdbpy_ref<thread_object>>;
+
+struct inferior_object : public gdbpy_dict_wrapper
+{
+ /* The inferior we represent. */
+ struct inferior *inferior;
+
+ /* thread_object instances under this inferior. This owns a
+ reference to each object it contains. */
+ thread_map_t *threads;
+};
extern struct cmd_list_element *set_python_list;
extern struct cmd_list_element *show_python_list;
gdbpy_ref<> gdb_py_object_from_ulongest (ULONGEST l);
int gdb_py_int_as_long (PyObject *, long *);
-PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
+PyObject *gdb_py_generic_dict_getter (PyObject *self, void *closure);
+PyObject *gdb_py_generic_getattro (PyObject *self, PyObject *attr);
+int gdb_py_generic_setattro (PyObject *self, PyObject *attr, PyObject *value);
int gdb_pymodule_addobject (PyObject *mod, const char *name,
PyObject *object);