|
45 | 45 | else:
|
46 | 46 | _collections_abc.MutableSequence.register(deque)
|
47 | 47 |
|
| 48 | +try: |
| 49 | + from _collections import _deque_iterator |
| 50 | +except ImportError: |
| 51 | + pass |
| 52 | + |
48 | 53 | try:
|
49 | 54 | from _collections import defaultdict
|
50 | 55 | except ImportError:
|
@@ -94,17 +99,19 @@ class OrderedDict(dict):
|
94 | 99 | # Individual links are kept alive by the hard reference in self.__map.
|
95 | 100 | # Those hard references disappear when a key is deleted from an OrderedDict.
|
96 | 101 |
|
| 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 | + |
97 | 111 | def __init__(self, other=(), /, **kwds):
|
98 | 112 | '''Initialize an ordered dictionary. The signature is the same as
|
99 | 113 | regular dictionaries. Keyword argument order is preserved.
|
100 | 114 | '''
|
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 = {} |
108 | 115 | self.__update(other, **kwds)
|
109 | 116 |
|
110 | 117 | def __setitem__(self, key, value,
|
@@ -271,7 +278,7 @@ def __repr__(self):
|
271 | 278 | 'od.__repr__() <==> repr(od)'
|
272 | 279 | if not self:
|
273 | 280 | 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())) |
275 | 282 |
|
276 | 283 | def __reduce__(self):
|
277 | 284 | 'Return state information for pickling'
|
@@ -511,9 +518,12 @@ def __getnewargs__(self):
|
511 | 518 | # specified a particular module.
|
512 | 519 | if module is None:
|
513 | 520 | 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 |
517 | 527 | if module is not None:
|
518 | 528 | result.__module__ = module
|
519 | 529 |
|
@@ -1015,8 +1025,8 @@ def __len__(self):
|
1015 | 1025 |
|
1016 | 1026 | def __iter__(self):
|
1017 | 1027 | 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 |
1020 | 1030 | return iter(d)
|
1021 | 1031 |
|
1022 | 1032 | def __contains__(self, key):
|
@@ -1136,10 +1146,17 @@ def __delitem__(self, key):
|
1136 | 1146 | def __iter__(self):
|
1137 | 1147 | return iter(self.data)
|
1138 | 1148 |
|
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. |
1140 | 1151 | def __contains__(self, key):
|
1141 | 1152 | return key in self.data
|
1142 | 1153 |
|
| 1154 | + def get(self, key, default=None): |
| 1155 | + if key in self: |
| 1156 | + return self[key] |
| 1157 | + return default |
| 1158 | + |
| 1159 | + |
1143 | 1160 | # Now, add the methods in dicts but not in MutableMapping
|
1144 | 1161 | def __repr__(self):
|
1145 | 1162 | return repr(self.data)
|
|
0 commit comments