From f49ae00f6d15a81876788df15f1678619a4c791d Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Mon, 8 Aug 2022 21:49:30 -0400 Subject: Un-deprecate BinaryView.symbols(), and raise KeyError like a real dict --- python/binaryview.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'python/binaryview.py') diff --git a/python/binaryview.py b/python/binaryview.py index 0870aff3..fe182925 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -1509,7 +1509,10 @@ class SymbolMapping(collections.abc.Mapping): # type: ignore def __getitem__(self, key): if self._symbol_cache is None: - return self._view.get_symbols_by_raw_name(key) + sym = self._view.get_symbols_by_raw_name(key) + if len(sym) == 0: + raise KeyError(f"'{key}': symbol not found") + return sym else: return self._symbol_cache[key] @@ -1556,7 +1559,7 @@ class SymbolMapping(collections.abc.Mapping): # type: ignore def __contains__(self, value): try: - self[value] + _ = self[value] return True except KeyError: return False @@ -2398,13 +2401,21 @@ class BinaryView: return _function.Function(self, func) @property - @decorators.deprecated def symbols(self) -> SymbolMapping: """ Dict of symbols (read-only) + Items in the dict are lists of all symbols matching that name. + :Example: + + >>> bv.symbols['_main'] + [] + >>> list(bv.symbols) + ['_start', '_main', '_printf', '_scanf', ...] + >>> bv.symbols['foo'] + KeyError: "'foo': symbol not found" - .. warning:: This method **should not** be used in any applications where speed is important as it \ - copies all symbols from the binaryview each time it is invoked. + :return: Dict-like generator of symbol names and values + :rtype: Generator[str, None, None] """ return SymbolMapping(self) -- cgit v1.3.1