diff options
author | Christian Biesinger <cbiesinger@google.com> | 2019-08-01 17:22:28 -0500 |
---|---|---|
committer | Christian Biesinger <cbiesinger@google.com> | 2019-08-05 13:06:18 -0500 |
commit | 0b27c27d0d3060171876736f05c39f9c86a65d3e (patch) | |
tree | e9ea50193b9ee06ba7468ee9bce4440343286c51 /gdb/python | |
parent | 4ee94178afbe062bc244d28595f86d63633a7af7 (diff) | |
download | gdb-0b27c27d0d3060171876736f05c39f9c86a65d3e.zip gdb-0b27c27d0d3060171876736f05c39f9c86a65d3e.tar.gz gdb-0b27c27d0d3060171876736f05c39f9c86a65d3e.tar.bz2 |
Add block['var'] accessor
Currently we support iteration on blocks; this patch extends that to make
subscript access work as well.
gdb/ChangeLog:
2019-08-05 Christian Biesinger <cbiesinger@google.com>
* NEWS: Mention dictionary access on blocks.
* python/py-block.c (blpy_getitem): New function.
(block_object_as_mapping): New struct.
(block_object_type): Use new struct for tp_as_mapping field.
gdb/doc/ChangeLog:
2019-08-05 Christian Biesinger <cbiesinger@google.com>
* python.texi (Blocks In Python): Document dictionary access on blocks.
gdb/testsuite/ChangeLog:
2019-08-05 Christian Biesinger <cbiesinger@google.com>
* gdb.python/py-block.exp: Test dictionary access on blocks.
Diffstat (limited to 'gdb/python')
-rw-r--r-- | gdb/python/py-block.c | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index 90140eb..4dc47ff 100644 --- a/gdb/python/py-block.c +++ b/gdb/python/py-block.c @@ -224,6 +224,43 @@ blpy_is_static (PyObject *self, void *closure) Py_RETURN_FALSE; } +/* Given a string, returns the gdb.Symbol representing that symbol in this + block. If such a symbol does not exist, returns NULL with a Python + exception. */ + +static PyObject * +blpy_getitem (PyObject *self, PyObject *key) +{ + const struct block *block; + + BLPY_REQUIRE_VALID (self, block); + + gdb::unique_xmalloc_ptr<char> name = python_string_to_host_string (key); + if (name == nullptr) + return nullptr; + + lookup_name_info lookup_name (name.get(), symbol_name_match_type::FULL); + + /* We use ALL_BLOCK_SYMBOLS_WITH_NAME instead of block_lookup_symbol so + that we can look up symbols irrespective of the domain, matching the + iterator. It would be confusing if the iterator returns symbols you + can't find via getitem. */ + struct block_iterator iter; + struct symbol *sym = nullptr; + ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) + { + /* Just stop at the first match */ + break; + } + + if (sym == nullptr) + { + PyErr_SetObject (PyExc_KeyError, key); + return nullptr; + } + return symbol_to_symbol_object (sym); +} + static void blpy_dealloc (PyObject *obj) { @@ -440,6 +477,12 @@ static gdb_PyGetSetDef block_object_getset[] = { { NULL } /* Sentinel */ }; +static PyMappingMethods block_object_as_mapping = { + NULL, + blpy_getitem, + NULL +}; + PyTypeObject block_object_type = { PyVarObject_HEAD_INIT (NULL, 0) "gdb.Block", /*tp_name*/ @@ -453,7 +496,7 @@ PyTypeObject block_object_type = { 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ + &block_object_as_mapping, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ 0, /*tp_str*/ |