diff options
| author | Peter LaFosse <peter@vector35.com> | 2021-11-05 22:11:51 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2021-11-05 22:12:03 -0400 |
| commit | 719ec9860158123d229dd729993834c66b13d8c9 (patch) | |
| tree | 6f5d37c61eca79f8d8682115cd6939e17bf68bdd /python/binaryview.py | |
| parent | afe2b869b0bc8ea2b4969163899d495d46c85fef (diff) | |
Turn some apis back into Lists for ease of use, improve __repr__ for list and mapping containers
Diffstat (limited to 'python/binaryview.py')
| -rw-r--r-- | python/binaryview.py | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/python/binaryview.py b/python/binaryview.py index e44d9559..88642e23 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -1058,29 +1058,25 @@ class Segment: return core.BNSegmentIsAutoDefined(self.handle) @property - def relocation_ranges(self) -> Generator[Tuple[int, int], None, None]: + def relocation_ranges(self) -> List[Tuple[int, int]]: """List of relocation range tuples (read-only)""" count = ctypes.c_ulonglong() ranges = core.BNSegmentGetRelocationRanges(self.handle, count) assert ranges is not None, "core.BNSegmentGetRelocationRanges returned None" - try: - for i in range(0, count.value): - yield (ranges[i].start, ranges[i].end) + return [(ranges[i].start, ranges[i].end) for i in range(count.value)] finally: core.BNFreeRelocationRanges(ranges, count) - def relocation_ranges_at(self, addr:int) -> Generator[Tuple[int, int], None, None]: + def relocation_ranges_at(self, addr:int) -> List[Tuple[int, int]]: """List of relocation range tuples (read-only)""" count = ctypes.c_ulonglong() ranges = core.BNSegmentGetRelocationRangesAtAddress(self.handle, addr, count) assert ranges is not None, "core.BNSegmentGetRelocationRangesAtAddress returned None" - try: - for i in range(0, count.value): - yield (ranges[i].start, ranges[i].end) + return [(ranges[i].start, ranges[i].end) for i in range(count.value)] finally: core.BNFreeRelocationRanges(ranges, count) @@ -1278,6 +1274,16 @@ class _BinaryViewAssociatedDataStore(associateddatastore._AssociatedDataStore): class SymbolMapping(collections.abc.Mapping): # type: ignore + """ + SymbolMapping object is used to improve performance of the `bv.symbols` API. + This allows pythonic code like this to have reasonable performance characteristics + + >>> my_symbols = get_my_symbols() + >>> for symbol in my_symbols: + >>> if bv.symbols[symbol].address == 0x41414141: + >>> print("Found") + + """ def __init__(self, view:'BinaryView'): self._symbol_list = None self._count = None @@ -1285,6 +1291,9 @@ class SymbolMapping(collections.abc.Mapping): # type: ignore self._view = view self._n = 0 + def __repr__(self): + return f"<SymbolMapping {len(self)} symbols: {self._symbol_cache}>" + def __del__(self): if core is not None and self._symbol_list is not None: core.BNFreeSymbolList(self._symbol_list, len(self)) @@ -1362,12 +1371,25 @@ class SymbolMapping(collections.abc.Mapping): # type: ignore class TypeMapping(collections.abc.Mapping): # type: ignore + """ + TypeMapping object is used to improve performance of the `bv.types` API. + This allows pythonic code like this to have reasonable performance characteristics + + >>> my_types = get_my_types() + >>> for type_name in my_types: + >>> if bv.types[type_name].width == 4: + >>> print("Found") + + """ def __init__(self, view:'BinaryView'): self._type_list = None self._count = None self._type_cache:Optional[Mapping[_types.QualifiedName, _types.Type]] = None self._view = view + def __repr__(self): + return f"<TypeMapping {len(self)} symbols: {self._type_cache}>" + def __del__(self): if core is not None and self._type_list is not None: core.BNFreeTypeList(self._type_list, len(self)) @@ -2358,11 +2380,8 @@ class BinaryView: count = ctypes.c_ulonglong() ranges = core.BNGetRelocationRanges(self.handle, count) assert ranges is not None, "core.BNGetRelocationRanges returned None" - result = [] try: - for i in range(0, count.value): - result.append((ranges[i].start, ranges[i].end)) - return result + return [(ranges[i].start, ranges[i].end) for i in range(count.value)] finally: core.BNFreeRelocationRanges(ranges, count) @@ -2372,11 +2391,8 @@ class BinaryView: count = ctypes.c_ulonglong() ranges = core.BNGetRelocationRangesAtAddress(self.handle, addr, count) assert ranges is not None, "core.BNGetRelocationRangesAtAddress returned None" - result = [] try: - for i in range(0, count.value): - result.append((ranges[i].start, ranges[i].end)) - return result + return [(ranges[i].start, ranges[i].end) for i in range(count.value)] finally: core.BNFreeRelocationRanges(ranges, count) |
