Skip to content

Commit

Permalink
Add __contains__ convenience impl for Backend (#437)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Magin <[email protected]>
  • Loading branch information
fmagin and fmagin authored Nov 8, 2023
1 parent 0c6753e commit 73f40a0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cle/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ def __setstate__(self, state):
for sym in self.symbols:
sym.owner = self

def __contains__(self, thing: Union[int]) -> bool:
"""
This serves two purposes:
1. It's slightly more convenient than writing self.min_addr <= thing < self.max_addr yourself
2. If a Backend implements some form of __getitem__ that always returns False for an integer, running
`0 in backend` will run into an infinite loop. This prevents that, by just defining sensible semantics for `in`
This could also be extended to other types, in the future, if it makes sense.
"""

if isinstance(thing, int):
return self.min_addr <= thing < self.max_addr
raise ValueError("Unsupported type %s for containment check" % type(thing))


ALL_BACKENDS: Dict[str, Type[Backend]] = {}

Expand Down

0 comments on commit 73f40a0

Please sign in to comment.