diff -r adf72cffceb7 Doc/library/collections.abc.rst --- a/Doc/library/collections.abc.rst Thu May 28 10:52:46 2015 -0400 +++ b/Doc/library/collections.abc.rst Thu May 28 11:11:18 2015 -0400 @@ -82,7 +82,7 @@ :class:`Set` ``__iter__`` :class:`ValuesView` :class:`MappingView` ``__contains__``, ``__iter__`` :class:`Awaitable` ``__await__`` -:class:`Coroutine` ``send``, ``throw`` ``close`` +:class:`Coroutine` :class:`Awaitable` ``send``, ``throw`` ``close`` :class:`AsyncIterable` ``__aiter__`` :class:`AsyncIterator` :class:`AsyncIterable` ``__anext__`` ``__aiter__`` ========================== ====================== ======================= ==================================================== diff -r adf72cffceb7 Lib/_collections_abc.py --- a/Lib/_collections_abc.py Thu May 28 10:52:46 2015 -0400 +++ b/Lib/_collections_abc.py Thu May 28 11:11:18 2015 -0400 @@ -75,7 +75,7 @@ return NotImplemented -class _CoroutineMeta(ABCMeta): +class _AwaitableMeta(ABCMeta): def __instancecheck__(cls, instance): # 0x80 = CO_COROUTINE @@ -92,7 +92,26 @@ return super().__instancecheck__(instance) -class Coroutine(metaclass=_CoroutineMeta): +class Awaitable(metaclass=_AwaitableMeta): + + __slots__ = () + + @abstractmethod + def __await__(self): + yield + + @classmethod + def __subclasshook__(cls, C): + if cls is Awaitable: + for B in C.__mro__: + if "__await__" in B.__dict__: + if B.__dict__["__await__"]: + return True + break + return NotImplemented + + +class Coroutine(Awaitable): __slots__ = () @@ -126,27 +145,19 @@ else: raise RuntimeError("coroutine ignored GeneratorExit") - -class Awaitable(metaclass=_CoroutineMeta): - - __slots__ = () - - @abstractmethod - def __await__(self): - yield - @classmethod def __subclasshook__(cls, C): - if cls is Awaitable: - for B in C.__mro__: - if "__await__" in B.__dict__: - if B.__dict__["__await__"]: - return True - break + if cls is Coroutine: + mro = C.__mro__ + for method in ('__await__', 'send', 'throw', 'close'): + for base in mro: + if method in base.__dict__: + break + else: + return NotImplemented + return True return NotImplemented -Awaitable.register(Coroutine) - class AsyncIterable(metaclass=ABCMeta): diff -r adf72cffceb7 Lib/test/test_collections.py --- a/Lib/test/test_collections.py Thu May 28 10:52:46 2015 -0400 +++ b/Lib/test/test_collections.py Thu May 28 11:11:18 2015 -0400 @@ -496,6 +496,8 @@ return value def throw(self, typ, val=None, tb=None): super().throw(typ, val, tb) + def __await__(self): + yield non_samples = [None, int(), gen(), object()] for x in non_samples: @@ -515,13 +517,7 @@ self.assertIsInstance(c, Awaitable) c.close() # awoid RuntimeWarning that coro() was not awaited - class CoroLike: - def send(self, value): - pass - def throw(self, typ, val=None, tb=None): - pass - def close(self): - pass + class CoroLike: pass Coroutine.register(CoroLike) self.assertTrue(isinstance(CoroLike(), Awaitable)) self.assertTrue(issubclass(CoroLike, Awaitable)) @@ -548,6 +544,8 @@ return value def throw(self, typ, val=None, tb=None): super().throw(typ, val, tb) + def __await__(self): + yield non_samples = [None, int(), gen(), object(), Bar()] for x in non_samples: @@ -567,6 +565,28 @@ self.assertIsInstance(c, Coroutine) c.close() # awoid RuntimeWarning that coro() was not awaited + class CoroLike: + def send(self, value): + pass + def throw(self, typ, val=None, tb=None): + pass + def close(self): + pass + def __await__(self): + pass + self.assertTrue(isinstance(CoroLike(), Coroutine)) + self.assertTrue(issubclass(CoroLike, Coroutine)) + + class CoroLike: + def send(self, value): + pass + def close(self): + pass + def __await__(self): + pass + self.assertFalse(isinstance(CoroLike(), Coroutine)) + self.assertFalse(issubclass(CoroLike, Coroutine)) + def test_Hashable(self): # Check some non-hashables non_samples = [bytearray(), list(), set(), dict()]