Skip to content

Commit 788cc15

Browse files
committed
Update DictStack implementation from jaraco.collections 3.5.1
1 parent 1560a1f commit 788cc15

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

distutils/_collections.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import itertools
33

44

5-
# from jaraco.collections 3.5
5+
# from jaraco.collections 3.5.1
66
class DictStack(list, collections.abc.Mapping):
77
"""
88
A stack of dictionaries that behaves as a view on those dictionaries,
@@ -15,6 +15,8 @@ class DictStack(list, collections.abc.Mapping):
1515
2
1616
>>> stack['c']
1717
2
18+
>>> len(stack)
19+
3
1820
>>> stack.push(dict(a=3))
1921
>>> stack['a']
2022
3
@@ -40,7 +42,7 @@ def __iter__(self):
4042
return iter(set(itertools.chain.from_iterable(c.keys() for c in dicts)))
4143

4244
def __getitem__(self, key):
43-
for scope in reversed(self):
45+
for scope in reversed(tuple(list.__iter__(self))):
4446
if key in scope:
4547
return scope[key]
4648
raise KeyError(key)
@@ -49,3 +51,6 @@ def __getitem__(self, key):
4951

5052
def __contains__(self, other):
5153
return collections.abc.Mapping.__contains__(self, other)
54+
55+
def __len__(self):
56+
return len(list(iter(self)))

0 commit comments

Comments
 (0)