blob: e965334a0140c8097351c391f5314d58b7e799d5 [file] [log] [blame]
Haibo Huangd8830302020-03-03 10:09:46 -08001
2/* Generator object interface */
3
4#ifndef Py_LIMITED_API
5#ifndef Py_GENOBJECT_H
6#define Py_GENOBJECT_H
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include "pystate.h" /* _PyErr_StackItem */
Yi Kong71199322022-08-30 15:53:45 +080012#include "abstract.h" /* PySendResult */
Haibo Huangd8830302020-03-03 10:09:46 -080013
Haibo Huangd8830302020-03-03 10:09:46 -080014/* _PyGenObject_HEAD defines the initial segment of generator
15 and coroutine objects. */
16#define _PyGenObject_HEAD(prefix) \
17 PyObject_HEAD \
18 /* Note: gi_frame can be NULL if the generator is "finished" */ \
Haibo Huang5eba2b42021-01-22 11:22:02 -080019 PyFrameObject *prefix##_frame; \
Haibo Huangd8830302020-03-03 10:09:46 -080020 /* The code object backing the generator */ \
21 PyObject *prefix##_code; \
22 /* List of weak reference. */ \
23 PyObject *prefix##_weakreflist; \
24 /* Name of the generator. */ \
25 PyObject *prefix##_name; \
26 /* Qualified name of the generator. */ \
27 PyObject *prefix##_qualname; \
28 _PyErr_StackItem prefix##_exc_state;
29
30typedef struct {
31 /* The gi_ prefix is intended to remind of generator-iterator. */
32 _PyGenObject_HEAD(gi)
33} PyGenObject;
34
35PyAPI_DATA(PyTypeObject) PyGen_Type;
36
37#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
Haibo Huang5eba2b42021-01-22 11:22:02 -080038#define PyGen_CheckExact(op) Py_IS_TYPE(op, &PyGen_Type)
Haibo Huangd8830302020-03-03 10:09:46 -080039
Haibo Huang5eba2b42021-01-22 11:22:02 -080040PyAPI_FUNC(PyObject *) PyGen_New(PyFrameObject *);
41PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(PyFrameObject *,
Haibo Huangd8830302020-03-03 10:09:46 -080042 PyObject *name, PyObject *qualname);
Haibo Huangd8830302020-03-03 10:09:46 -080043PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
44PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
Haibo Huangd8830302020-03-03 10:09:46 -080045PyObject *_PyGen_yf(PyGenObject *);
46PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
47
48#ifndef Py_LIMITED_API
49typedef struct {
50 _PyGenObject_HEAD(cr)
51 PyObject *cr_origin;
52} PyCoroObject;
53
54PyAPI_DATA(PyTypeObject) PyCoro_Type;
55PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type;
56
Haibo Huang5eba2b42021-01-22 11:22:02 -080057#define PyCoro_CheckExact(op) Py_IS_TYPE(op, &PyCoro_Type)
Haibo Huangd8830302020-03-03 10:09:46 -080058PyObject *_PyCoro_GetAwaitableIter(PyObject *o);
Haibo Huang5eba2b42021-01-22 11:22:02 -080059PyAPI_FUNC(PyObject *) PyCoro_New(PyFrameObject *,
Haibo Huangd8830302020-03-03 10:09:46 -080060 PyObject *name, PyObject *qualname);
61
62/* Asynchronous Generators */
63
64typedef struct {
65 _PyGenObject_HEAD(ag)
66 PyObject *ag_finalizer;
67
68 /* Flag is set to 1 when hooks set up by sys.set_asyncgen_hooks
69 were called on the generator, to avoid calling them more
70 than once. */
71 int ag_hooks_inited;
72
73 /* Flag is set to 1 when aclose() is called for the first time, or
74 when a StopAsyncIteration exception is raised. */
75 int ag_closed;
76
77 int ag_running_async;
78} PyAsyncGenObject;
79
80PyAPI_DATA(PyTypeObject) PyAsyncGen_Type;
81PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type;
82PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type;
83PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type;
84
Haibo Huang5eba2b42021-01-22 11:22:02 -080085PyAPI_FUNC(PyObject *) PyAsyncGen_New(PyFrameObject *,
Haibo Huangd8830302020-03-03 10:09:46 -080086 PyObject *name, PyObject *qualname);
87
Haibo Huang5eba2b42021-01-22 11:22:02 -080088#define PyAsyncGen_CheckExact(op) Py_IS_TYPE(op, &PyAsyncGen_Type)
Haibo Huangd8830302020-03-03 10:09:46 -080089
90PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
91
Haibo Huangd8830302020-03-03 10:09:46 -080092#endif
93
94#undef _PyGenObject_HEAD
95
96#ifdef __cplusplus
97}
98#endif
99#endif /* !Py_GENOBJECT_H */
100#endif /* Py_LIMITED_API */