diff options
author | Andrew Burgess <aburgess@redhat.com> | 2023-01-24 15:12:34 +0000 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2023-05-12 18:24:24 +0100 |
commit | 6a667807390b7091b5ec088d979c779f65d3dfa5 (patch) | |
tree | 46e7b61bd054ee66836a41b01a66127d3727270b /gdb/python/py-disasm.c | |
parent | 15ccb5e393f7c4032a8ee005a8183830a9c6accb (diff) | |
download | binutils-gdb-6a667807390b7091b5ec088d979c779f65d3dfa5.tar.gz |
gdb/python: implement DisassemblerResult.__str__ method
Add the DisassemblerResult.__str__ method. This gives the same result
as the DisassemblerResult.string attribute, but can be useful
sometimes depending on how the user is trying to print the object.
There's a test for the new functionality.
Diffstat (limited to 'gdb/python/py-disasm.c')
-rw-r--r-- | gdb/python/py-disasm.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c index eeb16d8650c..9c281f6e089 100644 --- a/gdb/python/py-disasm.c +++ b/gdb/python/py-disasm.c @@ -608,6 +608,21 @@ gdbpy_disassembler::read_memory_func (bfd_vma memaddr, gdb_byte *buff, return 0; } +/* Implement __str__ for the DisassemblerResult type. */ + +static PyObject * +disasmpy_result_str (PyObject *self) +{ + disasm_result_object *obj = (disasm_result_object *) self; + + gdb_assert (obj->content != nullptr); + gdb_assert (obj->content->size () > 0); + gdb_assert (obj->length > 0); + return PyUnicode_Decode (obj->content->c_str (), + obj->content->size (), + host_charset (), nullptr); +} + /* Implement DisassemblerResult.length attribute, return the length of the disassembled instruction. */ @@ -624,14 +639,7 @@ disasmpy_result_length (PyObject *self, void *closure) static PyObject * disasmpy_result_string (PyObject *self, void *closure) { - disasm_result_object *obj = (disasm_result_object *) self; - - gdb_assert (obj->content != nullptr); - gdb_assert (obj->content->size () > 0); - gdb_assert (obj->length > 0); - return PyUnicode_Decode (obj->content->c_str (), - obj->content->size (), - host_charset (), nullptr); + return disasmpy_result_str (self); } /* Implement DisassemblerResult.__init__. Takes two arguments, an @@ -1147,7 +1155,7 @@ PyTypeObject disasm_result_object_type = { 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ - 0, /*tp_str*/ + disasmpy_result_str, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ |