Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0be9ce3

Browse filesBrowse files
authored
bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534)
1 parent 9f00463 commit 0be9ce3
Copy full SHA for 0be9ce3

File tree

Expand file treeCollapse file tree

3 files changed

+18
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+18
-1
lines changed

‎Lib/collections/__init__.py

Copy file name to clipboardExpand all lines: Lib/collections/__init__.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def __len__(self):
10011001
def __iter__(self):
10021002
d = {}
10031003
for mapping in reversed(self.maps):
1004-
d.update(mapping) # reuses stored hash values if possible
1004+
d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible
10051005
return iter(d)
10061006

10071007
def __contains__(self, key):

‎Lib/test/test_collections.py

Copy file name to clipboardExpand all lines: Lib/test/test_collections.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ def test_order_preservation(self):
196196
('e', 55), ('f', 666), ('g', 777), ('h', 88888),
197197
('i', 9999), ('j', 0)])
198198

199+
def test_iter_not_calling_getitem_on_maps(self):
200+
class DictWithGetItem(UserDict):
201+
def __init__(self, *args, **kwds):
202+
self.called = False
203+
UserDict.__init__(self, *args, **kwds)
204+
def __getitem__(self, item):
205+
self.called = True
206+
UserDict.__getitem__(self, item)
207+
208+
d = DictWithGetItem(a=1)
209+
c = ChainMap(d)
210+
d.called = False
211+
212+
set(c) # iterate over chain map
213+
self.assertFalse(d.called, '__getitem__ was called')
214+
199215
def test_dict_coercion(self):
200216
d = ChainMap(dict(a=1, b=2), dict(b=20, c=30))
201217
self.assertEqual(dict(d), dict(a=1, b=2, c=30))
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ChainMap.__iter__ no longer calls __getitem__ on underlying maps

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.