2929import _collections_abc
3030import sys as _sys
3131
32+ _sys .modules ['collections.abc' ] = _collections_abc
33+ abc = _collections_abc
34+
3235from itertools import chain as _chain
3336from itertools import repeat as _repeat
3437from itertools import starmap as _starmap
4649 _collections_abc .MutableSequence .register (deque )
4750
4851try :
49- from _collections import _deque_iterator
52+ # Expose _deque_iterator to support pickling deque iterators
53+ from _collections import _deque_iterator # noqa: F401
5054except ImportError :
5155 pass
5256
5963 # with this instead.
6064 from ._defaultdict import defaultdict
6165
66+ heapq = None # Lazily imported
67+
6268
6369################################################################################
6470### OrderedDict
@@ -461,7 +467,7 @@ def _make(cls, iterable):
461467 def _replace (self , / , ** kwds ):
462468 result = self ._make (_map (kwds .pop , field_names , self ))
463469 if kwds :
464- raise ValueError (f'Got unexpected field names: { list (kwds )!r} ' )
470+ raise TypeError (f'Got unexpected field names: { list (kwds )!r} ' )
465471 return result
466472
467473 _replace .__doc__ = (f'Return a new { typename } object replacing specified '
@@ -499,6 +505,7 @@ def __getnewargs__(self):
499505 '_field_defaults' : field_defaults ,
500506 '__new__' : __new__ ,
501507 '_make' : _make ,
508+ '__replace__' : _replace ,
502509 '_replace' : _replace ,
503510 '__repr__' : __repr__ ,
504511 '_asdict' : _asdict ,
@@ -592,7 +599,7 @@ class Counter(dict):
592599 # References:
593600 # http://en.wikipedia.org/wiki/Multiset
594601 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
595- # http://www.demo2s .com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
602+ # http://www.java2s .com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
596603 # http://code.activestate.com/recipes/259174/
597604 # Knuth, TAOCP Vol. II section 4.6.3
598605
@@ -632,7 +639,10 @@ def most_common(self, n=None):
632639 return sorted (self .items (), key = _itemgetter (1 ), reverse = True )
633640
634641 # Lazy import to speedup Python startup time
635- import heapq
642+ global heapq
643+ if heapq is None :
644+ import heapq
645+
636646 return heapq .nlargest (n , self .items (), key = _itemgetter (1 ))
637647
638648 def elements (self ):
@@ -642,7 +652,8 @@ def elements(self):
642652 >>> sorted(c.elements())
643653 ['A', 'A', 'B', 'B', 'C', 'C']
644654
645- # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
655+ Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
656+
646657 >>> import math
647658 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
648659 >>> math.prod(prime_factors.elements())
@@ -683,7 +694,7 @@ def update(self, iterable=None, /, **kwds):
683694
684695 '''
685696 # The regular dict.update() operation makes no sense here because the
686- # replace behavior results in the some of original untouched counts
697+ # replace behavior results in some of the original untouched counts
687698 # being mixed-in with all of the other counts for a mismash that
688699 # doesn't have a straight-forward interpretation in most counting
689700 # contexts. Instead, we implement straight-addition. Both the inputs
@@ -1018,7 +1029,7 @@ def __getitem__(self, key):
10181029 return self .__missing__ (key ) # support subclasses that define __missing__
10191030
10201031 def get (self , key , default = None ):
1021- return self [key ] if key in self else default
1032+ return self [key ] if key in self else default # needs to make use of __contains__
10221033
10231034 def __len__ (self ):
10241035 return len (set ().union (* self .maps )) # reuses stored hash values if possible
@@ -1030,7 +1041,10 @@ def __iter__(self):
10301041 return iter (d )
10311042
10321043 def __contains__ (self , key ):
1033- return any (key in m for m in self .maps )
1044+ for mapping in self .maps :
1045+ if key in mapping :
1046+ return True
1047+ return False
10341048
10351049 def __bool__ (self ):
10361050 return any (self .maps )
@@ -1040,9 +1054,9 @@ def __repr__(self):
10401054 return f'{ self .__class__ .__name__ } ({ ", " .join (map (repr , self .maps ))} )'
10411055
10421056 @classmethod
1043- def fromkeys (cls , iterable , * args ):
1044- 'Create a ChainMap with a single dict created from the iterable .'
1045- return cls (dict .fromkeys (iterable , * args ))
1057+ def fromkeys (cls , iterable , value = None , / ):
1058+ 'Create a new ChainMap with keys from iterable and values set to value .'
1059+ return cls (dict .fromkeys (iterable , value ))
10461060
10471061 def copy (self ):
10481062 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
@@ -1485,6 +1499,8 @@ def format_map(self, mapping):
14851499 return self .data .format_map (mapping )
14861500
14871501 def index (self , sub , start = 0 , end = _sys .maxsize ):
1502+ if isinstance (sub , UserString ):
1503+ sub = sub .data
14881504 return self .data .index (sub , start , end )
14891505
14901506 def isalpha (self ):
@@ -1553,6 +1569,8 @@ def rfind(self, sub, start=0, end=_sys.maxsize):
15531569 return self .data .rfind (sub , start , end )
15541570
15551571 def rindex (self , sub , start = 0 , end = _sys .maxsize ):
1572+ if isinstance (sub , UserString ):
1573+ sub = sub .data
15561574 return self .data .rindex (sub , start , end )
15571575
15581576 def rjust (self , width , * args ):
0 commit comments