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

Side by Side Diff: Lib/collections/abc.py

Issue 11333: Add empty __slots__ to collections.abc abstract base classes
Patch Set: Created 8 years, 4 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:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 27 matching lines...) Expand all
38 dict_keys = type({}.keys()) 38 dict_keys = type({}.keys())
39 dict_values = type({}.values()) 39 dict_values = type({}.values())
40 dict_items = type({}.items()) 40 dict_items = type({}.items())
41 ## misc ## 41 ## misc ##
42 dict_proxy = type(type.__dict__) 42 dict_proxy = type(type.__dict__)
43 43
44 44
45 ### ONE-TRICK PONIES ### 45 ### ONE-TRICK PONIES ###
46 46
47 class Hashable(metaclass=ABCMeta): 47 class Hashable(metaclass=ABCMeta):
48
49 __slots__ = ()
48 50
49 @abstractmethod 51 @abstractmethod
50 def __hash__(self): 52 def __hash__(self):
51 return 0 53 return 0
52 54
53 @classmethod 55 @classmethod
54 def __subclasshook__(cls, C): 56 def __subclasshook__(cls, C):
55 if cls is Hashable: 57 if cls is Hashable:
56 for B in C.__mro__: 58 for B in C.__mro__:
57 if "__hash__" in B.__dict__: 59 if "__hash__" in B.__dict__:
58 if B.__dict__["__hash__"]: 60 if B.__dict__["__hash__"]:
59 return True 61 return True
60 break 62 break
61 return NotImplemented 63 return NotImplemented
62 64
63 65
64 class Iterable(metaclass=ABCMeta): 66 class Iterable(metaclass=ABCMeta):
67
68 __slots__ = ()
65 69
66 @abstractmethod 70 @abstractmethod
67 def __iter__(self): 71 def __iter__(self):
68 while False: 72 while False:
69 yield None 73 yield None
70 74
71 @classmethod 75 @classmethod
72 def __subclasshook__(cls, C): 76 def __subclasshook__(cls, C):
73 if cls is Iterable: 77 if cls is Iterable:
74 if any("__iter__" in B.__dict__ for B in C.__mro__): 78 if any("__iter__" in B.__dict__ for B in C.__mro__):
75 return True 79 return True
76 return NotImplemented 80 return NotImplemented
77 81
78 82
79 class Iterator(Iterable): 83 class Iterator(Iterable):
84
85 __slots__ = ()
80 86
81 @abstractmethod 87 @abstractmethod
82 def __next__(self): 88 def __next__(self):
83 raise StopIteration 89 raise StopIteration
84 90
85 def __iter__(self): 91 def __iter__(self):
86 return self 92 return self
87 93
88 @classmethod 94 @classmethod
89 def __subclasshook__(cls, C): 95 def __subclasshook__(cls, C):
(...skipping 11 matching lines...) Expand all
101 Iterator.register(dict_itemiterator) 107 Iterator.register(dict_itemiterator)
102 Iterator.register(list_iterator) 108 Iterator.register(list_iterator)
103 Iterator.register(list_reverseiterator) 109 Iterator.register(list_reverseiterator)
104 Iterator.register(range_iterator) 110 Iterator.register(range_iterator)
105 Iterator.register(set_iterator) 111 Iterator.register(set_iterator)
106 Iterator.register(str_iterator) 112 Iterator.register(str_iterator)
107 Iterator.register(tuple_iterator) 113 Iterator.register(tuple_iterator)
108 Iterator.register(zip_iterator) 114 Iterator.register(zip_iterator)
109 115
110 class Sized(metaclass=ABCMeta): 116 class Sized(metaclass=ABCMeta):
117
118 __slots__ = ()
111 119
112 @abstractmethod 120 @abstractmethod
113 def __len__(self): 121 def __len__(self):
114 return 0 122 return 0
115 123
116 @classmethod 124 @classmethod
117 def __subclasshook__(cls, C): 125 def __subclasshook__(cls, C):
118 if cls is Sized: 126 if cls is Sized:
119 if any("__len__" in B.__dict__ for B in C.__mro__): 127 if any("__len__" in B.__dict__ for B in C.__mro__):
120 return True 128 return True
121 return NotImplemented 129 return NotImplemented
122 130
123 131
124 class Container(metaclass=ABCMeta): 132 class Container(metaclass=ABCMeta):
133
134 __slots__ = ()
125 135
126 @abstractmethod 136 @abstractmethod
127 def __contains__(self, x): 137 def __contains__(self, x):
128 return False 138 return False
129 139
130 @classmethod 140 @classmethod
131 def __subclasshook__(cls, C): 141 def __subclasshook__(cls, C):
132 if cls is Container: 142 if cls is Container:
133 if any("__contains__" in B.__dict__ for B in C.__mro__): 143 if any("__contains__" in B.__dict__ for B in C.__mro__):
134 return True 144 return True
135 return NotImplemented 145 return NotImplemented
136 146
137 147
138 class Callable(metaclass=ABCMeta): 148 class Callable(metaclass=ABCMeta):
149
150 __slots__ = ()
139 151
140 @abstractmethod 152 @abstractmethod
141 def __call__(self, *args, **kwds): 153 def __call__(self, *args, **kwds):
142 return False 154 return False
143 155
144 @classmethod 156 @classmethod
145 def __subclasshook__(cls, C): 157 def __subclasshook__(cls, C):
146 if cls is Callable: 158 if cls is Callable:
147 if any("__call__" in B.__dict__ for B in C.__mro__): 159 if any("__call__" in B.__dict__ for B in C.__mro__):
148 return True 160 return True
149 return NotImplemented 161 return NotImplemented
150 162
151 163
152 ### SETS ### 164 ### SETS ###
153 165
154 166
155 class Set(Sized, Iterable, Container): 167 class Set(Sized, Iterable, Container):
156 168
157 """A set is a finite, iterable container. 169 """A set is a finite, iterable container.
158 170
159 This class provides concrete generic implementations of all 171 This class provides concrete generic implementations of all
160 methods except for __contains__, __iter__ and __len__. 172 methods except for __contains__, __iter__ and __len__.
161 173
162 To override the comparisons (presumably for speed, as the 174 To override the comparisons (presumably for speed, as the
163 semantics are fixed), all you have to do is redefine __le__ and 175 semantics are fixed), all you have to do is redefine __le__ and
164 then the other operations will automatically follow suit. 176 then the other operations will automatically follow suit.
165 """ 177 """
178
179 __slots__ = ()
166 180
167 def __le__(self, other): 181 def __le__(self, other):
168 if not isinstance(other, Set): 182 if not isinstance(other, Set):
169 return NotImplemented 183 return NotImplemented
170 if len(self) > len(other): 184 if len(self) > len(other):
171 return False 185 return False
172 for elem in self: 186 for elem in self:
173 if elem not in other: 187 if elem not in other:
174 return False 188 return False
175 return True 189 return True
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 if h > MAX: 281 if h > MAX:
268 h -= MASK + 1 282 h -= MASK + 1
269 if h == -1: 283 if h == -1:
270 h = 590923713 284 h = 590923713
271 return h 285 return h
272 286
273 Set.register(frozenset) 287 Set.register(frozenset)
274 288
275 289
276 class MutableSet(Set): 290 class MutableSet(Set):
291
292 __slots__ = ()
277 293
278 @abstractmethod 294 @abstractmethod
279 def add(self, value): 295 def add(self, value):
280 """Add an element.""" 296 """Add an element."""
281 raise NotImplementedError 297 raise NotImplementedError
282 298
283 @abstractmethod 299 @abstractmethod
284 def discard(self, value): 300 def discard(self, value):
285 """Remove an element. Do not raise an exception if absent.""" 301 """Remove an element. Do not raise an exception if absent."""
286 raise NotImplementedError 302 raise NotImplementedError
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 self.discard(value) 356 self.discard(value)
341 return self 357 return self
342 358
343 MutableSet.register(set) 359 MutableSet.register(set)
344 360
345 361
346 ### MAPPINGS ### 362 ### MAPPINGS ###
347 363
348 364
349 class Mapping(Sized, Iterable, Container): 365 class Mapping(Sized, Iterable, Container):
366
367 __slots__ = ()
350 368
351 @abstractmethod 369 @abstractmethod
352 def __getitem__(self, key): 370 def __getitem__(self, key):
353 raise KeyError 371 raise KeyError
354 372
355 def get(self, key, default=None): 373 def get(self, key, default=None):
356 try: 374 try:
357 return self[key] 375 return self[key]
358 except KeyError: 376 except KeyError:
359 return default 377 return default
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 return False 461 return False
444 462
445 def __iter__(self): 463 def __iter__(self):
446 for key in self._mapping: 464 for key in self._mapping:
447 yield self._mapping[key] 465 yield self._mapping[key]
448 466
449 ValuesView.register(dict_values) 467 ValuesView.register(dict_values)
450 468
451 469
452 class MutableMapping(Mapping): 470 class MutableMapping(Mapping):
471
472 __slots__ = ()
453 473
454 @abstractmethod 474 @abstractmethod
455 def __setitem__(self, key, value): 475 def __setitem__(self, key, value):
456 raise KeyError 476 raise KeyError
457 477
458 @abstractmethod 478 @abstractmethod
459 def __delitem__(self, key): 479 def __delitem__(self, key):
460 raise KeyError 480 raise KeyError
461 481
462 __marker = object() 482 __marker = object()
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 ### SEQUENCES ### 542 ### SEQUENCES ###
523 543
524 544
525 class Sequence(Sized, Iterable, Container): 545 class Sequence(Sized, Iterable, Container):
526 546
527 """All the operations on a read-only sequence. 547 """All the operations on a read-only sequence.
528 548
529 Concrete subclasses must override __new__ or __init__, 549 Concrete subclasses must override __new__ or __init__,
530 __getitem__, and __len__. 550 __getitem__, and __len__.
531 """ 551 """
552
553 __slots__ = ()
532 554
533 @abstractmethod 555 @abstractmethod
534 def __getitem__(self, index): 556 def __getitem__(self, index):
535 raise IndexError 557 raise IndexError
536 558
537 def __iter__(self): 559 def __iter__(self):
538 i = 0 560 i = 0
539 try: 561 try:
540 while True: 562 while True:
541 v = self[i] 563 v = self[i]
(...skipping 25 matching lines...) Expand all
567 Sequence.register(str) 589 Sequence.register(str)
568 Sequence.register(range) 590 Sequence.register(range)
569 591
570 592
571 class ByteString(Sequence): 593 class ByteString(Sequence):
572 594
573 """This unifies bytes and bytearray. 595 """This unifies bytes and bytearray.
574 596
575 XXX Should add all their methods. 597 XXX Should add all their methods.
576 """ 598 """
599
600 __slots__ = ()
577 601
578 ByteString.register(bytes) 602 ByteString.register(bytes)
579 ByteString.register(bytearray) 603 ByteString.register(bytearray)
580 604
581 605
582 class MutableSequence(Sequence): 606 class MutableSequence(Sequence):
583 607
608 __slots__ = ()
609
584 @abstractmethod 610 @abstractmethod
585 def __setitem__(self, index, value): 611 def __setitem__(self, index, value):
586 raise IndexError 612 raise IndexError
587 613
588 @abstractmethod 614 @abstractmethod
589 def __delitem__(self, index): 615 def __delitem__(self, index):
590 raise IndexError 616 raise IndexError
591 617
592 @abstractmethod 618 @abstractmethod
593 def insert(self, index, value): 619 def insert(self, index, value):
(...skipping 18 matching lines...) Expand all
612 638
613 def remove(self, value): 639 def remove(self, value):
614 del self[self.index(value)] 640 del self[self.index(value)]
615 641
616 def __iadd__(self, values): 642 def __iadd__(self, values):
617 self.extend(values) 643 self.extend(values)
618 return self 644 return self
619 645
620 MutableSequence.register(list) 646 MutableSequence.register(list)
621 MutableSequence.register(bytearray) # Multiply inheriting, see ByteString 647 MutableSequence.register(bytearray) # Multiply inheriting, see ByteString
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

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