diff --git a/distutils/_collections.py b/distutils/_collections.py index 7daff55e..98fce800 100644 --- a/distutils/_collections.py +++ b/distutils/_collections.py @@ -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, @@ -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 @@ -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) @@ -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)))