blob: e965334a0140c8097351c391f5314d58b7e799d5 [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 */
Vladimir Matveev037245c2020-10-09 17:15:15 -070012#include "abstract.h" /* PySendResult */
Victor Stinnered488662019-05-20 00:14:57 +020013
Yury Selivanov5376ba92015-06-22 12:19:30 -040014/* _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" */ \
Victor Stinner7c59d7c2020-04-28 16:32:48 +020019 PyFrameObject *prefix##_frame; \
Yury Selivanov5376ba92015-06-22 12:19:30 -040020 /* 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. */ \
Mark Shannonae3087c2017-10-22 22:41:51 +010027 PyObject *prefix##_qualname; \
28 _PyErr_StackItem prefix##_exc_state;
Yury Selivanov5376ba92015-06-22 12:19:30 -040029
Martin v. Löwise440e472004-06-01 15:22:42 +000030typedef struct {
Nick Coghlan76e1bb02012-01-14 16:08:08 +100031 /* The gi_ prefix is intended to remind of generator-iterator. */
Yury Selivanov5376ba92015-06-22 12:19:30 -040032 _PyGenObject_HEAD(gi)
Martin v. Löwise440e472004-06-01 15:22:42 +000033} PyGenObject;
34
35PyAPI_DATA(PyTypeObject) PyGen_Type;
36
37#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
Dong-hee Nad905df72020-02-14 02:37:17 +090038#define PyGen_CheckExact(op) Py_IS_TYPE(op, &PyGen_Type)
Martin v. Löwise440e472004-06-01 15:22:42 +000039
Victor Stinner7c59d7c2020-04-28 16:32:48 +020040PyAPI_FUNC(PyObject *) PyGen_New(PyFrameObject *);
41PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(PyFrameObject *,
Victor Stinner40ee3012014-06-16 15:59:28 +020042 PyObject *name, PyObject *qualname);
Serhiy Storchaka24411f82016-11-06 18:44:42 +020043PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
Nick Coghlanc40bc092012-06-17 15:15:49 +100044PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
Yury Selivanovc724bae2016-03-02 11:30:46 -050045PyObject *_PyGen_yf(PyGenObject *);
Antoine Pitrou58720d62013-08-05 23:26:40 +020046PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
47
Yury Selivanov5376ba92015-06-22 12:19:30 -040048#ifndef Py_LIMITED_API
49typedef struct {
50 _PyGenObject_HEAD(cr)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -080051 PyObject *cr_origin;
Yury Selivanov5376ba92015-06-22 12:19:30 -040052} PyCoroObject;
53
54PyAPI_DATA(PyTypeObject) PyCoro_Type;
55PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type;
56
Dong-hee Nad905df72020-02-14 02:37:17 +090057#define PyCoro_CheckExact(op) Py_IS_TYPE(op, &PyCoro_Type)
Yury Selivanov5376ba92015-06-22 12:19:30 -040058PyObject *_PyCoro_GetAwaitableIter(PyObject *o);
Victor Stinner7c59d7c2020-04-28 16:32:48 +020059PyAPI_FUNC(PyObject *) PyCoro_New(PyFrameObject *,
Yury Selivanov5376ba92015-06-22 12:19:30 -040060 PyObject *name, PyObject *qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -070061
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;
Yury Selivanovfc4a0442019-09-29 22:59:11 -070076
77 int ag_running_async;
Yury Selivanoveb636452016-09-08 22:01:51 -070078} PyAsyncGenObject;
79
80PyAPI_DATA(PyTypeObject) PyAsyncGen_Type;
81PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type;
82PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type;
83PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type;
84
Victor Stinner7c59d7c2020-04-28 16:32:48 +020085PyAPI_FUNC(PyObject *) PyAsyncGen_New(PyFrameObject *,
Yury Selivanoveb636452016-09-08 22:01:51 -070086 PyObject *name, PyObject *qualname);
87
Dong-hee Nad905df72020-02-14 02:37:17 +090088#define PyAsyncGen_CheckExact(op) Py_IS_TYPE(op, &PyAsyncGen_Type)
Yury Selivanoveb636452016-09-08 22:01:51 -070089
90PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
91
Yury Selivanov5376ba92015-06-22 12:19:30 -040092#endif
93
94#undef _PyGenObject_HEAD
Martin v. Löwise440e472004-06-01 15:22:42 +000095
96#ifdef __cplusplus
97}
98#endif
99#endif /* !Py_GENOBJECT_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000100#endif /* Py_LIMITED_API */