| 1 | from rpython.rtyper.lltypesystem import lltype |
| 2 | from pypy.interpreter.generator import GeneratorIterator, Coroutine |
| 3 | from pypy.module.cpyext.api import ( |
| 4 | build_type_checkers, cts, parse_dir, bootstrap_function, slot_function) |
| 5 | from pypy.module.cpyext.pyobject import PyObject, make_typedescr, as_pyobj |
| 6 | from pypy.module.cpyext.object import _dealloc |
| 7 | |
| 8 | cts.parse_header(parse_dir / 'cpyext_genobject.h') |
| 9 | |
| 10 | @bootstrap_function |
| 11 | def init_genobject(space): |
| 12 | make_typedescr(GeneratorIterator.typedef, |
| 13 | basestruct=cts.gettype('PyGenObject'), |
| 14 | attach=gi_attach, |
| 15 | dealloc=gi_dealloc) |
| 16 | |
| 17 | |
| 18 | PyGen_Check, PyGen_CheckExact = build_type_checkers("Gen", GeneratorIterator) |
| 19 | |
| 20 | _, PyCoro_CheckExact = build_type_checkers("Coro", Coroutine) |
| 21 | |
| 22 | def gi_attach(space, py_obj, w_obj, w_userdata=None): |
| 23 | assert isinstance(w_obj, GeneratorIterator) |
| 24 | cts.cast('PyGenObject*', py_obj).c_gi_code = as_pyobj(space, w_obj.pycode) |
| 25 | |
| 26 | def gi_realize(space, py_obj): |
| 27 | raise NotImplementedError( |
| 28 | "PyPy doesn't support creation of generators from the C-API.") |
| 29 | |
| 30 | @slot_function([PyObject], lltype.Void) |
| 31 | def gi_dealloc(space, py_obj): |
| 32 | _dealloc(space, py_obj) |