Compare commits

...

3 Commits

Author SHA1 Message Date
Raymond Hettinger 7f82f22eba
bpo-42501: Revise the usage note for Enums with the choices (GH-23563) 2020-11-30 09:55:13 -08:00
Terry Jan Reedy e41bfd15dd
bpo-42508: Remove bogus idlelib.pyshell.ModifiedInterpreter attribute (GH-23570)
restart_subprocess is a method of self, the pyshell.InteractiveInterpreter instance. The latter does not have an interp attribute redundantly referring to itself. (The PyShell instance does have an interp attribute, referring to the InteractiveInterpreter instance.)
2020-11-30 12:09:43 -05:00
Andreas Poehlmann 0be9ce305f
bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534) 2020-11-30 08:34:15 -08:00
5 changed files with 21 additions and 15 deletions

View File

@ -1133,20 +1133,9 @@ container should match the type_ specified::
Any container can be passed as the *choices* value, so :class:`list` objects,
:class:`set` objects, and custom containers are all supported.
This includes :class:`enum.Enum`, which could be used to restrain
argument's choices; if we reuse previous rock/paper/scissors game example,
this could be as follows::
>>> from enum import Enum
>>> class GameMove(Enum):
... ROCK = 'rock'
... PAPER = 'paper'
... SCISSORS = 'scissors'
...
>>> parser = argparse.ArgumentParser(prog='game.py')
>>> parser.add_argument('move', type=GameMove, choices=GameMove)
>>> parser.parse_args(['rock'])
Namespace(move=<GameMove.ROCK: 'rock'>)
Use of :class:`enum.Enum` is not recommended because it is difficult to
control its appearance in usage, help, and error messages.
required

View File

@ -1001,7 +1001,7 @@ class ChainMap(_collections_abc.MutableMapping):
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):

View File

@ -757,7 +757,7 @@ class ModifiedInterpreter(InteractiveInterpreter):
def runcode(self, code):
"Override base class method"
if self.tkconsole.executing:
self.interp.restart_subprocess()
self.restart_subprocess()
self.checklinecache()
debugger = self.debugger
try:

View File

@ -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))

View File

@ -0,0 +1 @@
ChainMap.__iter__ no longer calls __getitem__ on underlying maps