diff -r bc1a178b3bc8 Lib/_collections_abc.py --- a/Lib/_collections_abc.py Sat Apr 18 05:54:02 2015 +0200 +++ b/Lib/_collections_abc.py Mon Apr 20 21:33:43 2015 +0200 @@ -9,7 +9,7 @@ from abc import ABCMeta, abstractmethod import sys -__all__ = ["Hashable", "Iterable", "Iterator", +__all__ = ["Hashable", "Iterable", "Iterator", "Generator", "Sized", "Container", "Callable", "Set", "MutableSet", "Mapping", "MutableMapping", @@ -50,6 +50,7 @@ dict_items = type({}.items()) ## misc ## mappingproxy = type(type.__dict__) +generator = type((lambda: (yield))()) ### ONE-TRICK PONIES ### @@ -124,6 +125,37 @@ Iterator.register(tuple_iterator) Iterator.register(zip_iterator) + +class Generator(Iterator): + + __slots__ = () + + def __next__(self): + """Return the next item from the generator. + When exhausted, raise StopIteration. + """ + return self.send(None) + + @abstractmethod + def send(self, value): + """Send a value into the generator. + Return next yielded value or raise StopIteration. + """ + raise StopIteration + + def throw(self, typ, val=None, rb=None): + """Raise an exception in the generator. + Return next yielded value or raise StopIteration. + """ + raise StopIteration + + def close(self): + """Raise GeneratorExit inside generator. + """ + +Generator.register(generator) + + class Sized(metaclass=ABCMeta): __slots__ = ()