Commit d8853365 authored by Stefan Behnel's avatar Stefan Behnel

make internal marker types awaitable in older Python versions

parent cd70846b
...@@ -113,6 +113,9 @@ PyDoc_STRVAR(__Pyx_async_gen_throw_doc, ...@@ -113,6 +113,9 @@ PyDoc_STRVAR(__Pyx_async_gen_throw_doc,
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\ "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
return next yielded value or raise StopIteration."); return next yielded value or raise StopIteration.");
PyDoc_STRVAR(__Pyx_async_gen_await_doc,
"__await__() -> return a representation that can be passed into the 'await' expression.");
// COPY STARTS HERE: // COPY STARTS HERE:
static PyObject *__Pyx_async_gen_asend_new(__pyx_PyAsyncGenObject *, PyObject *); static PyObject *__Pyx_async_gen_asend_new(__pyx_PyAsyncGenObject *, PyObject *);
...@@ -535,6 +538,7 @@ static PyMethodDef __Pyx_async_gen_asend_methods[] = { ...@@ -535,6 +538,7 @@ static PyMethodDef __Pyx_async_gen_asend_methods[] = {
{"send", (PyCFunction)__Pyx_async_gen_asend_send, METH_O, __Pyx_async_gen_send_doc}, {"send", (PyCFunction)__Pyx_async_gen_asend_send, METH_O, __Pyx_async_gen_send_doc},
{"throw", (PyCFunction)__Pyx_async_gen_asend_throw, METH_VARARGS, __Pyx_async_gen_throw_doc}, {"throw", (PyCFunction)__Pyx_async_gen_asend_throw, METH_VARARGS, __Pyx_async_gen_throw_doc},
{"close", (PyCFunction)__Pyx_async_gen_asend_close, METH_NOARGS, __Pyx_async_gen_close_doc}, {"close", (PyCFunction)__Pyx_async_gen_asend_close, METH_NOARGS, __Pyx_async_gen_close_doc},
{"__await__", (PyCFunction)PyObject_SelfIter, METH_NOARGS, __Pyx_async_gen_await_doc},
{0, 0, 0, 0} /* Sentinel */ {0, 0, 0, 0} /* Sentinel */
}; };
...@@ -921,6 +925,7 @@ static PyMethodDef __Pyx_async_gen_athrow_methods[] = { ...@@ -921,6 +925,7 @@ static PyMethodDef __Pyx_async_gen_athrow_methods[] = {
{"send", (PyCFunction)__Pyx_async_gen_athrow_send, METH_O, __Pyx_async_gen_send_doc}, {"send", (PyCFunction)__Pyx_async_gen_athrow_send, METH_O, __Pyx_async_gen_send_doc},
{"throw", (PyCFunction)__Pyx_async_gen_athrow_throw, METH_VARARGS, __Pyx_async_gen_throw_doc}, {"throw", (PyCFunction)__Pyx_async_gen_athrow_throw, METH_VARARGS, __Pyx_async_gen_throw_doc},
{"close", (PyCFunction)__Pyx_async_gen_athrow_close, METH_NOARGS, __Pyx_async_gen_close_doc}, {"close", (PyCFunction)__Pyx_async_gen_athrow_close, METH_NOARGS, __Pyx_async_gen_close_doc},
{"__await__", (PyCFunction)PyObject_SelfIter, METH_NOARGS, __Pyx_async_gen_await_doc},
{0, 0, 0, 0} /* Sentinel */ {0, 0, 0, 0} /* Sentinel */
}; };
......
...@@ -33,6 +33,19 @@ else: ...@@ -33,6 +33,19 @@ else:
return c return c
try:
from types import coroutine as types_coroutine
except ImportError:
def types_coroutine(f):
return f
try:
from inspect import isawaitable as inspect_isawaitable
except ImportError:
def inspect_isawaitable(o):
return hasattr(o, '__await__')
# compiled exec() # compiled exec()
def exec(code_string, l, g): def exec(code_string, l, g):
from Cython.Compiler.Errors import CompileError from Cython.Compiler.Errors import CompileError
...@@ -57,7 +70,7 @@ class AwaitException(Exception): ...@@ -57,7 +70,7 @@ class AwaitException(Exception):
pass pass
@types.coroutine @types_coroutine
def awaitable(*, throw=False): def awaitable(*, throw=False):
if throw: if throw:
yield ('throw',) yield ('throw',)
...@@ -373,7 +386,7 @@ class AsyncGenTest(unittest.TestCase): ...@@ -373,7 +386,7 @@ class AsyncGenTest(unittest.TestCase):
self.assertFalse(g.ag_running) self.assertFalse(g.ag_running)
#self.assertIsInstance(g.ag_code, types.CodeType) #self.assertIsInstance(g.ag_code, types.CodeType)
self.assertTrue(inspect.isawaitable(g.aclose())) self.assertTrue(inspect_isawaitable(g.aclose()))
@requires_asyncio @requires_asyncio
......
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