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 */ |
| 12 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 13 | /* _PyGenObject_HEAD defines the initial segment of generator |
| 14 | and coroutine objects. */ |
| 15 | #define _PyGenObject_HEAD(prefix) \ |
| 16 | PyObject_HEAD \ |
| 17 | /* Note: gi_frame can be NULL if the generator is "finished" */ \ |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 18 | PyFrameObject *prefix##_frame; \ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 19 | /* The code object backing the generator */ \ |
| 20 | PyObject *prefix##_code; \ |
| 21 | /* List of weak reference. */ \ |
| 22 | PyObject *prefix##_weakreflist; \ |
| 23 | /* Name of the generator. */ \ |
| 24 | PyObject *prefix##_name; \ |
| 25 | /* Qualified name of the generator. */ \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 26 | PyObject *prefix##_qualname; \ |
| 27 | _PyErr_StackItem prefix##_exc_state; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 28 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 29 | typedef struct { |
Nick Coghlan | 76e1bb0 | 2012-01-14 16:08:08 +1000 | [diff] [blame] | 30 | /* The gi_ prefix is intended to remind of generator-iterator. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 31 | _PyGenObject_HEAD(gi) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 32 | } PyGenObject; |
| 33 | |
| 34 | PyAPI_DATA(PyTypeObject) PyGen_Type; |
| 35 | |
| 36 | #define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type) |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 37 | #define PyGen_CheckExact(op) Py_IS_TYPE(op, &PyGen_Type) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 38 | |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 39 | PyAPI_FUNC(PyObject *) PyGen_New(PyFrameObject *); |
| 40 | PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(PyFrameObject *, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 41 | PyObject *name, PyObject *qualname); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 42 | PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 43 | PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **); |
Yury Selivanov | 833c626 | 2016-10-28 18:48:50 -0400 | [diff] [blame] | 44 | PyAPI_FUNC(PyObject *) _PyGen_Send(PyGenObject *, 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 | |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 48 | typedef enum { |
| 49 | PYGEN_RETURN = 0, |
| 50 | PYGEN_ERROR = -1, |
| 51 | PYGEN_NEXT = 1, |
| 52 | } PySendResult; |
| 53 | |
| 54 | /* Sends the value into the generator or the coroutine. Returns: |
| 55 | - PYGEN_RETURN (0) if generator has returned. |
| 56 | 'result' parameter is filled with return value |
| 57 | - PYGEN_ERROR (-1) if exception was raised. |
| 58 | 'result' parameter is NULL |
| 59 | - PYGEN_NEXT (1) if generator has yielded. |
| 60 | 'result' parameter is filled with yielded value. */ |
| 61 | PyAPI_FUNC(PySendResult) PyGen_Send(PyGenObject *, PyObject *, PyObject **); |
| 62 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 63 | #ifndef Py_LIMITED_API |
| 64 | typedef struct { |
| 65 | _PyGenObject_HEAD(cr) |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 66 | PyObject *cr_origin; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 67 | } PyCoroObject; |
| 68 | |
| 69 | PyAPI_DATA(PyTypeObject) PyCoro_Type; |
| 70 | PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type; |
| 71 | |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 72 | #define PyCoro_CheckExact(op) Py_IS_TYPE(op, &PyCoro_Type) |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 73 | PyObject *_PyCoro_GetAwaitableIter(PyObject *o); |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 74 | PyAPI_FUNC(PyObject *) PyCoro_New(PyFrameObject *, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 75 | PyObject *name, PyObject *qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 76 | |
| 77 | /* Asynchronous Generators */ |
| 78 | |
| 79 | typedef struct { |
| 80 | _PyGenObject_HEAD(ag) |
| 81 | PyObject *ag_finalizer; |
| 82 | |
| 83 | /* Flag is set to 1 when hooks set up by sys.set_asyncgen_hooks |
| 84 | were called on the generator, to avoid calling them more |
| 85 | than once. */ |
| 86 | int ag_hooks_inited; |
| 87 | |
| 88 | /* Flag is set to 1 when aclose() is called for the first time, or |
| 89 | when a StopAsyncIteration exception is raised. */ |
| 90 | int ag_closed; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 91 | |
| 92 | int ag_running_async; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 93 | } PyAsyncGenObject; |
| 94 | |
| 95 | PyAPI_DATA(PyTypeObject) PyAsyncGen_Type; |
| 96 | PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type; |
| 97 | PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type; |
| 98 | PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type; |
| 99 | |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 100 | PyAPI_FUNC(PyObject *) PyAsyncGen_New(PyFrameObject *, |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 101 | PyObject *name, PyObject *qualname); |
| 102 | |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 103 | #define PyAsyncGen_CheckExact(op) Py_IS_TYPE(op, &PyAsyncGen_Type) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 104 | |
| 105 | PyObject *_PyAsyncGenValueWrapperNew(PyObject *); |
| 106 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | #undef _PyGenObject_HEAD |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 110 | |
| 111 | #ifdef __cplusplus |
| 112 | } |
| 113 | #endif |
| 114 | #endif /* !Py_GENOBJECT_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 115 | #endif /* Py_LIMITED_API */ |