| 1 | import pytest |
| 2 | from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 3 | from pypy.conftest import option |
| 4 | |
| 5 | |
| 6 | @pytest.mark.skip("too slow, over 30 seconds for this one test") |
| 7 | class AppTestAsyncIter(AppTestCpythonExtensionBase): |
| 8 | enable_leak_checking = True |
| 9 | |
| 10 | def test_asyncgen(self): |
| 11 | """ module is this code after running through cython |
| 12 | async def test_gen(): |
| 13 | a = yield 123 |
| 14 | assert a is None |
| 15 | yield 456 |
| 16 | yield 789 |
| 17 | |
| 18 | def run_until_complete(coro): |
| 19 | while True: |
| 20 | try: |
| 21 | fut = coro.send(None) |
| 22 | except StopIteration as ex: |
| 23 | return ex.args[0] |
| 24 | |
| 25 | def to_list(gen): |
| 26 | async def iterate(): |
| 27 | res = [] |
| 28 | async for i in gen: |
| 29 | res.append(i) |
| 30 | return res |
| 31 | |
| 32 | return run_until_complete(iterate()) |
| 33 | """ |
| 34 | module = self.import_module(name='test_asyncgen') |
| 35 | result = module.to_list(module.test_gen()) |
| 36 | assert result == [123, 456, 789] |
| 37 | |
| 38 | |
| 39 | def test_async_gen_exception_04(self): |
| 40 | """module is this code after running through cython, then making some |
| 41 | small adjustments (see https://github.com/cython/cython/pull/5429) |
| 42 | ZERO = 0 |
| 43 | |
| 44 | async def gen(): |
| 45 | yield 123 |
| 46 | 1 / ZERO |
| 47 | |
| 48 | def test_last_yield(g): |
| 49 | ai = g.__aiter__() |
| 50 | an = ai.__anext__() |
| 51 | try: |
| 52 | next(an) |
| 53 | except StopIteration as ex: |
| 54 | return ex.args |
| 55 | else: |
| 56 | return None |
| 57 | |
| 58 | """ |
| 59 | module = self.import_module(name='test_async_gen_exception_04') |
| 60 | g = module.gen() |
| 61 | result = module.test_last_yield(g) |
| 62 | assert result == 123 |
| 63 | |
| 64 | def test_coro_retval(self): |
| 65 | """ |
| 66 | # Check that the final result of a coroutine is available in the StopIteration |
| 67 | # that should be raised by the final call to its tp_iternext method |
| 68 | body = ''' |
| 69 | static PyObject *value_from_stopiteration(void) |
| 70 | { |
| 71 | PyObject *ptype, *pvalue, *ptraceback, *return_value; |
| 72 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
| 73 | if (PyErr_GivenExceptionMatches(pvalue, PyExc_StopIteration)) { |
| 74 | return_value = PyObject_GetAttrString(pvalue, "value"); |
| 75 | Py_XDECREF(pvalue); |
| 76 | } |
| 77 | else { |
| 78 | return_value = pvalue; |
| 79 | } |
| 80 | Py_XDECREF(ptype); |
| 81 | Py_XDECREF(ptraceback); |
| 82 | return return_value; |
| 83 | } |
| 84 | |
| 85 | static PyObject *exhaust_coro(PyObject *self, PyObject *args) |
| 86 | { |
| 87 | PyObject *coro; |
| 88 | if (!PyArg_ParseTuple(args, "O", &coro)) { |
| 89 | return NULL; |
| 90 | } |
| 91 | PyObject *coro_wrapper = PyObject_CallMethod(coro, "__await__", NULL); |
| 92 | if (coro_wrapper == NULL) { |
| 93 | return NULL; |
| 94 | } |
| 95 | PyObject *result; |
| 96 | iternextfunc next = Py_TYPE(coro_wrapper)->tp_iternext; |
| 97 | while (1) { |
| 98 | PyObject *value = next(coro_wrapper); |
| 99 | if (value) { |
| 100 | continue; |
| 101 | } |
| 102 | else if (!PyErr_Occurred()) { |
| 103 | PyErr_SetString(PyExc_AssertionError, "coroutine finished but there was no exception raised"); |
| 104 | return NULL; |
| 105 | } |
| 106 | else if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 107 | result = NULL; |
| 108 | } |
| 109 | else { |
| 110 | result = value_from_stopiteration(); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | Py_DECREF(coro_wrapper); |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | static PyMethodDef methods[] = { |
| 120 | {"exhaust_coro", exhaust_coro, METH_VARARGS}, |
| 121 | {NULL} |
| 122 | }; |
| 123 | |
| 124 | static struct PyModuleDef moduledef = { |
| 125 | PyModuleDef_HEAD_INIT, "test_coro_retval", NULL, -1, methods |
| 126 | }; |
| 127 | ''' |
| 128 | test_coro_retval = self.import_module(name='test_coro_retval', body=body) |
| 129 | |
| 130 | async def test_coro(): |
| 131 | return "hi coro" |
| 132 | assert test_coro_retval.exhaust_coro(test_coro()) == "hi coro" |
| 133 | """ |