_collections_abc.py 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. # Copyright 2007 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Abstract Base Classes (ABCs) for collections, according to PEP 3119.
  4. Unit tests are in test_collections.
  5. """
  6. from abc import ABCMeta, abstractmethod
  7. import sys
  8. GenericAlias = type(list[int])
  9. EllipsisType = type(...)
  10. def _f(): pass
  11. FunctionType = type(_f)
  12. del _f
  13. __all__ = ["Awaitable", "Coroutine",
  14. "AsyncIterable", "AsyncIterator", "AsyncGenerator",
  15. "Hashable", "Iterable", "Iterator", "Generator", "Reversible",
  16. "Sized", "Container", "Callable", "Collection",
  17. "Set", "MutableSet",
  18. "Mapping", "MutableMapping",
  19. "MappingView", "KeysView", "ItemsView", "ValuesView",
  20. "Sequence", "MutableSequence",
  21. "ByteString",
  22. ]
  23. # This module has been renamed from collections.abc to _collections_abc to
  24. # speed up interpreter startup. Some of the types such as MutableMapping are
  25. # required early but collections module imports a lot of other modules.
  26. # See issue #19218
  27. __name__ = "collections.abc"
  28. # Private list of types that we want to register with the various ABCs
  29. # so that they will pass tests like:
  30. # it = iter(somebytearray)
  31. # assert isinstance(it, Iterable)
  32. # Note: in other implementations, these types might not be distinct
  33. # and they may have their own implementation specific types that
  34. # are not included on this list.
  35. bytes_iterator = type(iter(b''))
  36. bytearray_iterator = type(iter(bytearray()))
  37. #callable_iterator = ???
  38. dict_keyiterator = type(iter({}.keys()))
  39. dict_valueiterator = type(iter({}.values()))
  40. dict_itemiterator = type(iter({}.items()))
  41. list_iterator = type(iter([]))
  42. list_reverseiterator = type(iter(reversed([])))
  43. range_iterator = type(iter(range(0)))
  44. longrange_iterator = type(iter(range(1 << 1000)))
  45. set_iterator = type(iter(set()))
  46. str_iterator = type(iter(""))
  47. tuple_iterator = type(iter(()))
  48. zip_iterator = type(iter(zip()))
  49. ## views ##
  50. dict_keys = type({}.keys())
  51. dict_values = type({}.values())
  52. dict_items = type({}.items())
  53. ## misc ##
  54. mappingproxy = type(type.__dict__)
  55. generator = type((lambda: (yield))())
  56. ## coroutine ##
  57. async def _coro(): pass
  58. _coro = _coro()
  59. coroutine = type(_coro)
  60. _coro.close() # Prevent ResourceWarning
  61. del _coro
  62. ## asynchronous generator ##
  63. async def _ag(): yield
  64. _ag = _ag()
  65. async_generator = type(_ag)
  66. del _ag
  67. ### ONE-TRICK PONIES ###
  68. def _check_methods(C, *methods):
  69. mro = C.__mro__
  70. for method in methods:
  71. for B in mro:
  72. if method in B.__dict__:
  73. if B.__dict__[method] is None:
  74. return NotImplemented
  75. break
  76. else:
  77. return NotImplemented
  78. return True
  79. class Hashable(metaclass=ABCMeta):
  80. __slots__ = ()
  81. @abstractmethod
  82. def __hash__(self):
  83. return 0
  84. @classmethod
  85. def __subclasshook__(cls, C):
  86. if cls is Hashable:
  87. return _check_methods(C, "__hash__")
  88. return NotImplemented
  89. class Awaitable(metaclass=ABCMeta):
  90. __slots__ = ()
  91. @abstractmethod
  92. def __await__(self):
  93. yield
  94. @classmethod
  95. def __subclasshook__(cls, C):
  96. if cls is Awaitable:
  97. return _check_methods(C, "__await__")
  98. return NotImplemented
  99. __class_getitem__ = classmethod(GenericAlias)
  100. class Coroutine(Awaitable):
  101. __slots__ = ()
  102. @abstractmethod
  103. def send(self, value):
  104. """Send a value into the coroutine.
  105. Return next yielded value or raise StopIteration.
  106. """
  107. raise StopIteration
  108. @abstractmethod
  109. def throw(self, typ, val=None, tb=None):
  110. """Raise an exception in the coroutine.
  111. Return next yielded value or raise StopIteration.
  112. """
  113. if val is None:
  114. if tb is None:
  115. raise typ
  116. val = typ()
  117. if tb is not None:
  118. val = val.with_traceback(tb)
  119. raise val
  120. def close(self):
  121. """Raise GeneratorExit inside coroutine.
  122. """
  123. try:
  124. self.throw(GeneratorExit)
  125. except (GeneratorExit, StopIteration):
  126. pass
  127. else:
  128. raise RuntimeError("coroutine ignored GeneratorExit")
  129. @classmethod
  130. def __subclasshook__(cls, C):
  131. if cls is Coroutine:
  132. return _check_methods(C, '__await__', 'send', 'throw', 'close')
  133. return NotImplemented
  134. Coroutine.register(coroutine)
  135. class AsyncIterable(metaclass=ABCMeta):
  136. __slots__ = ()
  137. @abstractmethod
  138. def __aiter__(self):
  139. return AsyncIterator()
  140. @classmethod
  141. def __subclasshook__(cls, C):
  142. if cls is AsyncIterable:
  143. return _check_methods(C, "__aiter__")
  144. return NotImplemented
  145. __class_getitem__ = classmethod(GenericAlias)
  146. class AsyncIterator(AsyncIterable):
  147. __slots__ = ()
  148. @abstractmethod
  149. async def __anext__(self):
  150. """Return the next item or raise StopAsyncIteration when exhausted."""
  151. raise StopAsyncIteration
  152. def __aiter__(self):
  153. return self
  154. @classmethod
  155. def __subclasshook__(cls, C):
  156. if cls is AsyncIterator:
  157. return _check_methods(C, "__anext__", "__aiter__")
  158. return NotImplemented
  159. class AsyncGenerator(AsyncIterator):
  160. __slots__ = ()
  161. async def __anext__(self):
  162. """Return the next item from the asynchronous generator.
  163. When exhausted, raise StopAsyncIteration.
  164. """
  165. return await self.asend(None)
  166. @abstractmethod
  167. async def asend(self, value):
  168. """Send a value into the asynchronous generator.
  169. Return next yielded value or raise StopAsyncIteration.
  170. """
  171. raise StopAsyncIteration
  172. @abstractmethod
  173. async def athrow(self, typ, val=None, tb=None):
  174. """Raise an exception in the asynchronous generator.
  175. Return next yielded value or raise StopAsyncIteration.
  176. """
  177. if val is None:
  178. if tb is None:
  179. raise typ
  180. val = typ()
  181. if tb is not None:
  182. val = val.with_traceback(tb)
  183. raise val
  184. async def aclose(self):
  185. """Raise GeneratorExit inside coroutine.
  186. """
  187. try:
  188. await self.athrow(GeneratorExit)
  189. except (GeneratorExit, StopAsyncIteration):
  190. pass
  191. else:
  192. raise RuntimeError("asynchronous generator ignored GeneratorExit")
  193. @classmethod
  194. def __subclasshook__(cls, C):
  195. if cls is AsyncGenerator:
  196. return _check_methods(C, '__aiter__', '__anext__',
  197. 'asend', 'athrow', 'aclose')
  198. return NotImplemented
  199. AsyncGenerator.register(async_generator)
  200. class Iterable(metaclass=ABCMeta):
  201. __slots__ = ()
  202. @abstractmethod
  203. def __iter__(self):
  204. while False:
  205. yield None
  206. @classmethod
  207. def __subclasshook__(cls, C):
  208. if cls is Iterable:
  209. return _check_methods(C, "__iter__")
  210. return NotImplemented
  211. __class_getitem__ = classmethod(GenericAlias)
  212. class Iterator(Iterable):
  213. __slots__ = ()
  214. @abstractmethod
  215. def __next__(self):
  216. 'Return the next item from the iterator. When exhausted, raise StopIteration'
  217. raise StopIteration
  218. def __iter__(self):
  219. return self
  220. @classmethod
  221. def __subclasshook__(cls, C):
  222. if cls is Iterator:
  223. return _check_methods(C, '__iter__', '__next__')
  224. return NotImplemented
  225. Iterator.register(bytes_iterator)
  226. Iterator.register(bytearray_iterator)
  227. #Iterator.register(callable_iterator)
  228. Iterator.register(dict_keyiterator)
  229. Iterator.register(dict_valueiterator)
  230. Iterator.register(dict_itemiterator)
  231. Iterator.register(list_iterator)
  232. Iterator.register(list_reverseiterator)
  233. Iterator.register(range_iterator)
  234. Iterator.register(longrange_iterator)
  235. Iterator.register(set_iterator)
  236. Iterator.register(str_iterator)
  237. Iterator.register(tuple_iterator)
  238. Iterator.register(zip_iterator)
  239. class Reversible(Iterable):
  240. __slots__ = ()
  241. @abstractmethod
  242. def __reversed__(self):
  243. while False:
  244. yield None
  245. @classmethod
  246. def __subclasshook__(cls, C):
  247. if cls is Reversible:
  248. return _check_methods(C, "__reversed__", "__iter__")
  249. return NotImplemented
  250. class Generator(Iterator):
  251. __slots__ = ()
  252. def __next__(self):
  253. """Return the next item from the generator.
  254. When exhausted, raise StopIteration.
  255. """
  256. return self.send(None)
  257. @abstractmethod
  258. def send(self, value):
  259. """Send a value into the generator.
  260. Return next yielded value or raise StopIteration.
  261. """
  262. raise StopIteration
  263. @abstractmethod
  264. def throw(self, typ, val=None, tb=None):
  265. """Raise an exception in the generator.
  266. Return next yielded value or raise StopIteration.
  267. """
  268. if val is None:
  269. if tb is None:
  270. raise typ
  271. val = typ()
  272. if tb is not None:
  273. val = val.with_traceback(tb)
  274. raise val
  275. def close(self):
  276. """Raise GeneratorExit inside generator.
  277. """
  278. try:
  279. self.throw(GeneratorExit)
  280. except (GeneratorExit, StopIteration):
  281. pass
  282. else:
  283. raise RuntimeError("generator ignored GeneratorExit")
  284. @classmethod
  285. def __subclasshook__(cls, C):
  286. if cls is Generator:
  287. return _check_methods(C, '__iter__', '__next__',
  288. 'send', 'throw', 'close')
  289. return NotImplemented
  290. Generator.register(generator)
  291. class Sized(metaclass=ABCMeta):
  292. __slots__ = ()
  293. @abstractmethod
  294. def __len__(self):
  295. return 0
  296. @classmethod
  297. def __subclasshook__(cls, C):
  298. if cls is Sized:
  299. return _check_methods(C, "__len__")
  300. return NotImplemented
  301. class Container(metaclass=ABCMeta):
  302. __slots__ = ()
  303. @abstractmethod
  304. def __contains__(self, x):
  305. return False
  306. @classmethod
  307. def __subclasshook__(cls, C):
  308. if cls is Container:
  309. return _check_methods(C, "__contains__")
  310. return NotImplemented
  311. __class_getitem__ = classmethod(GenericAlias)
  312. class Collection(Sized, Iterable, Container):
  313. __slots__ = ()
  314. @classmethod
  315. def __subclasshook__(cls, C):
  316. if cls is Collection:
  317. return _check_methods(C, "__len__", "__iter__", "__contains__")
  318. return NotImplemented
  319. class _CallableGenericAlias(GenericAlias):
  320. """ Represent `Callable[argtypes, resulttype]`.
  321. This sets ``__args__`` to a tuple containing the flattened ``argtypes``
  322. followed by ``resulttype``.
  323. Example: ``Callable[[int, str], float]`` sets ``__args__`` to
  324. ``(int, str, float)``.
  325. """
  326. __slots__ = ()
  327. def __new__(cls, origin, args):
  328. if not (isinstance(args, tuple) and len(args) == 2):
  329. raise TypeError(
  330. "Callable must be used as Callable[[arg, ...], result].")
  331. t_args, t_result = args
  332. if isinstance(t_args, (tuple, list)):
  333. args = (*t_args, t_result)
  334. elif not _is_param_expr(t_args):
  335. raise TypeError(f"Expected a list of types, an ellipsis, "
  336. f"ParamSpec, or Concatenate. Got {t_args}")
  337. return super().__new__(cls, origin, args)
  338. def __repr__(self):
  339. if len(self.__args__) == 2 and _is_param_expr(self.__args__[0]):
  340. return super().__repr__()
  341. return (f'collections.abc.Callable'
  342. f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
  343. f'{_type_repr(self.__args__[-1])}]')
  344. def __reduce__(self):
  345. args = self.__args__
  346. if not (len(args) == 2 and _is_param_expr(args[0])):
  347. args = list(args[:-1]), args[-1]
  348. return _CallableGenericAlias, (Callable, args)
  349. def __getitem__(self, item):
  350. # Called during TypeVar substitution, returns the custom subclass
  351. # rather than the default types.GenericAlias object. Most of the
  352. # code is copied from typing's _GenericAlias and the builtin
  353. # types.GenericAlias.
  354. if not isinstance(item, tuple):
  355. item = (item,)
  356. # A special case in PEP 612 where if X = Callable[P, int],
  357. # then X[int, str] == X[[int, str]].
  358. if (len(self.__parameters__) == 1
  359. and _is_param_expr(self.__parameters__[0])
  360. and item and not _is_param_expr(item[0])):
  361. item = (item,)
  362. new_args = super().__getitem__(item).__args__
  363. # args[0] occurs due to things like Z[[int, str, bool]] from PEP 612
  364. if not isinstance(new_args[0], (tuple, list)):
  365. t_result = new_args[-1]
  366. t_args = new_args[:-1]
  367. new_args = (t_args, t_result)
  368. return _CallableGenericAlias(Callable, tuple(new_args))
  369. def _is_param_expr(obj):
  370. """Checks if obj matches either a list of types, ``...``, ``ParamSpec`` or
  371. ``_ConcatenateGenericAlias`` from typing.py
  372. """
  373. if obj is Ellipsis:
  374. return True
  375. if isinstance(obj, list):
  376. return True
  377. obj = type(obj)
  378. names = ('ParamSpec', '_ConcatenateGenericAlias')
  379. return obj.__module__ == 'typing' and any(obj.__name__ == name for name in names)
  380. def _type_repr(obj):
  381. """Return the repr() of an object, special-casing types (internal helper).
  382. Copied from :mod:`typing` since collections.abc
  383. shouldn't depend on that module.
  384. """
  385. if isinstance(obj, GenericAlias):
  386. return repr(obj)
  387. if isinstance(obj, type):
  388. if obj.__module__ == 'builtins':
  389. return obj.__qualname__
  390. return f'{obj.__module__}.{obj.__qualname__}'
  391. if obj is Ellipsis:
  392. return '...'
  393. if isinstance(obj, FunctionType):
  394. return obj.__name__
  395. return repr(obj)
  396. class Callable(metaclass=ABCMeta):
  397. __slots__ = ()
  398. @abstractmethod
  399. def __call__(self, *args, **kwds):
  400. return False
  401. @classmethod
  402. def __subclasshook__(cls, C):
  403. if cls is Callable:
  404. return _check_methods(C, "__call__")
  405. return NotImplemented
  406. __class_getitem__ = classmethod(_CallableGenericAlias)
  407. ### SETS ###
  408. class Set(Collection):
  409. """A set is a finite, iterable container.
  410. This class provides concrete generic implementations of all
  411. methods except for __contains__, __iter__ and __len__.
  412. To override the comparisons (presumably for speed, as the
  413. semantics are fixed), redefine __le__ and __ge__,
  414. then the other operations will automatically follow suit.
  415. """
  416. __slots__ = ()
  417. def __le__(self, other):
  418. if not isinstance(other, Set):
  419. return NotImplemented
  420. if len(self) > len(other):
  421. return False
  422. for elem in self:
  423. if elem not in other:
  424. return False
  425. return True
  426. def __lt__(self, other):
  427. if not isinstance(other, Set):
  428. return NotImplemented
  429. return len(self) < len(other) and self.__le__(other)
  430. def __gt__(self, other):
  431. if not isinstance(other, Set):
  432. return NotImplemented
  433. return len(self) > len(other) and self.__ge__(other)
  434. def __ge__(self, other):
  435. if not isinstance(other, Set):
  436. return NotImplemented
  437. if len(self) < len(other):
  438. return False
  439. for elem in other:
  440. if elem not in self:
  441. return False
  442. return True
  443. def __eq__(self, other):
  444. if not isinstance(other, Set):
  445. return NotImplemented
  446. return len(self) == len(other) and self.__le__(other)
  447. @classmethod
  448. def _from_iterable(cls, it):
  449. '''Construct an instance of the class from any iterable input.
  450. Must override this method if the class constructor signature
  451. does not accept an iterable for an input.
  452. '''
  453. return cls(it)
  454. def __and__(self, other):
  455. if not isinstance(other, Iterable):
  456. return NotImplemented
  457. return self._from_iterable(value for value in other if value in self)
  458. __rand__ = __and__
  459. def isdisjoint(self, other):
  460. 'Return True if two sets have a null intersection.'
  461. for value in other:
  462. if value in self:
  463. return False
  464. return True
  465. def __or__(self, other):
  466. if not isinstance(other, Iterable):
  467. return NotImplemented
  468. chain = (e for s in (self, other) for e in s)
  469. return self._from_iterable(chain)
  470. __ror__ = __or__
  471. def __sub__(self, other):
  472. if not isinstance(other, Set):
  473. if not isinstance(other, Iterable):
  474. return NotImplemented
  475. other = self._from_iterable(other)
  476. return self._from_iterable(value for value in self
  477. if value not in other)
  478. def __rsub__(self, other):
  479. if not isinstance(other, Set):
  480. if not isinstance(other, Iterable):
  481. return NotImplemented
  482. other = self._from_iterable(other)
  483. return self._from_iterable(value for value in other
  484. if value not in self)
  485. def __xor__(self, other):
  486. if not isinstance(other, Set):
  487. if not isinstance(other, Iterable):
  488. return NotImplemented
  489. other = self._from_iterable(other)
  490. return (self - other) | (other - self)
  491. __rxor__ = __xor__
  492. def _hash(self):
  493. """Compute the hash value of a set.
  494. Note that we don't define __hash__: not all sets are hashable.
  495. But if you define a hashable set type, its __hash__ should
  496. call this function.
  497. This must be compatible __eq__.
  498. All sets ought to compare equal if they contain the same
  499. elements, regardless of how they are implemented, and
  500. regardless of the order of the elements; so there's not much
  501. freedom for __eq__ or __hash__. We match the algorithm used
  502. by the built-in frozenset type.
  503. """
  504. MAX = sys.maxsize
  505. MASK = 2 * MAX + 1
  506. n = len(self)
  507. h = 1927868237 * (n + 1)
  508. h &= MASK
  509. for x in self:
  510. hx = hash(x)
  511. h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
  512. h &= MASK
  513. h ^= (h >> 11) ^ (h >> 25)
  514. h = h * 69069 + 907133923
  515. h &= MASK
  516. if h > MAX:
  517. h -= MASK + 1
  518. if h == -1:
  519. h = 590923713
  520. return h
  521. Set.register(frozenset)
  522. class MutableSet(Set):
  523. """A mutable set is a finite, iterable container.
  524. This class provides concrete generic implementations of all
  525. methods except for __contains__, __iter__, __len__,
  526. add(), and discard().
  527. To override the comparisons (presumably for speed, as the
  528. semantics are fixed), all you have to do is redefine __le__ and
  529. then the other operations will automatically follow suit.
  530. """
  531. __slots__ = ()
  532. @abstractmethod
  533. def add(self, value):
  534. """Add an element."""
  535. raise NotImplementedError
  536. @abstractmethod
  537. def discard(self, value):
  538. """Remove an element. Do not raise an exception if absent."""
  539. raise NotImplementedError
  540. def remove(self, value):
  541. """Remove an element. If not a member, raise a KeyError."""
  542. if value not in self:
  543. raise KeyError(value)
  544. self.discard(value)
  545. def pop(self):
  546. """Return the popped value. Raise KeyError if empty."""
  547. it = iter(self)
  548. try:
  549. value = next(it)
  550. except StopIteration:
  551. raise KeyError from None
  552. self.discard(value)
  553. return value
  554. def clear(self):
  555. """This is slow (creates N new iterators!) but effective."""
  556. try:
  557. while True:
  558. self.pop()
  559. except KeyError:
  560. pass
  561. def __ior__(self, it):
  562. for value in it:
  563. self.add(value)
  564. return self
  565. def __iand__(self, it):
  566. for value in (self - it):
  567. self.discard(value)
  568. return self
  569. def __ixor__(self, it):
  570. if it is self:
  571. self.clear()
  572. else:
  573. if not isinstance(it, Set):
  574. it = self._from_iterable(it)
  575. for value in it:
  576. if value in self:
  577. self.discard(value)
  578. else:
  579. self.add(value)
  580. return self
  581. def __isub__(self, it):
  582. if it is self:
  583. self.clear()
  584. else:
  585. for value in it:
  586. self.discard(value)
  587. return self
  588. MutableSet.register(set)
  589. ### MAPPINGS ###
  590. class Mapping(Collection):
  591. """A Mapping is a generic container for associating key/value
  592. pairs.
  593. This class provides concrete generic implementations of all
  594. methods except for __getitem__, __iter__, and __len__.
  595. """
  596. __slots__ = ()
  597. # Tell ABCMeta.__new__ that this class should have TPFLAGS_MAPPING set.
  598. __abc_tpflags__ = 1 << 6 # Py_TPFLAGS_MAPPING
  599. @abstractmethod
  600. def __getitem__(self, key):
  601. raise KeyError
  602. def get(self, key, default=None):
  603. 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
  604. try:
  605. return self[key]
  606. except KeyError:
  607. return default
  608. def __contains__(self, key):
  609. try:
  610. self[key]
  611. except KeyError:
  612. return False
  613. else:
  614. return True
  615. def keys(self):
  616. "D.keys() -> a set-like object providing a view on D's keys"
  617. return KeysView(self)
  618. def items(self):
  619. "D.items() -> a set-like object providing a view on D's items"
  620. return ItemsView(self)
  621. def values(self):
  622. "D.values() -> an object providing a view on D's values"
  623. return ValuesView(self)
  624. def __eq__(self, other):
  625. if not isinstance(other, Mapping):
  626. return NotImplemented
  627. return dict(self.items()) == dict(other.items())
  628. __reversed__ = None
  629. Mapping.register(mappingproxy)
  630. class MappingView(Sized):
  631. __slots__ = '_mapping',
  632. def __init__(self, mapping):
  633. self._mapping = mapping
  634. def __len__(self):
  635. return len(self._mapping)
  636. def __repr__(self):
  637. return '{0.__class__.__name__}({0._mapping!r})'.format(self)
  638. __class_getitem__ = classmethod(GenericAlias)
  639. class KeysView(MappingView, Set):
  640. __slots__ = ()
  641. @classmethod
  642. def _from_iterable(cls, it):
  643. return set(it)
  644. def __contains__(self, key):
  645. return key in self._mapping
  646. def __iter__(self):
  647. yield from self._mapping
  648. KeysView.register(dict_keys)
  649. class ItemsView(MappingView, Set):
  650. __slots__ = ()
  651. @classmethod
  652. def _from_iterable(cls, it):
  653. return set(it)
  654. def __contains__(self, item):
  655. key, value = item
  656. try:
  657. v = self._mapping[key]
  658. except KeyError:
  659. return False
  660. else:
  661. return v is value or v == value
  662. def __iter__(self):
  663. for key in self._mapping:
  664. yield (key, self._mapping[key])
  665. ItemsView.register(dict_items)
  666. class ValuesView(MappingView, Collection):
  667. __slots__ = ()
  668. def __contains__(self, value):
  669. for key in self._mapping:
  670. v = self._mapping[key]
  671. if v is value or v == value:
  672. return True
  673. return False
  674. def __iter__(self):
  675. for key in self._mapping:
  676. yield self._mapping[key]
  677. ValuesView.register(dict_values)
  678. class MutableMapping(Mapping):
  679. """A MutableMapping is a generic container for associating
  680. key/value pairs.
  681. This class provides concrete generic implementations of all
  682. methods except for __getitem__, __setitem__, __delitem__,
  683. __iter__, and __len__.
  684. """
  685. __slots__ = ()
  686. @abstractmethod
  687. def __setitem__(self, key, value):
  688. raise KeyError
  689. @abstractmethod
  690. def __delitem__(self, key):
  691. raise KeyError
  692. __marker = object()
  693. def pop(self, key, default=__marker):
  694. '''D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  695. If key is not found, d is returned if given, otherwise KeyError is raised.
  696. '''
  697. try:
  698. value = self[key]
  699. except KeyError:
  700. if default is self.__marker:
  701. raise
  702. return default
  703. else:
  704. del self[key]
  705. return value
  706. def popitem(self):
  707. '''D.popitem() -> (k, v), remove and return some (key, value) pair
  708. as a 2-tuple; but raise KeyError if D is empty.
  709. '''
  710. try:
  711. key = next(iter(self))
  712. except StopIteration:
  713. raise KeyError from None
  714. value = self[key]
  715. del self[key]
  716. return key, value
  717. def clear(self):
  718. 'D.clear() -> None. Remove all items from D.'
  719. try:
  720. while True:
  721. self.popitem()
  722. except KeyError:
  723. pass
  724. def update(self, other=(), /, **kwds):
  725. ''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
  726. If E present and has a .keys() method, does: for k in E: D[k] = E[k]
  727. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
  728. In either case, this is followed by: for k, v in F.items(): D[k] = v
  729. '''
  730. if isinstance(other, Mapping):
  731. for key in other:
  732. self[key] = other[key]
  733. elif hasattr(other, "keys"):
  734. for key in other.keys():
  735. self[key] = other[key]
  736. else:
  737. for key, value in other:
  738. self[key] = value
  739. for key, value in kwds.items():
  740. self[key] = value
  741. def setdefault(self, key, default=None):
  742. 'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
  743. try:
  744. return self[key]
  745. except KeyError:
  746. self[key] = default
  747. return default
  748. MutableMapping.register(dict)
  749. ### SEQUENCES ###
  750. class Sequence(Reversible, Collection):
  751. """All the operations on a read-only sequence.
  752. Concrete subclasses must override __new__ or __init__,
  753. __getitem__, and __len__.
  754. """
  755. __slots__ = ()
  756. # Tell ABCMeta.__new__ that this class should have TPFLAGS_SEQUENCE set.
  757. __abc_tpflags__ = 1 << 5 # Py_TPFLAGS_SEQUENCE
  758. @abstractmethod
  759. def __getitem__(self, index):
  760. raise IndexError
  761. def __iter__(self):
  762. i = 0
  763. try:
  764. while True:
  765. v = self[i]
  766. yield v
  767. i += 1
  768. except IndexError:
  769. return
  770. def __contains__(self, value):
  771. for v in self:
  772. if v is value or v == value:
  773. return True
  774. return False
  775. def __reversed__(self):
  776. for i in reversed(range(len(self))):
  777. yield self[i]
  778. def index(self, value, start=0, stop=None):
  779. '''S.index(value, [start, [stop]]) -> integer -- return first index of value.
  780. Raises ValueError if the value is not present.
  781. Supporting start and stop arguments is optional, but
  782. recommended.
  783. '''
  784. if start is not None and start < 0:
  785. start = max(len(self) + start, 0)
  786. if stop is not None and stop < 0:
  787. stop += len(self)
  788. i = start
  789. while stop is None or i < stop:
  790. try:
  791. v = self[i]
  792. except IndexError:
  793. break
  794. if v is value or v == value:
  795. return i
  796. i += 1
  797. raise ValueError
  798. def count(self, value):
  799. 'S.count(value) -> integer -- return number of occurrences of value'
  800. return sum(1 for v in self if v is value or v == value)
  801. Sequence.register(tuple)
  802. Sequence.register(str)
  803. Sequence.register(range)
  804. Sequence.register(memoryview)
  805. class ByteString(Sequence):
  806. """This unifies bytes and bytearray.
  807. XXX Should add all their methods.
  808. """
  809. __slots__ = ()
  810. ByteString.register(bytes)
  811. ByteString.register(bytearray)
  812. class MutableSequence(Sequence):
  813. """All the operations on a read-write sequence.
  814. Concrete subclasses must provide __new__ or __init__,
  815. __getitem__, __setitem__, __delitem__, __len__, and insert().
  816. """
  817. __slots__ = ()
  818. @abstractmethod
  819. def __setitem__(self, index, value):
  820. raise IndexError
  821. @abstractmethod
  822. def __delitem__(self, index):
  823. raise IndexError
  824. @abstractmethod
  825. def insert(self, index, value):
  826. 'S.insert(index, value) -- insert value before index'
  827. raise IndexError
  828. def append(self, value):
  829. 'S.append(value) -- append value to the end of the sequence'
  830. self.insert(len(self), value)
  831. def clear(self):
  832. 'S.clear() -> None -- remove all items from S'
  833. try:
  834. while True:
  835. self.pop()
  836. except IndexError:
  837. pass
  838. def reverse(self):
  839. 'S.reverse() -- reverse *IN PLACE*'
  840. n = len(self)
  841. for i in range(n//2):
  842. self[i], self[n-i-1] = self[n-i-1], self[i]
  843. def extend(self, values):
  844. 'S.extend(iterable) -- extend sequence by appending elements from the iterable'
  845. if values is self:
  846. values = list(values)
  847. for v in values:
  848. self.append(v)
  849. def pop(self, index=-1):
  850. '''S.pop([index]) -> item -- remove and return item at index (default last).
  851. Raise IndexError if list is empty or index is out of range.
  852. '''
  853. v = self[index]
  854. del self[index]
  855. return v
  856. def remove(self, value):
  857. '''S.remove(value) -- remove first occurrence of value.
  858. Raise ValueError if the value is not present.
  859. '''
  860. del self[self.index(value)]
  861. def __iadd__(self, values):
  862. self.extend(values)
  863. return self
  864. MutableSequence.register(list)
  865. MutableSequence.register(bytearray) # Multiply inheriting, see ByteString