Skip to content

Commit cb54bd1

Browse files
Updated __init__.py to be inline with CPython 3.11.2
1 parent 7e50590 commit cb54bd1

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

Lib/collections/__init__.py

+55-44
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def pop(self, key, default=__marker):
243243
marker = self.__marker
244244
result = dict.pop(self, key, marker)
245245
if result is not marker:
246+
# The same as in __delitem__().
246247
link = self.__map.pop(key)
247248
link_prev = link.prev
248249
link_next = link.next
@@ -274,10 +275,22 @@ def __repr__(self):
274275

275276
def __reduce__(self):
276277
'Return state information for pickling'
277-
inst_dict = vars(self).copy()
278-
for k in vars(OrderedDict()):
279-
inst_dict.pop(k, None)
280-
return self.__class__, (), inst_dict or None, None, iter(self.items())
278+
state = self.__getstate__()
279+
if state:
280+
if isinstance(state, tuple):
281+
state, slots = state
282+
else:
283+
slots = {}
284+
state = state.copy()
285+
slots = slots.copy()
286+
for k in vars(OrderedDict()):
287+
state.pop(k, None)
288+
slots.pop(k, None)
289+
if slots:
290+
state = state, slots
291+
else:
292+
state = state or None
293+
return self.__class__, (), state, None, iter(self.items())
281294

282295
def copy(self):
283296
'od.copy() -> a shallow copy of od'
@@ -620,11 +633,9 @@ def elements(self):
620633
['A', 'A', 'B', 'B', 'C', 'C']
621634
622635
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
636+
>>> import math
623637
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
624-
>>> product = 1
625-
>>> for factor in prime_factors.elements(): # loop over factors
626-
... product *= factor # and multiply them
627-
>>> product
638+
>>> math.prod(prime_factors.elements())
628639
1836
629640
630641
Note, if an element's count has been set to zero or is a negative
@@ -721,42 +732,6 @@ def __delitem__(self, elem):
721732
if elem in self:
722733
super().__delitem__(elem)
723734

724-
def __eq__(self, other):
725-
'True if all counts agree. Missing counts are treated as zero.'
726-
if not isinstance(other, Counter):
727-
return NotImplemented
728-
return all(self[e] == other[e] for c in (self, other) for e in c)
729-
730-
def __ne__(self, other):
731-
'True if any counts disagree. Missing counts are treated as zero.'
732-
if not isinstance(other, Counter):
733-
return NotImplemented
734-
return not self == other
735-
736-
def __le__(self, other):
737-
'True if all counts in self are a subset of those in other.'
738-
if not isinstance(other, Counter):
739-
return NotImplemented
740-
return all(self[e] <= other[e] for c in (self, other) for e in c)
741-
742-
def __lt__(self, other):
743-
'True if all counts in self are a proper subset of those in other.'
744-
if not isinstance(other, Counter):
745-
return NotImplemented
746-
return self <= other and self != other
747-
748-
def __ge__(self, other):
749-
'True if all counts in self are a superset of those in other.'
750-
if not isinstance(other, Counter):
751-
return NotImplemented
752-
return all(self[e] >= other[e] for c in (self, other) for e in c)
753-
754-
def __gt__(self, other):
755-
'True if all counts in self are a proper superset of those in other.'
756-
if not isinstance(other, Counter):
757-
return NotImplemented
758-
return self >= other and self != other
759-
760735
def __repr__(self):
761736
if not self:
762737
return f'{self.__class__.__name__}()'
@@ -802,6 +777,42 @@ def __repr__(self):
802777
# (cp >= cq) == (sp >= sq)
803778
# (cp > cq) == (sp > sq)
804779

780+
def __eq__(self, other):
781+
'True if all counts agree. Missing counts are treated as zero.'
782+
if not isinstance(other, Counter):
783+
return NotImplemented
784+
return all(self[e] == other[e] for c in (self, other) for e in c)
785+
786+
def __ne__(self, other):
787+
'True if any counts disagree. Missing counts are treated as zero.'
788+
if not isinstance(other, Counter):
789+
return NotImplemented
790+
return not self == other
791+
792+
def __le__(self, other):
793+
'True if all counts in self are a subset of those in other.'
794+
if not isinstance(other, Counter):
795+
return NotImplemented
796+
return all(self[e] <= other[e] for c in (self, other) for e in c)
797+
798+
def __lt__(self, other):
799+
'True if all counts in self are a proper subset of those in other.'
800+
if not isinstance(other, Counter):
801+
return NotImplemented
802+
return self <= other and self != other
803+
804+
def __ge__(self, other):
805+
'True if all counts in self are a superset of those in other.'
806+
if not isinstance(other, Counter):
807+
return NotImplemented
808+
return all(self[e] >= other[e] for c in (self, other) for e in c)
809+
810+
def __gt__(self, other):
811+
'True if all counts in self are a proper superset of those in other.'
812+
if not isinstance(other, Counter):
813+
return NotImplemented
814+
return self >= other and self != other
815+
805816
def __add__(self, other):
806817
'''Add counts from two counters.
807818

0 commit comments

Comments
 (0)