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

[3.9] bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534) #23569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__it…
…er__ (GH-23534)

(cherry picked from commit 0be9ce3)

Co-authored-by: Andreas Poehlmann <andreas@poehlmann.io>
  • Loading branch information
ap-- authored and miss-islington committed Nov 30, 2020
commit 3df7f26c9b569856e5ba8e0bce33f8427c68063a
2 changes: 1 addition & 1 deletion 2 Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ def __len__(self):
def __iter__(self):
d = {}
for mapping in reversed(self.maps):
d.update(mapping) # reuses stored hash values if possible
d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible
return iter(d)

def __contains__(self, key):
Expand Down
16 changes: 16 additions & 0 deletions 16 Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,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))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ChainMap.__iter__ no longer calls __getitem__ on underlying maps
Morty Proxy This is a proxified and sanitized view of the page, visit original site.