bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534)
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 150c2a1..a1ca958 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -196,6 +196,22 @@ def test_order_preservation(self):
('e', 55), ('f', 666), ('g', 777), ('h', 88888),
('i', 9999), ('j', 0)])
+ def test_iter_not_calling_getitem_on_maps(self):
+ class DictWithGetItem(UserDict):
+ def __init__(self, *args, **kwds):
+ self.called = False
+ UserDict.__init__(self, *args, **kwds)
+ def __getitem__(self, item):
+ self.called = True
+ UserDict.__getitem__(self, item)
+
+ d = DictWithGetItem(a=1)
+ c = ChainMap(d)
+ d.called = False
+
+ set(c) # iterate over chain map
+ self.assertFalse(d.called, '__getitem__ was called')
+
def test_dict_coercion(self):
d = ChainMap(dict(a=1, b=2), dict(b=20, c=30))
self.assertEqual(dict(d), dict(a=1, b=2, c=30))