mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534)
This commit is contained in:
parent
9f004634a2
commit
0be9ce305f
3 changed files with 18 additions and 1 deletions
|
@ -196,6 +196,22 @@ class TestChainMap(unittest.TestCase):
|
|||
('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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue