From 8f1bc5944b849f3ad0cbbd5972d5ab9948ed4d57 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Sat, 2 May 2020 11:01:35 -0400 Subject: Fix up equality operators and had hash operators --- python/lineardisassembly.py | 175 ++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 81 deletions(-) (limited to 'python/lineardisassembly.py') diff --git a/python/lineardisassembly.py b/python/lineardisassembly.py index 3e49f5ee..44f0aeb9 100644 --- a/python/lineardisassembly.py +++ b/python/lineardisassembly.py @@ -35,12 +35,12 @@ class LinearDisassemblyLine(object): self.block = block self.contents = contents - def __str__(self): - return str(self.contents) - def __repr__(self): return repr(self.contents) + def __str__(self): + return str(self.contents) + class LinearViewObjectIdentifier(object): def __init__(self, name, start = None, end = None): @@ -48,29 +48,8 @@ class LinearViewObjectIdentifier(object): self._start = start self._end = end - @property - def name(self): - return self._name - - @property - def address(self): - return self._start - - @property - def start(self): - return self._start - - @property - def end(self): - return self._end - - @property - def has_address(self): - return self._start is not None - - @property - def has_range(self): - return self._start is not None and self._end is not None + def __repr__(self): + return "" def __str__(self): if not self.has_address: @@ -79,8 +58,18 @@ class LinearViewObjectIdentifier(object): return "%s 0x%x-0x%x" % (self._name, self._start, self._end) return "%s 0x%x" % (self._name, self._start) - def __repr__(self): - return "" + def __eq__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return (self._name, self._start, self._end) == (other._name, other._start, other._end) + + def __ne__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return not (self == other) + + def __hash__(self): + return hash((self._name, self._start, self._end)) def _to_api_object(self, obj = None): if obj is None: @@ -102,6 +91,30 @@ class LinearViewObjectIdentifier(object): result.end = 0 return result + @property + def name(self): + return self._name + + @property + def address(self): + return self._start + + @property + def start(self): + return self._start + + @property + def end(self): + return self._end + + @property + def has_address(self): + return self._start is not None + + @property + def has_range(self): + return self._start is not None and self._end is not None + @classmethod def _from_api_object(cls, obj): if obj.type == LinearViewObjectIdentifierType.AddressLinearViewObject: @@ -121,6 +134,18 @@ class LinearViewObject(object): def __del__(self): core.BNFreeLinearViewObject(self.handle) + def __repr__(self): + return "" + + def __len__(self): + return self.end - self.start + + def __str__(self): + result = str(self.identifier) + if self._parent is not None: + result = str(self._parent) + "/" + result + return result + @property def first_child(self): result = core.BNGetFirstLinearViewObjectChild(self.handle) @@ -189,18 +214,6 @@ class LinearViewObject(object): def ordering_index_total(self): return core.BNGetLinearViewObjectOrderingIndexTotal(self.handle) - def __len__(self): - return self.end - self.start - - def __str__(self): - result = str(self.identifier) - if self._parent is not None: - result = str(self._parent) + "/" + result - return result - - def __repr__(self): - return "" - def child_for_address(self, addr): result = core.BNGetLinearViewObjectChildForAddress(self.handle, addr) if not result: @@ -323,6 +336,47 @@ class LinearViewCursor(object): def __del__(self): core.BNFreeLinearViewCursor(self.handle) + def __repr__(self): + return "" + + def __str__(self): + return str(self.current_object) + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) == 0 + + def __ne__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) != 0 + + def __lt__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) < 0 + + def __le__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) <= 0 + + def __gt__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) > 0 + + def __ge__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) >= 0 + + def __cmp__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return LinearViewCursor.compare(self, other) + @property def before_begin(self): return core.BNIsLinearViewCursorBeforeBegin(self.handle) @@ -431,47 +485,6 @@ class LinearViewCursor(object): def duplicate(self): return LinearViewCursor(None, handle = core.BNDuplicateLinearViewCursor(self.handle)) - def __str__(self): - return str(self.current_object) - - def __repr__(self): - return "" - - def __eq__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) == 0 - return False - - def __ne__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) != 0 - return True - - def __lt__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) < 0 - return False - - def __le__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) <= 0 - return False - - def __gt__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) > 0 - return False - - def __ge__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) >= 0 - return False - - def __cmp__(self, other): - if isinstance(other, LinearViewCursor): - return LinearViewCursor.compare(self, other) - return 0 - @classmethod def compare(cls, a, b): return core.BNCompareLinearViewCursors(a.handle, b.handle) -- cgit v1.3.1