blob: 96f8dcc74d8ce28654bb724fc4f0b4b3c5d648dc [file] [log] [blame]
Martin v. Löwise440e472004-06-01 15:22:42 +00001
2/* Generator object interface */
3
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004#ifndef Py_LIMITED_API
Martin v. Löwise440e472004-06-01 15:22:42 +00005#ifndef Py_GENOBJECT_H
6#define Py_GENOBJECT_H
7#ifdef __cplusplus
8extern "C" {
9#endif
10
Victor Stinnered488662019-05-20 00:14:57 +020011#include "pystate.h" /* _PyErr_StackItem */
12
Martin v. Löwis8d97e332004-06-27 15:43:12 +000013struct _frame; /* Avoid including frameobject.h */
14
Yury Selivanov5376ba92015-06-22 12:19:30 -040015/* _PyGenObject_HEAD defines the initial segment of generator
16 and coroutine objects. */
17#define _PyGenObject_HEAD(prefix) \
18 PyObject_HEAD \
19 /* Note: gi_frame can be NULL if the generator is "finished" */ \
20 struct _frame *prefix##_frame; \
21 /* True if generator is being executed. */ \
22 char prefix##_running; \
23 /* The code object backing the generator */ \
24 PyObject *prefix##_code; \
25 /* List of weak reference. */ \
26 PyObject *prefix##_weakreflist; \
27 /* Name of the generator. */ \
28 PyObject *prefix##_name; \
29 /* Qualified name of the generator. */ \
Mark Shannonae3087c2017-10-22 22:41:51 +010030 PyObject *prefix##_qualname; \
31 _PyErr_StackItem prefix##_exc_state;
Yury Selivanov5376ba92015-06-22 12:19:30 -040032
Martin v. Löwise440e472004-06-01 15:22:42 +000033typedef struct {
Nick Coghlan76e1bb02012-01-14 16:08:08 +100034 /* The gi_ prefix is intended to remind of generator-iterator. */
Yury Selivanov5376ba92015-06-22 12:19:30 -040035 _PyGenObject_HEAD(gi)
Martin v. Löwise440e472004-06-01 15:22:42 +000036} PyGenObject;
37
38PyAPI_DATA(PyTypeObject) PyGen_Type;
39
40#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
Christian Heimes90aa7642007-12-19 02:45:37 +000041#define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
Martin v. Löwise440e472004-06-01 15:22:42 +000042
Martin v. Löwis8d97e332004-06-27 15:43:12 +000043PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
Victor Stinner40ee3012014-06-16 15:59:28 +020044PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
45 PyObject *name, PyObject *qualname);
Serhiy Storchaka24411f82016-11-06 18:44:42 +020046PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
Nick Coghlanc40bc092012-06-17 15:15:49 +100047PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
Yury Selivanov833c6262016-10-28 18:48:50 -040048PyAPI_FUNC(PyObject *) _PyGen_Send(PyGenObject *, PyObject *);
Yury Selivanovc724bae2016-03-02 11:30:46 -050049PyObject *_PyGen_yf(PyGenObject *);
Antoine Pitrou58720d62013-08-05 23:26:40 +020050PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
51
Yury Selivanov5376ba92015-06-22 12:19:30 -040052#ifndef Py_LIMITED_API
53typedef struct {
54 _PyGenObject_HEAD(cr)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -080055 PyObject *cr_origin;
Yury Selivanov5376ba92015-06-22 12:19:30 -040056} PyCoroObject;
57
58PyAPI_DATA(PyTypeObject) PyCoro_Type;
59PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type;
60
Yury Selivanova6f6edb2016-06-09 15:08:31 -040061PyAPI_DATA(PyTypeObject) _PyAIterWrapper_Type;
Yury Selivanova6f6edb2016-06-09 15:08:31 -040062
Yury Selivanov5376ba92015-06-22 12:19:30 -040063#define PyCoro_CheckExact(op) (Py_TYPE(op) == &PyCoro_Type)
64PyObject *_PyCoro_GetAwaitableIter(PyObject *o);
65PyAPI_FUNC(PyObject *) PyCoro_New(struct _frame *,
66 PyObject *name, PyObject *qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -070067
68/* Asynchronous Generators */
69
70typedef struct {
71 _PyGenObject_HEAD(ag)
72 PyObject *ag_finalizer;
73
74 /* Flag is set to 1 when hooks set up by sys.set_asyncgen_hooks
75 were called on the generator, to avoid calling them more
76 than once. */
77 int ag_hooks_inited;
78
79 /* Flag is set to 1 when aclose() is called for the first time, or
80 when a StopAsyncIteration exception is raised. */
81 int ag_closed;
Yury Selivanovfc4a0442019-09-29 22:59:11 -070082
83 int ag_running_async;
Yury Selivanoveb636452016-09-08 22:01:51 -070084} PyAsyncGenObject;
85
86PyAPI_DATA(PyTypeObject) PyAsyncGen_Type;
87PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type;
88PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type;
89PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type;
90
91PyAPI_FUNC(PyObject *) PyAsyncGen_New(struct _frame *,
92 PyObject *name, PyObject *qualname);
93
94#define PyAsyncGen_CheckExact(op) (Py_TYPE(op) == &PyAsyncGen_Type)
95
96PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
97
98int PyAsyncGen_ClearFreeLists(void);
99
Yury Selivanov5376ba92015-06-22 12:19:30 -0400100#endif
101
102#undef _PyGenObject_HEAD
Martin v. Löwise440e472004-06-01 15:22:42 +0000103
104#ifdef __cplusplus
105}
106#endif
107#endif /* !Py_GENOBJECT_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000108#endif /* Py_LIMITED_API */