Skip to content

Commit

Permalink
Fix tests: PyPy does not implement __sizeof__ or getsizeof
Browse files Browse the repository at this point in the history
  • Loading branch information
awolverp committed Aug 3, 2024
1 parent 5facc73 commit 9d5f6a4
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions tests/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def __hash__(self) -> int:
return self.val


def getsizeof(obj, use_sys=True):
try:
if use_sys:
return sys.getsizeof(obj)
else:
return obj.__sizeof__()
except TypeError: # PyPy doesn't implement getsizeof or __sizeof__
return len(obj)


class _TestMixin:
CACHE: typing.Type[BaseCacheImpl]

Expand Down Expand Up @@ -94,7 +104,7 @@ def test___sizeof__(self):
# __sizeof__ returns exactly allocated memory size by cache
# but sys.getsizeof add also garbage collector overhead to that, so sometimes
# sys.getsizeof is greater than __sizeof__
assert sys.getsizeof(cache) >= cache.__sizeof__()
assert getsizeof(cache) >= getsizeof(cache, False)

def test___bool__(self):
cache = self.CACHE(1, **self.KWARGS, capacity=1)
Expand Down Expand Up @@ -205,21 +215,21 @@ def test_clear(self):
obj[2] = 2
assert 2 == len(obj)

cap = obj.__sizeof__()
cap = getsizeof(obj, False)
obj.clear(reuse=True)
assert 0 == len(obj)
assert obj.__sizeof__() >= cap
assert getsizeof(obj, False) >= cap

obj[1] = 1
obj[2] = 2
assert 2 == len(obj)

cap = obj.__sizeof__()
cap = getsizeof(obj, False)
obj.clear(reuse=False)
assert 0 == len(obj)
# this is not stable and
# may increases the capacity!
assert cap != obj.__sizeof__()
assert cap != getsizeof(obj, False)

def test_update(self):
obj = self.CACHE(2, **self.KWARGS, capacity=2)
Expand Down

0 comments on commit 9d5f6a4

Please sign in to comment.