Skip to content

Commit

Permalink
Update DictStack implementation from jaraco.collections 3.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jan 5, 2022
1 parent 1560a1f commit 788cc15
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions distutils/_collections.py
Expand Up @@ -2,7 +2,7 @@
import itertools


# from jaraco.collections 3.5
# from jaraco.collections 3.5.1
class DictStack(list, collections.abc.Mapping):
"""
A stack of dictionaries that behaves as a view on those dictionaries,
Expand All @@ -15,6 +15,8 @@ class DictStack(list, collections.abc.Mapping):
2
>>> stack['c']
2
>>> len(stack)
3
>>> stack.push(dict(a=3))
>>> stack['a']
3
Expand All @@ -40,7 +42,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 @@ -49,3 +51,6 @@ def __getitem__(self, key):

def __contains__(self, other):
return collections.abc.Mapping.__contains__(self, other)

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

0 comments on commit 788cc15

Please sign in to comment.