Skip to content

Commit 55ee8af

Browse files
committed
Compatibility with PyPy environments as requested by @smuuf
1 parent 08ef09a commit 55ee8af

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

circular_dict/CircularDict.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ class is an implementation of the OrderedDict from the collections module and is
1515
from collections import OrderedDict
1616
from typing import Any, Optional, Tuple
1717

18+
# To make sure it is compatible with PyPy environments (PyPy does not have sys.getsizeof for all types)
19+
try:
20+
sys.getsizeof("Exist Check")
21+
getsizeof_available = True
22+
except TypeError:
23+
sys.getsizeof = lambda x: 0
24+
getsizeof_available = False
25+
26+
1827
class CircularDict(OrderedDict):
1928
"""
2029
A dictionary that operates as a circular buffer, removing the oldest item when either maxlen or
@@ -34,6 +43,11 @@ def __init__(self, maxlen: Optional[int] = None, maxsize_bytes: Optional[int] =
3443
:param kwargs: Keyword arguments passed to OrderedDict.
3544
"""
3645
assert maxlen is not None or maxsize_bytes is not None, "Either maxlen or maxsize must be set"
46+
if not getsizeof_available:
47+
assert maxsize_bytes is None, ("sys.getsizeof is not available for all types in this environment. "
48+
"That's common on, for example PyPy environments."
49+
"maxsize_bytes cannot be used. Set it to None.")
50+
3751
self.maxlen = maxlen
3852
self.maxsize_bytes = maxsize_bytes
3953
self.current_size = 0

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='circular-dict',
5-
version='1.7',
5+
version='1.8',
66
author='Eric-Canas',
77
author_email='[email protected]',
88
url='https://github.com/Eric-Canas/CircularDict',

0 commit comments

Comments
 (0)