From 0af2f233330024e0e9b4697d510c7030e518e64c Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Mon, 23 Jan 2023 15:31:28 +0000 Subject: gdb/python: rework how the disassembler API reads the result object This commit is a refactor ahead of the next change which will make disassembler styling available through the Python API. Unfortunately, in order to make the styling support available, I think the easiest solution is to make a very small change to the existing API. The current API relies on returning a DisassemblerResult object to represent each disassembled instruction. Currently GDB allows the DisassemblerResult class to be sub-classed, which could mean that a user tries to override the various attributes that exist on the DisassemblerResult object. This commit removes this ability, effectively making the DisassemblerResult class final. Though this is a change to the existing API, I'm hoping this isn't going to cause too many issues: - The Python disassembler API was only added in the previous release of GDB, so I don't expect it to be widely used yet, and - It's not clear to me why a user would need to sub-class the DisassemblerResult type, I allowed it in the original patch because at the time I couldn't see any reason to NOT allow it. Having prevented sub-classing I can now rework the tail end of the gdbpy_print_insn function; instead of pulling the results out of the DisassemblerResult object by calling back into Python, I now cast the Python object back to its C++ type (disasm_result_object), and access the fields directly from there. In later commits I will be reworking the disasm_result_object type in order to hold information about the styled disassembler output. The tests that dealt with sub-classing DisassemblerResult have been removed, and a new test that confirms that DisassemblerResult can't be sub-classed has been added. Reviewed-By: Eli Zaretskii Reviewed-By: Tom Tromey --- gdb/testsuite/gdb.python/py-disasm.py | 37 ----------------------------------- 1 file changed, 37 deletions(-) (limited to 'gdb/testsuite/gdb.python/py-disasm.py') diff --git a/gdb/testsuite/gdb.python/py-disasm.py b/gdb/testsuite/gdb.python/py-disasm.py index 435a3bf..17a7e75 100644 --- a/gdb/testsuite/gdb.python/py-disasm.py +++ b/gdb/testsuite/gdb.python/py-disasm.py @@ -274,43 +274,6 @@ class ResultOfWrongType(TestDisassembler): return self.Blah(1, "ABC") -class ResultWrapper(gdb.disassembler.DisassemblerResult): - def __init__(self, length, string, length_x=None, string_x=None): - super().__init__(length, string) - if length_x is None: - self.__length = length - else: - self.__length = length_x - if string_x is None: - self.__string = string - else: - self.__string = string_x - - @property - def length(self): - return self.__length - - @property - def string(self): - return self.__string - - -class ResultWithInvalidLength(TestDisassembler): - """Return a result object with an invalid length.""" - - def disassemble(self, info): - result = gdb.disassembler.builtin_disassemble(info) - return ResultWrapper(result.length, result.string, 0) - - -class ResultWithInvalidString(TestDisassembler): - """Return a result object with an empty string.""" - - def disassemble(self, info): - result = gdb.disassembler.builtin_disassemble(info) - return ResultWrapper(result.length, result.string, None, "") - - class TaggingDisassembler(TestDisassembler): """A simple disassembler that just tags the output.""" -- cgit v1.1