Skip to content

Commit

Permalink
Fix issues with DictStack length and item access on PyPy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jan 5, 2022
1 parent 13965b7 commit 9d39238
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v3.5.1
======

Fixed ``DictStack.__len__`` and addressed recursion error on
PyPy in ``__getitem__``.

v3.5.0
======

Expand Down
7 changes: 6 additions & 1 deletion jaraco/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ class DictStack(list, collections.abc.Mapping):
2
>>> stack['c']
2
>>> len(stack)
3
>>> stack.push(dict(a=3))
>>> stack['a']
3
Expand All @@ -598,7 +600,7 @@ def __iter__(self):
return iter(set(itertools.chain.from_iterable(c.keys() for c in dicts)))

def __getitem__(self, key):
for scope in reversed(self):
for scope in reversed(tuple(list.__iter__(self))):
if key in scope:
return scope[key]
raise KeyError(key)
Expand All @@ -608,6 +610,9 @@ def __getitem__(self, key):
def __contains__(self, other):
return collections.abc.Mapping.__contains__(self, other)

def __len__(self):
return len(list(iter(self)))


class BijectiveMap(dict):
"""
Expand Down

0 comments on commit 9d39238

Please sign in to comment.