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)
#endif
#ifdef __Pyx_Coroutine_USED
CSTRING("""\
def mk_coroutine():
def mk_awaitable():
from abc import abstractmethod, ABCMeta
""")
#if PY_MAJOR_VERSION >= 3
CSTRING("""\
class Coroutine(metaclass=ABCMeta):
class Awaitable(metaclass=ABCMeta):
""")
#else
CSTRING("""\
class Coroutine(object):
class Awaitable(object):
__metaclass__ = ABCMeta
""")
#endif
CSTRING("""\
__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
def send(self, value):
'''Send a value into the coroutine.
......@@ -1497,6 +1524,20 @@ def mk_coroutine():
else:
raise RuntimeError('coroutine ignored GeneratorExit')
@classmethod
def __subclasshook__(cls, C):
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
return Coroutine
try:
......@@ -1504,46 +1545,6 @@ try:
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
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()
Awaitable.register(Coroutine)
Awaitable.register(_cython_coroutine_type)
""")
#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