Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 1 | |
| 2 | /* Generator object interface */ |
| 3 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4 | #ifndef Py_LIMITED_API |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 5 | #ifndef Py_GENOBJECT_H |
| 6 | #define Py_GENOBJECT_H |
| 7 | #ifdef __cplusplus |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
Victor Stinner | ed48866 | 2019-05-20 00:14:57 +0200 | [diff] [blame] | 11 | #include "pystate.h" /* _PyErr_StackItem */ |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 12 | #include "abstract.h" /* PySendResult */ |
Victor Stinner | ed48866 | 2019-05-20 00:14:57 +0200 | [diff] [blame] | 13 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 14 | /* _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 Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 19 | PyFrameObject *prefix##_frame; \ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 20 | /* 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 Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 27 | PyObject *prefix##_qualname; \ |
| 28 | _PyErr_StackItem prefix##_exc_state; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 29 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 30 | typedef struct { |
Nick Coghlan | 76e1bb0 | 2012-01-14 16:08:08 +1000 | [diff] [blame] | 31 | /* The gi_ prefix is intended to remind of generator-iterator. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 32 | _PyGenObject_HEAD(gi) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 33 | } PyGenObject; |
| 34 | |
| 35 | PyAPI_DATA(PyTypeObject) PyGen_Type; |
| 36 | |
| 37 | #define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type) |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 38 | #define PyGen_CheckExact(op) Py_IS_TYPE(op, &PyGen_Type) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 39 | |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 40 | PyAPI_FUNC(PyObject *) PyGen_New(PyFrameObject *); |
| 41 | PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(PyFrameObject *, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 42 | PyObject *name, PyObject *qualname); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 43 | PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 44 | PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **); |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 45 | PyObject *_PyGen_yf(PyGenObject *); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 46 | PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self); |
| 47 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 48 | #ifndef Py_LIMITED_API |
| 49 | typedef struct { |
| 50 | _PyGenObject_HEAD(cr) |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 51 | PyObject *cr_origin; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 52 | } PyCoroObject; |
| 53 | |
| 54 | PyAPI_DATA(PyTypeObject) PyCoro_Type; |
| 55 | PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type; |
| 56 | |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 57 | #define PyCoro_CheckExact(op) Py_IS_TYPE(op, &PyCoro_Type) |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 58 | PyObject *_PyCoro_GetAwaitableIter(PyObject *o); |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 59 | PyAPI_FUNC(PyObject *) PyCoro_New(PyFrameObject *, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 60 | PyObject *name, PyObject *qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 61 | |
| 62 | /* Asynchronous Generators */ |
| 63 | |
| 64 | typedef 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 Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 76 | |
| 77 | int ag_running_async; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 78 | } PyAsyncGenObject; |
| 79 | |
| 80 | PyAPI_DATA(PyTypeObject) PyAsyncGen_Type; |
| 81 | PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type; |
| 82 | PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type; |
| 83 | PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type; |
| 84 | |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 85 | PyAPI_FUNC(PyObject *) PyAsyncGen_New(PyFrameObject *, |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 86 | PyObject *name, PyObject *qualname); |
| 87 | |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 88 | #define PyAsyncGen_CheckExact(op) Py_IS_TYPE(op, &PyAsyncGen_Type) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 89 | |
| 90 | PyObject *_PyAsyncGenValueWrapperNew(PyObject *); |
| 91 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 92 | #endif |
| 93 | |
| 94 | #undef _PyGenObject_HEAD |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 95 | |
| 96 | #ifdef __cplusplus |
| 97 | } |
| 98 | #endif |
| 99 | #endif /* !Py_GENOBJECT_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 100 | #endif /* Py_LIMITED_API */ |