Commit 7c6073cf authored by Stefan Behnel's avatar Stefan Behnel

make Coroutine ABC inherit from Awaitable

parent c22c0fb9
...@@ -1451,22 +1451,49 @@ Generator.register(_cython_generator_type) ...@@ -1451,22 +1451,49 @@ Generator.register(_cython_generator_type)
#endif #endif
#ifdef __Pyx_Coroutine_USED #ifdef __Pyx_Coroutine_USED
CSTRING("""\ CSTRING("""\
def mk_coroutine(): def mk_awaitable():
from abc import abstractmethod, ABCMeta from abc import abstractmethod, ABCMeta
""") """)
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
CSTRING("""\ CSTRING("""\
class Coroutine(metaclass=ABCMeta): class Awaitable(metaclass=ABCMeta):
""") """)
#else #else
CSTRING("""\ CSTRING("""\
class Coroutine(object): class Awaitable(object):
__metaclass__ = ABCMeta __metaclass__ = ABCMeta
""") """)
#endif #endif
CSTRING("""\ CSTRING("""\
__slots__ = () __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
return Awaitable
try:
Awaitable = _module.Awaitable
except AttributeError:
Awaitable = _module.Awaitable = mk_awaitable()
def mk_coroutine():
from abc import abstractmethod, ABCMeta
class Coroutine(Awaitable):
__slots__ = ()
@abstractmethod @abstractmethod
def send(self, value): def send(self, value):
'''Send a value into the coroutine. '''Send a value into the coroutine.
...@@ -1497,53 +1524,27 @@ def mk_coroutine(): ...@@ -1497,53 +1524,27 @@ def mk_coroutine():
else: else:
raise RuntimeError('coroutine ignored GeneratorExit') raise RuntimeError('coroutine ignored GeneratorExit')
return Coroutine
try:
Coroutine = _module.Coroutine
except AttributeError:
Coroutine = _module.Coroutine = mk_coroutine()
Coroutine.register(_cython_coroutine_type)
def mk_awaitable():
from abc import abstractmethod, ABCMeta
""")
#if PY_MAJOR_VERSION >= 3
CSTRING("""\
class Awaitable(metaclass=ABCMeta):
""")
#else
CSTRING("""\
class Awaitable(object):
__metaclass__ = ABCMeta
""")
#endif
CSTRING("""\
__slots__ = ()
@abstractmethod
def __await__(self):
yield
@classmethod @classmethod
def __subclasshook__(cls, C): def __subclasshook__(cls, C):
if cls is Awaitable: if cls is Coroutine:
for B in C.__mro__: mro = C.__mro__
if '__await__' in B.__dict__: for method in ('__await__', 'send', 'throw', 'close'):
if B.__dict__['__await__']: for base in mro:
return True if method in base.__dict__:
break break
else:
return NotImplemented
return True
return NotImplemented return NotImplemented
return Awaitable
return Coroutine
try: try:
Awaitable = _module.Awaitable Coroutine = _module.Coroutine
except AttributeError: except AttributeError:
Awaitable = _module.Awaitable = mk_awaitable() Coroutine = _module.Coroutine = mk_coroutine()
Awaitable.register(Coroutine) Coroutine.register(_cython_coroutine_type)
Awaitable.register(_cython_coroutine_type)
""") """)
#endif #endif
); );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment