diff options
author | Andrew Burgess <aburgess@redhat.com> | 2024-01-04 16:46:40 +0000 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2024-01-12 11:21:30 +0000 |
commit | 13cd9bceea3c3a3081c463597146a11ade765e39 (patch) | |
tree | 8c2a7b96be594b13b55b320d17d89468303016a2 /gdb/python/py-inferior.c | |
parent | 2f47f48fe55eb72bfe4d3c5291a74f4a53121c5e (diff) | |
download | gdb-13cd9bceea3c3a3081c463597146a11ade765e39.zip gdb-13cd9bceea3c3a3081c463597146a11ade765e39.tar.gz gdb-13cd9bceea3c3a3081c463597146a11ade765e39.tar.bz2 |
gdb/python: Add gdb.Inferior.__dict__ attribute
The gdb.Objfile, gdb.Progspace, and gdb.Type Python types already have
a __dict__ attribute, which allows users to create user defined
attributes within the objects. This is useful if the user wants to
cache information within an object.
This commit adds the same functionality to the gdb.Inferior type.
After this commit there is a new gdb.Inferior.__dict__ attribute,
which is a dictionary. A user can, for example, do this:
(gdb) pi
>>> i = gdb.selected_inferior()
>>> i._user_attribute = 123
>>> i._user_attribute
123
>>>
There's a new test included.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python/py-inferior.c')
-rw-r--r-- | gdb/python/py-inferior.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 929d8bd..3f14bc3 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -46,6 +46,10 @@ struct inferior_object /* 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 @@ -241,6 +245,9 @@ inferior_to_inferior_object (struct inferior *inferior) inf_obj->inferior = inferior; inf_obj->threads = new thread_map_t (); + inf_obj->dict = PyDict_New (); + if (inf_obj->dict == nullptr) + return nullptr; /* PyObject_New initializes the new object with a refcount of 1. This counts for the reference we are keeping in the inferior data. */ @@ -981,6 +988,8 @@ infpy_dealloc (PyObject *obj) function is called. */ gdb_assert (inf_obj->inferior == nullptr); + Py_XDECREF (inf_obj->dict); + Py_TYPE (obj)->tp_free (obj); } @@ -1038,6 +1047,8 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_inferior); static gdb_PyGetSetDef inferior_object_getset[] = { + { "__dict__", gdb_py_generic_dict, nullptr, + "The __dict__ for this inferior.", &inferior_object_type }, { "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 }, @@ -1135,7 +1146,7 @@ PyTypeObject inferior_object_type = 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ + offsetof (inferior_object, dict), /* tp_dictoffset */ 0, /* tp_init */ 0 /* tp_alloc */ }; |