From 788cc159e4d734b972e22ccf06dbcd8ed8f94885 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 5 Jan 2022 18:29:29 -0500 Subject: [PATCH] Update DictStack implementation from jaraco.collections 3.5.1 --- distutils/_collections.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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)))