Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(20)

Delta Between Two Patch Sets: Lib/_collections_abc.py

Issue 25958: Implicit ABCs have no means of "anti-registration"
Left Patch Set: Created 4 years, 1 month ago
Right Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/reference/datamodel.rst ('k') | Lib/test/test_augassign.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright 2007 Google, Inc. All Rights Reserved. 1 # Copyright 2007 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """Abstract Base Classes (ABCs) for collections, according to PEP 3119. 4 """Abstract Base Classes (ABCs) for collections, according to PEP 3119.
5 5
6 Unit tests are in test_collections. 6 Unit tests are in test_collections.
7 """ 7 """
8 8
9 from abc import ABCMeta, abstractmethod 9 from abc import ABCMeta, abstractmethod
10 import sys 10 import sys
11 11
12 __all__ = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator", 12 __all__ = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator",
13 "Hashable", "Iterable", "Iterator", "Generator", 13 "Hashable", "Iterable", "Iterator", "Generator", "Reversible",
14 "Sized", "Container", "Reversible", "Callable", 14 "Sized", "Container", "Callable",
15 "Set", "MutableSet", 15 "Set", "MutableSet",
16 "Mapping", "MutableMapping", 16 "Mapping", "MutableMapping",
17 "MappingView", "KeysView", "ItemsView", "ValuesView", 17 "MappingView", "KeysView", "ItemsView", "ValuesView",
18 "Sequence", "MutableSequence", 18 "Sequence", "MutableSequence",
19 "ByteString", 19 "ByteString",
20 ] 20 ]
21 21
22 # This module has been renamed from collections.abc to _collections_abc to 22 # This module has been renamed from collections.abc to _collections_abc to
23 # speed up interpreter startup. Some of the types such as MutableMapping are 23 # speed up interpreter startup. Some of the types such as MutableMapping are
24 # required early but collections module imports a lot of other modules. 24 # required early but collections module imports a lot of other modules.
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 146
147 147
148 Coroutine.register(coroutine) 148 Coroutine.register(coroutine)
149 149
150 150
151 class AsyncIterable(metaclass=ABCMeta): 151 class AsyncIterable(metaclass=ABCMeta):
152 152
153 __slots__ = () 153 __slots__ = ()
154 154
155 @abstractmethod 155 @abstractmethod
156 async def __aiter__(self): 156 def __aiter__(self):
157 return AsyncIterator() 157 return AsyncIterator()
158 158
159 @classmethod 159 @classmethod
160 def __subclasshook__(cls, C): 160 def __subclasshook__(cls, C):
161 if cls is AsyncIterable: 161 if cls is AsyncIterable:
162 return _check_methods(C, "__aiter__") 162 return _check_methods(C, "__aiter__")
163 return NotImplemented 163 return NotImplemented
164 164
165 165
166 class AsyncIterator(AsyncIterable): 166 class AsyncIterator(AsyncIterable):
167 167
168 __slots__ = () 168 __slots__ = ()
169 169
170 @abstractmethod 170 @abstractmethod
171 async def __anext__(self): 171 async def __anext__(self):
172 """Return the next item or raise StopAsyncIteration when exhausted.""" 172 """Return the next item or raise StopAsyncIteration when exhausted."""
173 raise StopAsyncIteration 173 raise StopAsyncIteration
174 174
175 async def __aiter__(self): 175 def __aiter__(self):
176 return self 176 return self
177 177
178 @classmethod 178 @classmethod
179 def __subclasshook__(cls, C): 179 def __subclasshook__(cls, C):
180 if cls is AsyncIterator: 180 if cls is AsyncIterator:
181 return _check_methods(C, "__anext__", "__aiter__") 181 return _check_methods(C, "__anext__", "__aiter__")
182 return NotImplemented 182 return NotImplemented
183 183
184 184
185 class Iterable(metaclass=ABCMeta): 185 class Iterable(metaclass=ABCMeta):
(...skipping 20 matching lines...) Expand all
206 def __next__(self): 206 def __next__(self):
207 'Return the next item from the iterator. When exhausted, raise StopItera tion' 207 'Return the next item from the iterator. When exhausted, raise StopItera tion'
208 raise StopIteration 208 raise StopIteration
209 209
210 def __iter__(self): 210 def __iter__(self):
211 return self 211 return self
212 212
213 @classmethod 213 @classmethod
214 def __subclasshook__(cls, C): 214 def __subclasshook__(cls, C):
215 if cls is Iterator: 215 if cls is Iterator:
216 return _check_methods(C, "__next__", "__iter__") 216 return _check_methods(C, '__iter__', '__next__')
217 return NotImplemented 217 return NotImplemented
218 218
219 Iterator.register(bytes_iterator) 219 Iterator.register(bytes_iterator)
220 Iterator.register(bytearray_iterator) 220 Iterator.register(bytearray_iterator)
221 #Iterator.register(callable_iterator) 221 #Iterator.register(callable_iterator)
222 Iterator.register(dict_keyiterator) 222 Iterator.register(dict_keyiterator)
223 Iterator.register(dict_valueiterator) 223 Iterator.register(dict_valueiterator)
224 Iterator.register(dict_itemiterator) 224 Iterator.register(dict_itemiterator)
225 Iterator.register(list_iterator) 225 Iterator.register(list_iterator)
226 Iterator.register(list_reverseiterator) 226 Iterator.register(list_reverseiterator)
227 Iterator.register(range_iterator) 227 Iterator.register(range_iterator)
228 Iterator.register(set_iterator) 228 Iterator.register(set_iterator)
229 Iterator.register(str_iterator) 229 Iterator.register(str_iterator)
230 Iterator.register(tuple_iterator) 230 Iterator.register(tuple_iterator)
231 Iterator.register(zip_iterator) 231 Iterator.register(zip_iterator)
232 232
233 233
234 class Reversible(Iterable):
235
236 __slots__ = ()
237
238 @abstractmethod
239 def __reversed__(self):
240 while False:
241 yield None
242
243 @classmethod
244 def __subclasshook__(cls, C):
245 if cls is Reversible:
246 return _check_methods(C, "__reversed__", "__iter__")
247 return NotImplemented
248
249
234 class Generator(Iterator): 250 class Generator(Iterator):
235 251
236 __slots__ = () 252 __slots__ = ()
237 253
238 def __next__(self): 254 def __next__(self):
239 """Return the next item from the generator. 255 """Return the next item from the generator.
240 When exhausted, raise StopIteration. 256 When exhausted, raise StopIteration.
241 """ 257 """
242 return self.send(None) 258 return self.send(None)
243 259
(...skipping 27 matching lines...) Expand all
271 else: 287 else:
272 raise RuntimeError("generator ignored GeneratorExit") 288 raise RuntimeError("generator ignored GeneratorExit")
273 289
274 @classmethod 290 @classmethod
275 def __subclasshook__(cls, C): 291 def __subclasshook__(cls, C):
276 if cls is Generator: 292 if cls is Generator:
277 return _check_methods(C, '__iter__', '__next__', 293 return _check_methods(C, '__iter__', '__next__',
278 'send', 'throw', 'close') 294 'send', 'throw', 'close')
279 return NotImplemented 295 return NotImplemented
280 296
281
282 Generator.register(generator) 297 Generator.register(generator)
283 298
284 299
285 class Sized(metaclass=ABCMeta): 300 class Sized(metaclass=ABCMeta):
286 301
287 __slots__ = () 302 __slots__ = ()
288 303
289 @abstractmethod 304 @abstractmethod
290 def __len__(self): 305 def __len__(self):
291 return 0 306 return 0
(...skipping 25 matching lines...) Expand all
317 __slots__ = () 332 __slots__ = ()
318 333
319 @abstractmethod 334 @abstractmethod
320 def __call__(self, *args, **kwds): 335 def __call__(self, *args, **kwds):
321 return False 336 return False
322 337
323 @classmethod 338 @classmethod
324 def __subclasshook__(cls, C): 339 def __subclasshook__(cls, C):
325 if cls is Callable: 340 if cls is Callable:
326 return _check_methods(C, "__call__") 341 return _check_methods(C, "__call__")
327 return NotImplemented
328
329
330 class Reversible(Iterable):
331
332 __slots__ = ()
333
334 @abstractmethod
335 def __reversed__(self):
336 while False:
337 yield None
338
339 @classmethod
340 def __subclasshook__(cls, C):
341 if cls is Reversible:
342 return _check_methods(C, "__reversed__", "__iter__")
343 return NotImplemented 342 return NotImplemented
344 343
345 344
346 ### SETS ### 345 ### SETS ###
347 346
348 347
349 class Set(Sized, Iterable, Container): 348 class Set(Sized, Iterable, Container):
350 349
351 """A set is a finite, iterable container. 350 """A set is a finite, iterable container.
352 351
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 def _from_iterable(self, it): 662 def _from_iterable(self, it):
664 return set(it) 663 return set(it)
665 664
666 def __contains__(self, item): 665 def __contains__(self, item):
667 key, value = item 666 key, value = item
668 try: 667 try:
669 v = self._mapping[key] 668 v = self._mapping[key]
670 except KeyError: 669 except KeyError:
671 return False 670 return False
672 else: 671 else:
673 return v == value 672 return v is value or v == value
674 673
675 def __iter__(self): 674 def __iter__(self):
676 for key in self._mapping: 675 for key in self._mapping:
677 yield (key, self._mapping[key]) 676 yield (key, self._mapping[key])
678 677
679 ItemsView.register(dict_items) 678 ItemsView.register(dict_items)
680 679
681 680
682 class ValuesView(MappingView): 681 class ValuesView(MappingView):
683 682
684 __slots__ = () 683 __slots__ = ()
685 684
686 def __contains__(self, value): 685 def __contains__(self, value):
687 for key in self._mapping: 686 for key in self._mapping:
688 if value == self._mapping[key]: 687 v = self._mapping[key]
688 if v is value or v == value:
689 return True 689 return True
690 return False 690 return False
691 691
692 def __iter__(self): 692 def __iter__(self):
693 for key in self._mapping: 693 for key in self._mapping:
694 yield self._mapping[key] 694 yield self._mapping[key]
695 695
696 ValuesView.register(dict_values) 696 ValuesView.register(dict_values)
697 697
698 698
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
813 try: 813 try:
814 while True: 814 while True:
815 v = self[i] 815 v = self[i]
816 yield v 816 yield v
817 i += 1 817 i += 1
818 except IndexError: 818 except IndexError:
819 return 819 return
820 820
821 def __contains__(self, value): 821 def __contains__(self, value):
822 for v in self: 822 for v in self:
823 if v == value: 823 if v is value or v == value:
824 return True 824 return True
825 return False 825 return False
826 826
827 def __reversed__(self): 827 def __reversed__(self):
828 for i in reversed(range(len(self))): 828 for i in reversed(range(len(self))):
829 yield self[i] 829 yield self[i]
830 830
831 def index(self, value, start=0, stop=None): 831 def index(self, value, start=0, stop=None):
832 '''S.index(value, [start, [stop]]) -> integer -- return first index of v alue. 832 '''S.index(value, [start, [stop]]) -> integer -- return first index of v alue.
833 Raises ValueError if the value is not present. 833 Raises ValueError if the value is not present.
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 Raise ValueError if the value is not present. 930 Raise ValueError if the value is not present.
931 ''' 931 '''
932 del self[self.index(value)] 932 del self[self.index(value)]
933 933
934 def __iadd__(self, values): 934 def __iadd__(self, values):
935 self.extend(values) 935 self.extend(values)
936 return self 936 return self
937 937
938 MutableSequence.register(list) 938 MutableSequence.register(list)
939 MutableSequence.register(bytearray) # Multiply inheriting, see ByteString 939 MutableSequence.register(bytearray) # Multiply inheriting, see ByteString
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+