Skip to content

Commit bcb3591

Browse files
committed
Update collections/__init__.py to 3.12.2
1 parent 5dc6d55 commit bcb3591

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

Lib/collections/__init__.py

+31-14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
else:
4646
_collections_abc.MutableSequence.register(deque)
4747

48+
try:
49+
from _collections import _deque_iterator
50+
except ImportError:
51+
pass
52+
4853
try:
4954
from _collections import defaultdict
5055
except ImportError:
@@ -94,17 +99,19 @@ class OrderedDict(dict):
9499
# Individual links are kept alive by the hard reference in self.__map.
95100
# Those hard references disappear when a key is deleted from an OrderedDict.
96101

102+
def __new__(cls, /, *args, **kwds):
103+
"Create the ordered dict object and set up the underlying structures."
104+
self = dict.__new__(cls)
105+
self.__hardroot = _Link()
106+
self.__root = root = _proxy(self.__hardroot)
107+
root.prev = root.next = root
108+
self.__map = {}
109+
return self
110+
97111
def __init__(self, other=(), /, **kwds):
98112
'''Initialize an ordered dictionary. The signature is the same as
99113
regular dictionaries. Keyword argument order is preserved.
100114
'''
101-
try:
102-
self.__root
103-
except AttributeError:
104-
self.__hardroot = _Link()
105-
self.__root = root = _proxy(self.__hardroot)
106-
root.prev = root.next = root
107-
self.__map = {}
108115
self.__update(other, **kwds)
109116

110117
def __setitem__(self, key, value,
@@ -271,7 +278,7 @@ def __repr__(self):
271278
'od.__repr__() <==> repr(od)'
272279
if not self:
273280
return '%s()' % (self.__class__.__name__,)
274-
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
281+
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))
275282

276283
def __reduce__(self):
277284
'Return state information for pickling'
@@ -511,9 +518,12 @@ def __getnewargs__(self):
511518
# specified a particular module.
512519
if module is None:
513520
try:
514-
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
515-
except (AttributeError, ValueError):
516-
pass
521+
module = _sys._getframemodulename(1) or '__main__'
522+
except AttributeError:
523+
try:
524+
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
525+
except (AttributeError, ValueError):
526+
pass
517527
if module is not None:
518528
result.__module__ = module
519529

@@ -1015,8 +1025,8 @@ def __len__(self):
10151025

10161026
def __iter__(self):
10171027
d = {}
1018-
for mapping in reversed(self.maps):
1019-
d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible
1028+
for mapping in map(dict.fromkeys, reversed(self.maps)):
1029+
d |= mapping # reuses stored hash values if possible
10201030
return iter(d)
10211031

10221032
def __contains__(self, key):
@@ -1136,10 +1146,17 @@ def __delitem__(self, key):
11361146
def __iter__(self):
11371147
return iter(self.data)
11381148

1139-
# Modify __contains__ to work correctly when __missing__ is present
1149+
# Modify __contains__ and get() to work like dict
1150+
# does when __missing__ is present.
11401151
def __contains__(self, key):
11411152
return key in self.data
11421153

1154+
def get(self, key, default=None):
1155+
if key in self:
1156+
return self[key]
1157+
return default
1158+
1159+
11431160
# Now, add the methods in dicts but not in MutableMapping
11441161
def __repr__(self):
11451162
return repr(self.data)

0 commit comments

Comments
 (0)