Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 1 | /* Generator object implementation */ |
| 2 | |
| 3 | #include "Python.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 4 | #include "pycore_ceval.h" // _PyEval_EvalFrame() |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 5 | #include "pycore_object.h" |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 6 | #include "pycore_pyerrors.h" // _PyErr_ClearExcState() |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 7 | #include "pycore_pystate.h" // _PyThreadState_GET() |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 8 | #include "frameobject.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 9 | #include "structmember.h" // PyMemberDef |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10 | #include "opcode.h" |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 11 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 12 | static PyObject *gen_close(PyGenObject *, PyObject *); |
| 13 | static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *); |
| 14 | static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *); |
| 15 | |
Andy Lester | 7386a70 | 2020-02-13 22:42:56 -0600 | [diff] [blame] | 16 | static const char *NON_INIT_CORO_MSG = "can't send non-None value to a " |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 17 | "just-started coroutine"; |
| 18 | |
Andy Lester | 7386a70 | 2020-02-13 22:42:56 -0600 | [diff] [blame] | 19 | static const char *ASYNC_GEN_IGNORED_EXIT_MSG = |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 20 | "async generator ignored GeneratorExit"; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 21 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 22 | static inline int |
| 23 | exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg) |
| 24 | { |
| 25 | Py_VISIT(exc_state->exc_type); |
| 26 | Py_VISIT(exc_state->exc_value); |
| 27 | Py_VISIT(exc_state->exc_traceback); |
| 28 | return 0; |
| 29 | } |
| 30 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 31 | static int |
| 32 | gen_traverse(PyGenObject *gen, visitproc visit, void *arg) |
| 33 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | Py_VISIT((PyObject *)gen->gi_frame); |
| 35 | Py_VISIT(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 36 | Py_VISIT(gen->gi_name); |
| 37 | Py_VISIT(gen->gi_qualname); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 38 | /* No need to visit cr_origin, because it's just tuples/str/int, so can't |
| 39 | participate in a reference cycle. */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 40 | return exc_state_traverse(&gen->gi_exc_state, visit, arg); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 43 | void |
| 44 | _PyGen_Finalize(PyObject *self) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 45 | { |
| 46 | PyGenObject *gen = (PyGenObject *)self; |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 47 | PyObject *res = NULL; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 48 | PyObject *error_type, *error_value, *error_traceback; |
| 49 | |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 50 | if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 51 | /* Generator isn't paused, so no need to close */ |
| 52 | return; |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 53 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 54 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 55 | if (PyAsyncGen_CheckExact(self)) { |
| 56 | PyAsyncGenObject *agen = (PyAsyncGenObject*)self; |
| 57 | PyObject *finalizer = agen->ag_finalizer; |
| 58 | if (finalizer && !agen->ag_closed) { |
| 59 | /* Save the current exception, if any. */ |
| 60 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 61 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 62 | res = PyObject_CallOneArg(finalizer, self); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 63 | |
| 64 | if (res == NULL) { |
| 65 | PyErr_WriteUnraisable(self); |
| 66 | } else { |
| 67 | Py_DECREF(res); |
| 68 | } |
| 69 | /* Restore the saved exception. */ |
| 70 | PyErr_Restore(error_type, error_value, error_traceback); |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 75 | /* Save the current exception, if any. */ |
| 76 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 77 | |
Benjamin Peterson | 2f40ed4 | 2016-09-05 10:14:54 -0700 | [diff] [blame] | 78 | /* If `gen` is a coroutine, and if it was never awaited on, |
| 79 | issue a RuntimeWarning. */ |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 80 | if (gen->gi_code != NULL && |
| 81 | ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE && |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 82 | gen->gi_frame->f_lasti == -1) |
| 83 | { |
| 84 | _PyErr_WarnUnawaitedCoroutine((PyObject *)gen); |
Benjamin Peterson | 2f40ed4 | 2016-09-05 10:14:54 -0700 | [diff] [blame] | 85 | } |
| 86 | else { |
| 87 | res = gen_close(gen, NULL); |
| 88 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 89 | |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 90 | if (res == NULL) { |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 91 | if (PyErr_Occurred()) { |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 92 | PyErr_WriteUnraisable(self); |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 93 | } |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 94 | } |
| 95 | else { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 96 | Py_DECREF(res); |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 97 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 98 | |
| 99 | /* Restore the saved exception. */ |
| 100 | PyErr_Restore(error_type, error_value, error_traceback); |
| 101 | } |
| 102 | |
| 103 | static void |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 104 | gen_dealloc(PyGenObject *gen) |
| 105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | PyObject *self = (PyObject *) gen; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | _PyObject_GC_UNTRACK(gen); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | if (gen->gi_weakreflist != NULL) |
| 111 | PyObject_ClearWeakRefs(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 113 | _PyObject_GC_TRACK(self); |
| 114 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 115 | if (PyObject_CallFinalizerFromDealloc(self)) |
| 116 | return; /* resurrected. :( */ |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 117 | |
| 118 | _PyObject_GC_UNTRACK(self); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 119 | if (PyAsyncGen_CheckExact(gen)) { |
| 120 | /* We have to handle this case for asynchronous generators |
| 121 | right here, because this code has to be between UNTRACK |
| 122 | and GC_Del. */ |
| 123 | Py_CLEAR(((PyAsyncGenObject*)gen)->ag_finalizer); |
| 124 | } |
Benjamin Peterson | bdddb11 | 2016-09-05 10:39:57 -0700 | [diff] [blame] | 125 | if (gen->gi_frame != NULL) { |
| 126 | gen->gi_frame->f_gen = NULL; |
| 127 | Py_CLEAR(gen->gi_frame); |
| 128 | } |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 129 | if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) { |
| 130 | Py_CLEAR(((PyCoroObject *)gen)->cr_origin); |
| 131 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | Py_CLEAR(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 133 | Py_CLEAR(gen->gi_name); |
| 134 | Py_CLEAR(gen->gi_qualname); |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 135 | _PyErr_ClearExcState(&gen->gi_exc_state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | PyObject_GC_Del(gen); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static PyObject * |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 140 | gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 141 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 142 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 144 | PyObject *result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 146 | if (gen->gi_running) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 147 | const char *msg = "generator already executing"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 148 | if (PyCoro_CheckExact(gen)) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 149 | msg = "coroutine already executing"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 150 | } |
| 151 | else if (PyAsyncGen_CheckExact(gen)) { |
| 152 | msg = "async generator already executing"; |
| 153 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 154 | PyErr_SetString(PyExc_ValueError, msg); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 155 | return NULL; |
| 156 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 157 | if (f == NULL || f->f_stacktop == NULL) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 158 | if (PyCoro_CheckExact(gen) && !closing) { |
| 159 | /* `gen` is an exhausted coroutine: raise an error, |
| 160 | except when called from gen_close(), which should |
| 161 | always be a silent method. */ |
| 162 | PyErr_SetString( |
| 163 | PyExc_RuntimeError, |
| 164 | "cannot reuse already awaited coroutine"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 165 | } |
| 166 | else if (arg && !exc) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 167 | /* `gen` is an exhausted generator: |
| 168 | only set exception if called from send(). */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 169 | if (PyAsyncGen_CheckExact(gen)) { |
| 170 | PyErr_SetNone(PyExc_StopAsyncIteration); |
| 171 | } |
| 172 | else { |
| 173 | PyErr_SetNone(PyExc_StopIteration); |
| 174 | } |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 175 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 178 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 179 | if (f->f_lasti == -1) { |
| 180 | if (arg && arg != Py_None) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 181 | const char *msg = "can't send non-None value to a " |
| 182 | "just-started generator"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 183 | if (PyCoro_CheckExact(gen)) { |
| 184 | msg = NON_INIT_CORO_MSG; |
| 185 | } |
| 186 | else if (PyAsyncGen_CheckExact(gen)) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 187 | msg = "can't send non-None value to a " |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 188 | "just-started async generator"; |
| 189 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 190 | PyErr_SetString(PyExc_TypeError, msg); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | } else { |
| 194 | /* Push arg onto the frame's value stack */ |
| 195 | result = arg ? arg : Py_None; |
| 196 | Py_INCREF(result); |
| 197 | *(f->f_stacktop++) = result; |
| 198 | } |
| 199 | |
| 200 | /* Generators always return to their most recent caller, not |
| 201 | * necessarily their creator. */ |
| 202 | Py_XINCREF(tstate->frame); |
| 203 | assert(f->f_back == NULL); |
| 204 | f->f_back = tstate->frame; |
| 205 | |
Miss Islington (bot) | f02c304 | 2020-05-18 19:14:13 -0700 | [diff] [blame] | 206 | if (exc) { |
| 207 | _PyErr_ChainStackItem(&gen->gi_exc_state); |
Chris Jerdonek | 75cd8e4 | 2020-05-13 16:18:27 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 210 | gen->gi_running = 1; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 211 | gen->gi_exc_state.previous_item = tstate->exc_info; |
| 212 | tstate->exc_info = &gen->gi_exc_state; |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 213 | result = _PyEval_EvalFrame(tstate, f, exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 214 | tstate->exc_info = gen->gi_exc_state.previous_item; |
| 215 | gen->gi_exc_state.previous_item = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 216 | gen->gi_running = 0; |
| 217 | |
| 218 | /* Don't keep the reference to f_back any longer than necessary. It |
| 219 | * may keep a chain of frames alive or it could create a reference |
| 220 | * cycle. */ |
| 221 | assert(f->f_back == tstate->frame); |
| 222 | Py_CLEAR(f->f_back); |
| 223 | |
| 224 | /* If the generator just returned (as opposed to yielding), signal |
| 225 | * that the generator is exhausted. */ |
| 226 | if (result && f->f_stacktop == NULL) { |
| 227 | if (result == Py_None) { |
| 228 | /* Delay exception instantiation if we can */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 229 | if (PyAsyncGen_CheckExact(gen)) { |
| 230 | PyErr_SetNone(PyExc_StopAsyncIteration); |
| 231 | } |
| 232 | else { |
| 233 | PyErr_SetNone(PyExc_StopIteration); |
| 234 | } |
| 235 | } |
| 236 | else { |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 237 | /* Async generators cannot return anything but None */ |
| 238 | assert(!PyAsyncGen_CheckExact(gen)); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 239 | _PyGen_SetStopIterationValue(result); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 240 | } |
| 241 | Py_CLEAR(result); |
| 242 | } |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 243 | else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) { |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 244 | const char *msg = "generator raised StopIteration"; |
| 245 | if (PyCoro_CheckExact(gen)) { |
| 246 | msg = "coroutine raised StopIteration"; |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 247 | } |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 248 | else if (PyAsyncGen_CheckExact(gen)) { |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 249 | msg = "async generator raised StopIteration"; |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 250 | } |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 251 | _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg); |
| 252 | |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 253 | } |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 254 | else if (!result && PyAsyncGen_CheckExact(gen) && |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 255 | PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) |
| 256 | { |
| 257 | /* code in `gen` raised a StopAsyncIteration error: |
| 258 | raise a RuntimeError. |
| 259 | */ |
| 260 | const char *msg = "async generator raised StopAsyncIteration"; |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 261 | _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 262 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 263 | |
| 264 | if (!result || f->f_stacktop == NULL) { |
| 265 | /* generator can't be rerun, so release the frame */ |
| 266 | /* first clean reference cycle through stored exception traceback */ |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 267 | _PyErr_ClearExcState(&gen->gi_exc_state); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 268 | gen->gi_frame->f_gen = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 269 | gen->gi_frame = NULL; |
| 270 | Py_DECREF(f); |
| 271 | } |
| 272 | |
| 273 | return result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 276 | PyDoc_STRVAR(send_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 277 | "send(arg) -> send 'arg' into generator,\n\ |
| 278 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 279 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 280 | PyObject * |
| 281 | _PyGen_Send(PyGenObject *gen, PyObject *arg) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 282 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 283 | return gen_send_ex(gen, arg, 0, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | PyDoc_STRVAR(close_doc, |
Benjamin Peterson | ab3da29 | 2012-05-03 18:44:09 -0400 | [diff] [blame] | 287 | "close() -> raise GeneratorExit inside generator."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 288 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 289 | /* |
| 290 | * This helper function is used by gen_close and gen_throw to |
| 291 | * close a subiterator being delegated to by yield-from. |
| 292 | */ |
| 293 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 294 | static int |
| 295 | gen_close_iter(PyObject *yf) |
| 296 | { |
| 297 | PyObject *retval = NULL; |
| 298 | _Py_IDENTIFIER(close); |
| 299 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 300 | if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 301 | retval = gen_close((PyGenObject *)yf, NULL); |
| 302 | if (retval == NULL) |
| 303 | return -1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 304 | } |
| 305 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 306 | PyObject *meth; |
| 307 | if (_PyObject_LookupAttrId(yf, &PyId_close, &meth) < 0) { |
| 308 | PyErr_WriteUnraisable(yf); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 309 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 310 | if (meth) { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 311 | retval = _PyObject_CallNoArg(meth); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 312 | Py_DECREF(meth); |
| 313 | if (retval == NULL) |
| 314 | return -1; |
| 315 | } |
| 316 | } |
| 317 | Py_XDECREF(retval); |
| 318 | return 0; |
| 319 | } |
| 320 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 321 | PyObject * |
| 322 | _PyGen_yf(PyGenObject *gen) |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 323 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 324 | PyObject *yf = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 325 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 326 | |
| 327 | if (f && f->f_stacktop) { |
| 328 | PyObject *bytecode = f->f_code->co_code; |
| 329 | unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode); |
| 330 | |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 331 | if (f->f_lasti < 0) { |
| 332 | /* Return immediately if the frame didn't start yet. YIELD_FROM |
| 333 | always come after LOAD_CONST: a code object should not start |
| 334 | with YIELD_FROM */ |
| 335 | assert(code[0] != YIELD_FROM); |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 339 | if (code[f->f_lasti + sizeof(_Py_CODEUNIT)] != YIELD_FROM) |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 340 | return NULL; |
| 341 | yf = f->f_stacktop[-1]; |
| 342 | Py_INCREF(yf); |
| 343 | } |
| 344 | |
| 345 | return yf; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 346 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 347 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 348 | static PyObject * |
| 349 | gen_close(PyGenObject *gen, PyObject *args) |
| 350 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 351 | PyObject *retval; |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 352 | PyObject *yf = _PyGen_yf(gen); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 353 | int err = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 354 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 355 | if (yf) { |
| 356 | gen->gi_running = 1; |
| 357 | err = gen_close_iter(yf); |
| 358 | gen->gi_running = 0; |
| 359 | Py_DECREF(yf); |
| 360 | } |
| 361 | if (err == 0) |
| 362 | PyErr_SetNone(PyExc_GeneratorExit); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 363 | retval = gen_send_ex(gen, Py_None, 1, 1); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 364 | if (retval) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 365 | const char *msg = "generator ignored GeneratorExit"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 366 | if (PyCoro_CheckExact(gen)) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 367 | msg = "coroutine ignored GeneratorExit"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 368 | } else if (PyAsyncGen_CheckExact(gen)) { |
| 369 | msg = ASYNC_GEN_IGNORED_EXIT_MSG; |
| 370 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 371 | Py_DECREF(retval); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 372 | PyErr_SetString(PyExc_RuntimeError, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | return NULL; |
| 374 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 375 | if (PyErr_ExceptionMatches(PyExc_StopIteration) |
| 376 | || PyErr_ExceptionMatches(PyExc_GeneratorExit)) { |
| 377 | PyErr_Clear(); /* ignore these errors */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 378 | Py_RETURN_NONE; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 379 | } |
| 380 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 383 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 384 | PyDoc_STRVAR(throw_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | "throw(typ[,val[,tb]]) -> raise exception in generator,\n\ |
| 386 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 387 | |
| 388 | static PyObject * |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 389 | _gen_throw(PyGenObject *gen, int close_on_genexit, |
| 390 | PyObject *typ, PyObject *val, PyObject *tb) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 391 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 392 | PyObject *yf = _PyGen_yf(gen); |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 393 | _Py_IDENTIFIER(throw); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 394 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 395 | if (yf) { |
| 396 | PyObject *ret; |
| 397 | int err; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 398 | if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) && |
| 399 | close_on_genexit |
| 400 | ) { |
| 401 | /* Asynchronous generators *should not* be closed right away. |
| 402 | We have to allow some awaits to work it through, hence the |
| 403 | `close_on_genexit` parameter here. |
| 404 | */ |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 405 | gen->gi_running = 1; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 406 | err = gen_close_iter(yf); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 407 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 408 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 409 | if (err < 0) |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 410 | return gen_send_ex(gen, Py_None, 1, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 411 | goto throw_here; |
| 412 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 413 | if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) { |
| 414 | /* `yf` is a generator or a coroutine. */ |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 415 | gen->gi_running = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 416 | /* Close the generator that we are currently iterating with |
| 417 | 'yield from' or awaiting on with 'await'. */ |
| 418 | ret = _gen_throw((PyGenObject *)yf, close_on_genexit, |
| 419 | typ, val, tb); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 420 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 421 | } else { |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 422 | /* `yf` is an iterator or a coroutine-like object. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 423 | PyObject *meth; |
| 424 | if (_PyObject_LookupAttrId(yf, &PyId_throw, &meth) < 0) { |
| 425 | Py_DECREF(yf); |
| 426 | return NULL; |
| 427 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 428 | if (meth == NULL) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 429 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 430 | goto throw_here; |
| 431 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 432 | gen->gi_running = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 433 | ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 434 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 435 | Py_DECREF(meth); |
| 436 | } |
| 437 | Py_DECREF(yf); |
| 438 | if (!ret) { |
| 439 | PyObject *val; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 440 | /* Pop subiterator from stack */ |
| 441 | ret = *(--gen->gi_frame->f_stacktop); |
| 442 | assert(ret == yf); |
| 443 | Py_DECREF(ret); |
| 444 | /* Termination repetition of YIELD_FROM */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 445 | assert(gen->gi_frame->f_lasti >= 0); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 446 | gen->gi_frame->f_lasti += sizeof(_Py_CODEUNIT); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 447 | if (_PyGen_FetchStopIterationValue(&val) == 0) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 448 | ret = gen_send_ex(gen, val, 0, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 449 | Py_DECREF(val); |
| 450 | } else { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 451 | ret = gen_send_ex(gen, Py_None, 1, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | throw_here: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | /* First, check the traceback argument, replacing None with |
| 459 | NULL. */ |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 460 | if (tb == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | tb = NULL; |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 462 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 464 | PyErr_SetString(PyExc_TypeError, |
| 465 | "throw() third argument must be a traceback object"); |
| 466 | return NULL; |
| 467 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | Py_INCREF(typ); |
| 470 | Py_XINCREF(val); |
| 471 | Py_XINCREF(tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 472 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 473 | if (PyExceptionClass_Check(typ)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | PyErr_NormalizeException(&typ, &val, &tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | else if (PyExceptionInstance_Check(typ)) { |
| 477 | /* Raising an instance. The value should be a dummy. */ |
| 478 | if (val && val != Py_None) { |
| 479 | PyErr_SetString(PyExc_TypeError, |
| 480 | "instance exception may not have a separate value"); |
| 481 | goto failed_throw; |
| 482 | } |
| 483 | else { |
| 484 | /* Normalize to raise <class>, <instance> */ |
| 485 | Py_XDECREF(val); |
| 486 | val = typ; |
| 487 | typ = PyExceptionInstance_Class(typ); |
| 488 | Py_INCREF(typ); |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 489 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 490 | if (tb == NULL) |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 491 | /* Returns NULL if there's no traceback */ |
| 492 | tb = PyException_GetTraceback(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | else { |
| 496 | /* Not something you can raise. throw() fails. */ |
| 497 | PyErr_Format(PyExc_TypeError, |
| 498 | "exceptions must be classes or instances " |
| 499 | "deriving from BaseException, not %s", |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 500 | Py_TYPE(typ)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | goto failed_throw; |
| 502 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | PyErr_Restore(typ, val, tb); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 505 | return gen_send_ex(gen, Py_None, 1, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 506 | |
| 507 | failed_throw: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | /* Didn't use our arguments, so restore their original refcounts */ |
| 509 | Py_DECREF(typ); |
| 510 | Py_XDECREF(val); |
| 511 | Py_XDECREF(tb); |
| 512 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | |
| 516 | static PyObject * |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 517 | gen_throw(PyGenObject *gen, PyObject *args) |
| 518 | { |
| 519 | PyObject *typ; |
| 520 | PyObject *tb = NULL; |
| 521 | PyObject *val = NULL; |
| 522 | |
| 523 | if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) { |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | return _gen_throw(gen, 1, typ, val, tb); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | static PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 532 | gen_iternext(PyGenObject *gen) |
| 533 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 534 | return gen_send_ex(gen, NULL, 0, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 537 | /* |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 538 | * Set StopIteration with specified value. Value can be arbitrary object |
| 539 | * or NULL. |
| 540 | * |
| 541 | * Returns 0 if StopIteration is set and -1 if any other exception is set. |
| 542 | */ |
| 543 | int |
| 544 | _PyGen_SetStopIterationValue(PyObject *value) |
| 545 | { |
| 546 | PyObject *e; |
| 547 | |
| 548 | if (value == NULL || |
Yury Selivanov | b7c9150 | 2017-03-12 15:53:07 -0400 | [diff] [blame] | 549 | (!PyTuple_Check(value) && !PyExceptionInstance_Check(value))) |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 550 | { |
| 551 | /* Delay exception instantiation if we can */ |
| 552 | PyErr_SetObject(PyExc_StopIteration, value); |
| 553 | return 0; |
| 554 | } |
| 555 | /* Construct an exception instance manually with |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 556 | * PyObject_CallOneArg and pass it to PyErr_SetObject. |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 557 | * |
| 558 | * We do this to handle a situation when "value" is a tuple, in which |
| 559 | * case PyErr_SetObject would set the value of StopIteration to |
| 560 | * the first element of the tuple. |
| 561 | * |
| 562 | * (See PyErr_SetObject/_PyErr_CreateException code for details.) |
| 563 | */ |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 564 | e = PyObject_CallOneArg(PyExc_StopIteration, value); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 565 | if (e == NULL) { |
| 566 | return -1; |
| 567 | } |
| 568 | PyErr_SetObject(PyExc_StopIteration, e); |
| 569 | Py_DECREF(e); |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /* |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 574 | * If StopIteration exception is set, fetches its 'value' |
| 575 | * attribute if any, otherwise sets pvalue to None. |
| 576 | * |
| 577 | * Returns 0 if no exception or StopIteration is set. |
| 578 | * If any other exception is set, returns -1 and leaves |
| 579 | * pvalue unchanged. |
| 580 | */ |
| 581 | |
| 582 | int |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 583 | _PyGen_FetchStopIterationValue(PyObject **pvalue) |
| 584 | { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 585 | PyObject *et, *ev, *tb; |
| 586 | PyObject *value = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 587 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 588 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 589 | PyErr_Fetch(&et, &ev, &tb); |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 590 | if (ev) { |
| 591 | /* exception will usually be normalised already */ |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 592 | if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 593 | value = ((PyStopIterationObject *)ev)->value; |
| 594 | Py_INCREF(value); |
| 595 | Py_DECREF(ev); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 596 | } else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) { |
| 597 | /* Avoid normalisation and take ev as value. |
| 598 | * |
| 599 | * Normalization is required if the value is a tuple, in |
| 600 | * that case the value of StopIteration would be set to |
| 601 | * the first element of the tuple. |
| 602 | * |
| 603 | * (See _PyErr_CreateException code for details.) |
| 604 | */ |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 605 | value = ev; |
| 606 | } else { |
| 607 | /* normalisation required */ |
| 608 | PyErr_NormalizeException(&et, &ev, &tb); |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 609 | if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 610 | PyErr_Restore(et, ev, tb); |
| 611 | return -1; |
| 612 | } |
| 613 | value = ((PyStopIterationObject *)ev)->value; |
| 614 | Py_INCREF(value); |
| 615 | Py_DECREF(ev); |
| 616 | } |
| 617 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 618 | Py_XDECREF(et); |
| 619 | Py_XDECREF(tb); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 620 | } else if (PyErr_Occurred()) { |
| 621 | return -1; |
| 622 | } |
| 623 | if (value == NULL) { |
| 624 | value = Py_None; |
Amaury Forgeot d'Arc | e557da8 | 2012-01-13 21:06:12 +0100 | [diff] [blame] | 625 | Py_INCREF(value); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 626 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 627 | *pvalue = value; |
| 628 | return 0; |
| 629 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 630 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 631 | static PyObject * |
| 632 | gen_repr(PyGenObject *gen) |
| 633 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 634 | return PyUnicode_FromFormat("<generator object %S at %p>", |
| 635 | gen->gi_qualname, gen); |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 638 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 639 | gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 640 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 641 | Py_INCREF(op->gi_name); |
| 642 | return op->gi_name; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 645 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 646 | gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 647 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 648 | /* Not legal to del gen.gi_name or to set it to anything |
| 649 | * other than a string object. */ |
| 650 | if (value == NULL || !PyUnicode_Check(value)) { |
| 651 | PyErr_SetString(PyExc_TypeError, |
| 652 | "__name__ must be set to a string object"); |
| 653 | return -1; |
| 654 | } |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 655 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 656 | Py_XSETREF(op->gi_name, value); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 661 | gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored)) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 662 | { |
| 663 | Py_INCREF(op->gi_qualname); |
| 664 | return op->gi_qualname; |
| 665 | } |
| 666 | |
| 667 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 668 | gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 669 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 670 | /* Not legal to del gen.__qualname__ or to set it to anything |
| 671 | * other than a string object. */ |
| 672 | if (value == NULL || !PyUnicode_Check(value)) { |
| 673 | PyErr_SetString(PyExc_TypeError, |
| 674 | "__qualname__ must be set to a string object"); |
| 675 | return -1; |
| 676 | } |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 677 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 678 | Py_XSETREF(op->gi_qualname, value); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 679 | return 0; |
| 680 | } |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 681 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 682 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 683 | gen_getyieldfrom(PyGenObject *gen, void *Py_UNUSED(ignored)) |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 684 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 685 | PyObject *yf = _PyGen_yf(gen); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 686 | if (yf == NULL) |
| 687 | Py_RETURN_NONE; |
| 688 | return yf; |
| 689 | } |
| 690 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 691 | static PyGetSetDef gen_getsetlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 692 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 693 | PyDoc_STR("name of the generator")}, |
| 694 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 695 | PyDoc_STR("qualified name of the generator")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 696 | {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL, |
| 697 | PyDoc_STR("object being iterated by yield from, or None")}, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 698 | {NULL} /* Sentinel */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 699 | }; |
| 700 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 701 | static PyMemberDef gen_memberlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 702 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
| 703 | {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY}, |
| 704 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | {NULL} /* Sentinel */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 706 | }; |
| 707 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 708 | static PyMethodDef gen_methods[] = { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 709 | {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 711 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 712 | {NULL, NULL} /* Sentinel */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 713 | }; |
| 714 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 715 | PyTypeObject PyGen_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 717 | "generator", /* tp_name */ |
| 718 | sizeof(PyGenObject), /* tp_basicsize */ |
| 719 | 0, /* tp_itemsize */ |
| 720 | /* methods */ |
| 721 | (destructor)gen_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 722 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | 0, /* tp_getattr */ |
| 724 | 0, /* tp_setattr */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 725 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | (reprfunc)gen_repr, /* tp_repr */ |
| 727 | 0, /* tp_as_number */ |
| 728 | 0, /* tp_as_sequence */ |
| 729 | 0, /* tp_as_mapping */ |
| 730 | 0, /* tp_hash */ |
| 731 | 0, /* tp_call */ |
| 732 | 0, /* tp_str */ |
| 733 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 734 | 0, /* tp_setattro */ |
| 735 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 736 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | 0, /* tp_doc */ |
| 738 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 739 | 0, /* tp_clear */ |
| 740 | 0, /* tp_richcompare */ |
| 741 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 742 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 744 | gen_methods, /* tp_methods */ |
| 745 | gen_memberlist, /* tp_members */ |
| 746 | gen_getsetlist, /* tp_getset */ |
| 747 | 0, /* tp_base */ |
| 748 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | 0, /* tp_descr_get */ |
| 751 | 0, /* tp_descr_set */ |
| 752 | 0, /* tp_dictoffset */ |
| 753 | 0, /* tp_init */ |
| 754 | 0, /* tp_alloc */ |
| 755 | 0, /* tp_new */ |
| 756 | 0, /* tp_free */ |
| 757 | 0, /* tp_is_gc */ |
| 758 | 0, /* tp_bases */ |
| 759 | 0, /* tp_mro */ |
| 760 | 0, /* tp_cache */ |
| 761 | 0, /* tp_subclasses */ |
| 762 | 0, /* tp_weaklist */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 763 | 0, /* tp_del */ |
| 764 | 0, /* tp_version_tag */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 765 | _PyGen_Finalize, /* tp_finalize */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 766 | }; |
| 767 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 768 | static PyObject * |
| 769 | gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f, |
| 770 | PyObject *name, PyObject *qualname) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 771 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 772 | PyGenObject *gen = PyObject_GC_New(PyGenObject, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | if (gen == NULL) { |
| 774 | Py_DECREF(f); |
| 775 | return NULL; |
| 776 | } |
| 777 | gen->gi_frame = f; |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 778 | f->f_gen = (PyObject *) gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | Py_INCREF(f->f_code); |
| 780 | gen->gi_code = (PyObject *)(f->f_code); |
| 781 | gen->gi_running = 0; |
| 782 | gen->gi_weakreflist = NULL; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 783 | gen->gi_exc_state.exc_type = NULL; |
| 784 | gen->gi_exc_state.exc_value = NULL; |
| 785 | gen->gi_exc_state.exc_traceback = NULL; |
| 786 | gen->gi_exc_state.previous_item = NULL; |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 787 | if (name != NULL) |
| 788 | gen->gi_name = name; |
| 789 | else |
| 790 | gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 791 | Py_INCREF(gen->gi_name); |
| 792 | if (qualname != NULL) |
| 793 | gen->gi_qualname = qualname; |
| 794 | else |
| 795 | gen->gi_qualname = gen->gi_name; |
| 796 | Py_INCREF(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | _PyObject_GC_TRACK(gen); |
| 798 | return (PyObject *)gen; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 799 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 800 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 801 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 802 | PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 803 | { |
| 804 | return gen_new_with_qualname(&PyGen_Type, f, name, qualname); |
| 805 | } |
| 806 | |
| 807 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 808 | PyGen_New(PyFrameObject *f) |
| 809 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 810 | return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 813 | /* Coroutine Object */ |
| 814 | |
| 815 | typedef struct { |
| 816 | PyObject_HEAD |
| 817 | PyCoroObject *cw_coroutine; |
| 818 | } PyCoroWrapper; |
| 819 | |
| 820 | static int |
| 821 | gen_is_coroutine(PyObject *o) |
| 822 | { |
| 823 | if (PyGen_CheckExact(o)) { |
| 824 | PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code; |
| 825 | if (code->co_flags & CO_ITERABLE_COROUTINE) { |
| 826 | return 1; |
| 827 | } |
| 828 | } |
| 829 | return 0; |
| 830 | } |
| 831 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 832 | /* |
| 833 | * This helper function returns an awaitable for `o`: |
| 834 | * - `o` if `o` is a coroutine-object; |
| 835 | * - `type(o)->tp_as_async->am_await(o)` |
| 836 | * |
| 837 | * Raises a TypeError if it's not possible to return |
| 838 | * an awaitable and returns NULL. |
| 839 | */ |
| 840 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 841 | _PyCoro_GetAwaitableIter(PyObject *o) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 842 | { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 843 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 844 | PyTypeObject *ot; |
| 845 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 846 | if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) { |
| 847 | /* 'o' is a coroutine. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 848 | Py_INCREF(o); |
| 849 | return o; |
| 850 | } |
| 851 | |
| 852 | ot = Py_TYPE(o); |
| 853 | if (ot->tp_as_async != NULL) { |
| 854 | getter = ot->tp_as_async->am_await; |
| 855 | } |
| 856 | if (getter != NULL) { |
| 857 | PyObject *res = (*getter)(o); |
| 858 | if (res != NULL) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 859 | if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) { |
| 860 | /* __await__ must return an *iterator*, not |
| 861 | a coroutine or another awaitable (see PEP 492) */ |
| 862 | PyErr_SetString(PyExc_TypeError, |
| 863 | "__await__() returned a coroutine"); |
| 864 | Py_CLEAR(res); |
| 865 | } else if (!PyIter_Check(res)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 866 | PyErr_Format(PyExc_TypeError, |
| 867 | "__await__() returned non-iterator " |
| 868 | "of type '%.100s'", |
| 869 | Py_TYPE(res)->tp_name); |
| 870 | Py_CLEAR(res); |
| 871 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 872 | } |
| 873 | return res; |
| 874 | } |
| 875 | |
| 876 | PyErr_Format(PyExc_TypeError, |
| 877 | "object %.100s can't be used in 'await' expression", |
| 878 | ot->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 881 | |
| 882 | static PyObject * |
| 883 | coro_repr(PyCoroObject *coro) |
| 884 | { |
| 885 | return PyUnicode_FromFormat("<coroutine object %S at %p>", |
| 886 | coro->cr_qualname, coro); |
| 887 | } |
| 888 | |
| 889 | static PyObject * |
| 890 | coro_await(PyCoroObject *coro) |
| 891 | { |
| 892 | PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type); |
| 893 | if (cw == NULL) { |
| 894 | return NULL; |
| 895 | } |
| 896 | Py_INCREF(coro); |
| 897 | cw->cw_coroutine = coro; |
| 898 | _PyObject_GC_TRACK(cw); |
| 899 | return (PyObject *)cw; |
| 900 | } |
| 901 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 902 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 903 | coro_get_cr_await(PyCoroObject *coro, void *Py_UNUSED(ignored)) |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 904 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 905 | PyObject *yf = _PyGen_yf((PyGenObject *) coro); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 906 | if (yf == NULL) |
| 907 | Py_RETURN_NONE; |
| 908 | return yf; |
| 909 | } |
| 910 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 911 | static PyGetSetDef coro_getsetlist[] = { |
| 912 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 913 | PyDoc_STR("name of the coroutine")}, |
| 914 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 915 | PyDoc_STR("qualified name of the coroutine")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 916 | {"cr_await", (getter)coro_get_cr_await, NULL, |
| 917 | PyDoc_STR("object being awaited on, or None")}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 918 | {NULL} /* Sentinel */ |
| 919 | }; |
| 920 | |
| 921 | static PyMemberDef coro_memberlist[] = { |
| 922 | {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY}, |
| 923 | {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY}, |
| 924 | {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY}, |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 925 | {"cr_origin", T_OBJECT, offsetof(PyCoroObject, cr_origin), READONLY}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 926 | {NULL} /* Sentinel */ |
| 927 | }; |
| 928 | |
| 929 | PyDoc_STRVAR(coro_send_doc, |
| 930 | "send(arg) -> send 'arg' into coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 931 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 932 | |
| 933 | PyDoc_STRVAR(coro_throw_doc, |
| 934 | "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 935 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 936 | |
| 937 | PyDoc_STRVAR(coro_close_doc, |
| 938 | "close() -> raise GeneratorExit inside coroutine."); |
| 939 | |
| 940 | static PyMethodDef coro_methods[] = { |
| 941 | {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc}, |
| 942 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc}, |
| 943 | {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc}, |
| 944 | {NULL, NULL} /* Sentinel */ |
| 945 | }; |
| 946 | |
| 947 | static PyAsyncMethods coro_as_async = { |
| 948 | (unaryfunc)coro_await, /* am_await */ |
| 949 | 0, /* am_aiter */ |
| 950 | 0 /* am_anext */ |
| 951 | }; |
| 952 | |
| 953 | PyTypeObject PyCoro_Type = { |
| 954 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 955 | "coroutine", /* tp_name */ |
| 956 | sizeof(PyCoroObject), /* tp_basicsize */ |
| 957 | 0, /* tp_itemsize */ |
| 958 | /* methods */ |
| 959 | (destructor)gen_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 960 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 961 | 0, /* tp_getattr */ |
| 962 | 0, /* tp_setattr */ |
| 963 | &coro_as_async, /* tp_as_async */ |
| 964 | (reprfunc)coro_repr, /* tp_repr */ |
| 965 | 0, /* tp_as_number */ |
| 966 | 0, /* tp_as_sequence */ |
| 967 | 0, /* tp_as_mapping */ |
| 968 | 0, /* tp_hash */ |
| 969 | 0, /* tp_call */ |
| 970 | 0, /* tp_str */ |
| 971 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 972 | 0, /* tp_setattro */ |
| 973 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 974 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 975 | 0, /* tp_doc */ |
| 976 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 977 | 0, /* tp_clear */ |
| 978 | 0, /* tp_richcompare */ |
| 979 | offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */ |
| 980 | 0, /* tp_iter */ |
| 981 | 0, /* tp_iternext */ |
| 982 | coro_methods, /* tp_methods */ |
| 983 | coro_memberlist, /* tp_members */ |
| 984 | coro_getsetlist, /* tp_getset */ |
| 985 | 0, /* tp_base */ |
| 986 | 0, /* tp_dict */ |
| 987 | 0, /* tp_descr_get */ |
| 988 | 0, /* tp_descr_set */ |
| 989 | 0, /* tp_dictoffset */ |
| 990 | 0, /* tp_init */ |
| 991 | 0, /* tp_alloc */ |
| 992 | 0, /* tp_new */ |
| 993 | 0, /* tp_free */ |
| 994 | 0, /* tp_is_gc */ |
| 995 | 0, /* tp_bases */ |
| 996 | 0, /* tp_mro */ |
| 997 | 0, /* tp_cache */ |
| 998 | 0, /* tp_subclasses */ |
| 999 | 0, /* tp_weaklist */ |
| 1000 | 0, /* tp_del */ |
| 1001 | 0, /* tp_version_tag */ |
| 1002 | _PyGen_Finalize, /* tp_finalize */ |
| 1003 | }; |
| 1004 | |
| 1005 | static void |
| 1006 | coro_wrapper_dealloc(PyCoroWrapper *cw) |
| 1007 | { |
| 1008 | _PyObject_GC_UNTRACK((PyObject *)cw); |
| 1009 | Py_CLEAR(cw->cw_coroutine); |
| 1010 | PyObject_GC_Del(cw); |
| 1011 | } |
| 1012 | |
| 1013 | static PyObject * |
| 1014 | coro_wrapper_iternext(PyCoroWrapper *cw) |
| 1015 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 1016 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static PyObject * |
| 1020 | coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg) |
| 1021 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 1022 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | static PyObject * |
| 1026 | coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args) |
| 1027 | { |
| 1028 | return gen_throw((PyGenObject *)cw->cw_coroutine, args); |
| 1029 | } |
| 1030 | |
| 1031 | static PyObject * |
| 1032 | coro_wrapper_close(PyCoroWrapper *cw, PyObject *args) |
| 1033 | { |
| 1034 | return gen_close((PyGenObject *)cw->cw_coroutine, args); |
| 1035 | } |
| 1036 | |
| 1037 | static int |
| 1038 | coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg) |
| 1039 | { |
| 1040 | Py_VISIT((PyObject *)cw->cw_coroutine); |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | static PyMethodDef coro_wrapper_methods[] = { |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 1045 | {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc}, |
| 1046 | {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc}, |
| 1047 | {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1048 | {NULL, NULL} /* Sentinel */ |
| 1049 | }; |
| 1050 | |
| 1051 | PyTypeObject _PyCoroWrapper_Type = { |
| 1052 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1053 | "coroutine_wrapper", |
| 1054 | sizeof(PyCoroWrapper), /* tp_basicsize */ |
| 1055 | 0, /* tp_itemsize */ |
| 1056 | (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1057 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1058 | 0, /* tp_getattr */ |
| 1059 | 0, /* tp_setattr */ |
| 1060 | 0, /* tp_as_async */ |
| 1061 | 0, /* tp_repr */ |
| 1062 | 0, /* tp_as_number */ |
| 1063 | 0, /* tp_as_sequence */ |
| 1064 | 0, /* tp_as_mapping */ |
| 1065 | 0, /* tp_hash */ |
| 1066 | 0, /* tp_call */ |
| 1067 | 0, /* tp_str */ |
| 1068 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1069 | 0, /* tp_setattro */ |
| 1070 | 0, /* tp_as_buffer */ |
| 1071 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1072 | "A wrapper object implementing __await__ for coroutines.", |
| 1073 | (traverseproc)coro_wrapper_traverse, /* tp_traverse */ |
| 1074 | 0, /* tp_clear */ |
| 1075 | 0, /* tp_richcompare */ |
| 1076 | 0, /* tp_weaklistoffset */ |
| 1077 | PyObject_SelfIter, /* tp_iter */ |
| 1078 | (iternextfunc)coro_wrapper_iternext, /* tp_iternext */ |
| 1079 | coro_wrapper_methods, /* tp_methods */ |
| 1080 | 0, /* tp_members */ |
| 1081 | 0, /* tp_getset */ |
| 1082 | 0, /* tp_base */ |
| 1083 | 0, /* tp_dict */ |
| 1084 | 0, /* tp_descr_get */ |
| 1085 | 0, /* tp_descr_set */ |
| 1086 | 0, /* tp_dictoffset */ |
| 1087 | 0, /* tp_init */ |
| 1088 | 0, /* tp_alloc */ |
| 1089 | 0, /* tp_new */ |
Yury Selivanov | 33499b7 | 2016-11-08 19:19:28 -0500 | [diff] [blame] | 1090 | 0, /* tp_free */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1091 | }; |
| 1092 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1093 | static PyObject * |
| 1094 | compute_cr_origin(int origin_depth) |
| 1095 | { |
| 1096 | PyFrameObject *frame = PyEval_GetFrame(); |
| 1097 | /* First count how many frames we have */ |
| 1098 | int frame_count = 0; |
| 1099 | for (; frame && frame_count < origin_depth; ++frame_count) { |
| 1100 | frame = frame->f_back; |
| 1101 | } |
| 1102 | |
| 1103 | /* Now collect them */ |
| 1104 | PyObject *cr_origin = PyTuple_New(frame_count); |
Alexey Izbyshev | 8fdd331 | 2018-08-25 10:15:23 +0300 | [diff] [blame] | 1105 | if (cr_origin == NULL) { |
| 1106 | return NULL; |
| 1107 | } |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1108 | frame = PyEval_GetFrame(); |
| 1109 | for (int i = 0; i < frame_count; ++i) { |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 1110 | PyCodeObject *code = frame->f_code; |
| 1111 | PyObject *frameinfo = Py_BuildValue("OiO", |
| 1112 | code->co_filename, |
| 1113 | PyFrame_GetLineNumber(frame), |
| 1114 | code->co_name); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1115 | if (!frameinfo) { |
| 1116 | Py_DECREF(cr_origin); |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | PyTuple_SET_ITEM(cr_origin, i, frameinfo); |
| 1120 | frame = frame->f_back; |
| 1121 | } |
| 1122 | |
| 1123 | return cr_origin; |
| 1124 | } |
| 1125 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1126 | PyObject * |
| 1127 | PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 1128 | { |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1129 | PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname); |
| 1130 | if (!coro) { |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1134 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1135 | int origin_depth = tstate->coroutine_origin_tracking_depth; |
| 1136 | |
| 1137 | if (origin_depth == 0) { |
| 1138 | ((PyCoroObject *)coro)->cr_origin = NULL; |
| 1139 | } else { |
| 1140 | PyObject *cr_origin = compute_cr_origin(origin_depth); |
Zackery Spytz | 062a57b | 2018-11-18 09:45:57 -0700 | [diff] [blame] | 1141 | ((PyCoroObject *)coro)->cr_origin = cr_origin; |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1142 | if (!cr_origin) { |
| 1143 | Py_DECREF(coro); |
| 1144 | return NULL; |
| 1145 | } |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | return coro; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1149 | } |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1150 | |
| 1151 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1152 | /* ========= Asynchronous Generators ========= */ |
| 1153 | |
| 1154 | |
| 1155 | typedef enum { |
| 1156 | AWAITABLE_STATE_INIT, /* new awaitable, has not yet been iterated */ |
| 1157 | AWAITABLE_STATE_ITER, /* being iterated */ |
| 1158 | AWAITABLE_STATE_CLOSED, /* closed */ |
| 1159 | } AwaitableState; |
| 1160 | |
| 1161 | |
| 1162 | typedef struct { |
| 1163 | PyObject_HEAD |
| 1164 | PyAsyncGenObject *ags_gen; |
| 1165 | |
| 1166 | /* Can be NULL, when in the __anext__() mode |
| 1167 | (equivalent of "asend(None)") */ |
| 1168 | PyObject *ags_sendval; |
| 1169 | |
| 1170 | AwaitableState ags_state; |
| 1171 | } PyAsyncGenASend; |
| 1172 | |
| 1173 | |
| 1174 | typedef struct { |
| 1175 | PyObject_HEAD |
| 1176 | PyAsyncGenObject *agt_gen; |
| 1177 | |
| 1178 | /* Can be NULL, when in the "aclose()" mode |
| 1179 | (equivalent of "athrow(GeneratorExit)") */ |
| 1180 | PyObject *agt_args; |
| 1181 | |
| 1182 | AwaitableState agt_state; |
| 1183 | } PyAsyncGenAThrow; |
| 1184 | |
| 1185 | |
| 1186 | typedef struct { |
| 1187 | PyObject_HEAD |
| 1188 | PyObject *agw_val; |
| 1189 | } _PyAsyncGenWrappedValue; |
| 1190 | |
| 1191 | |
| 1192 | #ifndef _PyAsyncGen_MAXFREELIST |
| 1193 | #define _PyAsyncGen_MAXFREELIST 80 |
| 1194 | #endif |
| 1195 | |
| 1196 | /* Freelists boost performance 6-10%; they also reduce memory |
| 1197 | fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend |
| 1198 | are short-living objects that are instantiated for every |
| 1199 | __anext__ call. |
| 1200 | */ |
| 1201 | |
| 1202 | static _PyAsyncGenWrappedValue *ag_value_freelist[_PyAsyncGen_MAXFREELIST]; |
| 1203 | static int ag_value_freelist_free = 0; |
| 1204 | |
| 1205 | static PyAsyncGenASend *ag_asend_freelist[_PyAsyncGen_MAXFREELIST]; |
| 1206 | static int ag_asend_freelist_free = 0; |
| 1207 | |
| 1208 | #define _PyAsyncGenWrappedValue_CheckExact(o) \ |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 1209 | Py_IS_TYPE(o, &_PyAsyncGenWrappedValue_Type) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1210 | |
| 1211 | #define PyAsyncGenASend_CheckExact(o) \ |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 1212 | Py_IS_TYPE(o, &_PyAsyncGenASend_Type) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1213 | |
| 1214 | |
| 1215 | static int |
| 1216 | async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg) |
| 1217 | { |
| 1218 | Py_VISIT(gen->ag_finalizer); |
| 1219 | return gen_traverse((PyGenObject*)gen, visit, arg); |
| 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | static PyObject * |
| 1224 | async_gen_repr(PyAsyncGenObject *o) |
| 1225 | { |
| 1226 | return PyUnicode_FromFormat("<async_generator object %S at %p>", |
| 1227 | o->ag_qualname, o); |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | static int |
| 1232 | async_gen_init_hooks(PyAsyncGenObject *o) |
| 1233 | { |
| 1234 | PyThreadState *tstate; |
| 1235 | PyObject *finalizer; |
| 1236 | PyObject *firstiter; |
| 1237 | |
| 1238 | if (o->ag_hooks_inited) { |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | o->ag_hooks_inited = 1; |
| 1243 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1244 | tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1245 | |
| 1246 | finalizer = tstate->async_gen_finalizer; |
| 1247 | if (finalizer) { |
| 1248 | Py_INCREF(finalizer); |
| 1249 | o->ag_finalizer = finalizer; |
| 1250 | } |
| 1251 | |
| 1252 | firstiter = tstate->async_gen_firstiter; |
| 1253 | if (firstiter) { |
| 1254 | PyObject *res; |
| 1255 | |
| 1256 | Py_INCREF(firstiter); |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1257 | res = PyObject_CallOneArg(firstiter, (PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1258 | Py_DECREF(firstiter); |
| 1259 | if (res == NULL) { |
| 1260 | return 1; |
| 1261 | } |
| 1262 | Py_DECREF(res); |
| 1263 | } |
| 1264 | |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | |
| 1269 | static PyObject * |
| 1270 | async_gen_anext(PyAsyncGenObject *o) |
| 1271 | { |
| 1272 | if (async_gen_init_hooks(o)) { |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | return async_gen_asend_new(o, NULL); |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | static PyObject * |
| 1280 | async_gen_asend(PyAsyncGenObject *o, PyObject *arg) |
| 1281 | { |
| 1282 | if (async_gen_init_hooks(o)) { |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | return async_gen_asend_new(o, arg); |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | static PyObject * |
| 1290 | async_gen_aclose(PyAsyncGenObject *o, PyObject *arg) |
| 1291 | { |
| 1292 | if (async_gen_init_hooks(o)) { |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | return async_gen_athrow_new(o, NULL); |
| 1296 | } |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | async_gen_athrow(PyAsyncGenObject *o, PyObject *args) |
| 1300 | { |
| 1301 | if (async_gen_init_hooks(o)) { |
| 1302 | return NULL; |
| 1303 | } |
| 1304 | return async_gen_athrow_new(o, args); |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | static PyGetSetDef async_gen_getsetlist[] = { |
| 1309 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 1310 | PyDoc_STR("name of the async generator")}, |
| 1311 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 1312 | PyDoc_STR("qualified name of the async generator")}, |
| 1313 | {"ag_await", (getter)coro_get_cr_await, NULL, |
| 1314 | PyDoc_STR("object being awaited on, or None")}, |
| 1315 | {NULL} /* Sentinel */ |
| 1316 | }; |
| 1317 | |
| 1318 | static PyMemberDef async_gen_memberlist[] = { |
| 1319 | {"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY}, |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1320 | {"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running_async), |
| 1321 | READONLY}, |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1322 | {"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY}, |
| 1323 | {NULL} /* Sentinel */ |
| 1324 | }; |
| 1325 | |
| 1326 | PyDoc_STRVAR(async_aclose_doc, |
| 1327 | "aclose() -> raise GeneratorExit inside generator."); |
| 1328 | |
| 1329 | PyDoc_STRVAR(async_asend_doc, |
| 1330 | "asend(v) -> send 'v' in generator."); |
| 1331 | |
| 1332 | PyDoc_STRVAR(async_athrow_doc, |
| 1333 | "athrow(typ[,val[,tb]]) -> raise exception in generator."); |
| 1334 | |
| 1335 | static PyMethodDef async_gen_methods[] = { |
| 1336 | {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc}, |
| 1337 | {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc}, |
| 1338 | {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc}, |
Ethan Smith | 7c4185d | 2020-04-09 21:25:53 -0700 | [diff] [blame] | 1339 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 1340 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1341 | {NULL, NULL} /* Sentinel */ |
| 1342 | }; |
| 1343 | |
| 1344 | |
| 1345 | static PyAsyncMethods async_gen_as_async = { |
| 1346 | 0, /* am_await */ |
| 1347 | PyObject_SelfIter, /* am_aiter */ |
| 1348 | (unaryfunc)async_gen_anext /* am_anext */ |
| 1349 | }; |
| 1350 | |
| 1351 | |
| 1352 | PyTypeObject PyAsyncGen_Type = { |
| 1353 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1354 | "async_generator", /* tp_name */ |
| 1355 | sizeof(PyAsyncGenObject), /* tp_basicsize */ |
| 1356 | 0, /* tp_itemsize */ |
| 1357 | /* methods */ |
| 1358 | (destructor)gen_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1359 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1360 | 0, /* tp_getattr */ |
| 1361 | 0, /* tp_setattr */ |
| 1362 | &async_gen_as_async, /* tp_as_async */ |
| 1363 | (reprfunc)async_gen_repr, /* tp_repr */ |
| 1364 | 0, /* tp_as_number */ |
| 1365 | 0, /* tp_as_sequence */ |
| 1366 | 0, /* tp_as_mapping */ |
| 1367 | 0, /* tp_hash */ |
| 1368 | 0, /* tp_call */ |
| 1369 | 0, /* tp_str */ |
| 1370 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1371 | 0, /* tp_setattro */ |
| 1372 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 1373 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1374 | 0, /* tp_doc */ |
| 1375 | (traverseproc)async_gen_traverse, /* tp_traverse */ |
| 1376 | 0, /* tp_clear */ |
| 1377 | 0, /* tp_richcompare */ |
| 1378 | offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */ |
| 1379 | 0, /* tp_iter */ |
| 1380 | 0, /* tp_iternext */ |
| 1381 | async_gen_methods, /* tp_methods */ |
| 1382 | async_gen_memberlist, /* tp_members */ |
| 1383 | async_gen_getsetlist, /* tp_getset */ |
| 1384 | 0, /* tp_base */ |
| 1385 | 0, /* tp_dict */ |
| 1386 | 0, /* tp_descr_get */ |
| 1387 | 0, /* tp_descr_set */ |
| 1388 | 0, /* tp_dictoffset */ |
| 1389 | 0, /* tp_init */ |
| 1390 | 0, /* tp_alloc */ |
| 1391 | 0, /* tp_new */ |
| 1392 | 0, /* tp_free */ |
| 1393 | 0, /* tp_is_gc */ |
| 1394 | 0, /* tp_bases */ |
| 1395 | 0, /* tp_mro */ |
| 1396 | 0, /* tp_cache */ |
| 1397 | 0, /* tp_subclasses */ |
| 1398 | 0, /* tp_weaklist */ |
| 1399 | 0, /* tp_del */ |
| 1400 | 0, /* tp_version_tag */ |
| 1401 | _PyGen_Finalize, /* tp_finalize */ |
| 1402 | }; |
| 1403 | |
| 1404 | |
| 1405 | PyObject * |
| 1406 | PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 1407 | { |
| 1408 | PyAsyncGenObject *o; |
| 1409 | o = (PyAsyncGenObject *)gen_new_with_qualname( |
| 1410 | &PyAsyncGen_Type, f, name, qualname); |
| 1411 | if (o == NULL) { |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | o->ag_finalizer = NULL; |
| 1415 | o->ag_closed = 0; |
| 1416 | o->ag_hooks_inited = 0; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1417 | o->ag_running_async = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1418 | return (PyObject*)o; |
| 1419 | } |
| 1420 | |
| 1421 | |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 1422 | void |
| 1423 | _PyAsyncGen_ClearFreeLists(void) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1424 | { |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1425 | while (ag_value_freelist_free) { |
| 1426 | _PyAsyncGenWrappedValue *o; |
| 1427 | o = ag_value_freelist[--ag_value_freelist_free]; |
| 1428 | assert(_PyAsyncGenWrappedValue_CheckExact(o)); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1429 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | while (ag_asend_freelist_free) { |
| 1433 | PyAsyncGenASend *o; |
| 1434 | o = ag_asend_freelist[--ag_asend_freelist_free]; |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 1435 | assert(Py_IS_TYPE(o, &_PyAsyncGenASend_Type)); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1436 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1437 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | void |
Victor Stinner | bed4817 | 2019-08-27 00:12:32 +0200 | [diff] [blame] | 1441 | _PyAsyncGen_Fini(void) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1442 | { |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 1443 | _PyAsyncGen_ClearFreeLists(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | static PyObject * |
| 1448 | async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result) |
| 1449 | { |
| 1450 | if (result == NULL) { |
| 1451 | if (!PyErr_Occurred()) { |
| 1452 | PyErr_SetNone(PyExc_StopAsyncIteration); |
| 1453 | } |
| 1454 | |
| 1455 | if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) |
| 1456 | || PyErr_ExceptionMatches(PyExc_GeneratorExit) |
| 1457 | ) { |
| 1458 | gen->ag_closed = 1; |
| 1459 | } |
| 1460 | |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1461 | gen->ag_running_async = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1462 | return NULL; |
| 1463 | } |
| 1464 | |
| 1465 | if (_PyAsyncGenWrappedValue_CheckExact(result)) { |
| 1466 | /* async yield */ |
Serhiy Storchaka | 60e49aa | 2016-11-06 18:47:03 +0200 | [diff] [blame] | 1467 | _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1468 | Py_DECREF(result); |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1469 | gen->ag_running_async = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1470 | return NULL; |
| 1471 | } |
| 1472 | |
| 1473 | return result; |
| 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | /* ---------- Async Generator ASend Awaitable ------------ */ |
| 1478 | |
| 1479 | |
| 1480 | static void |
| 1481 | async_gen_asend_dealloc(PyAsyncGenASend *o) |
| 1482 | { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1483 | _PyObject_GC_UNTRACK((PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1484 | Py_CLEAR(o->ags_gen); |
| 1485 | Py_CLEAR(o->ags_sendval); |
| 1486 | if (ag_asend_freelist_free < _PyAsyncGen_MAXFREELIST) { |
| 1487 | assert(PyAsyncGenASend_CheckExact(o)); |
| 1488 | ag_asend_freelist[ag_asend_freelist_free++] = o; |
| 1489 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1490 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1494 | static int |
| 1495 | async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg) |
| 1496 | { |
| 1497 | Py_VISIT(o->ags_gen); |
| 1498 | Py_VISIT(o->ags_sendval); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1502 | |
| 1503 | static PyObject * |
| 1504 | async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg) |
| 1505 | { |
| 1506 | PyObject *result; |
| 1507 | |
| 1508 | if (o->ags_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1509 | PyErr_SetString( |
| 1510 | PyExc_RuntimeError, |
| 1511 | "cannot reuse already awaited __anext__()/asend()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1512 | return NULL; |
| 1513 | } |
| 1514 | |
| 1515 | if (o->ags_state == AWAITABLE_STATE_INIT) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1516 | if (o->ags_gen->ag_running_async) { |
| 1517 | PyErr_SetString( |
| 1518 | PyExc_RuntimeError, |
| 1519 | "anext(): asynchronous generator is already running"); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1523 | if (arg == NULL || arg == Py_None) { |
| 1524 | arg = o->ags_sendval; |
| 1525 | } |
| 1526 | o->ags_state = AWAITABLE_STATE_ITER; |
| 1527 | } |
| 1528 | |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1529 | o->ags_gen->ag_running_async = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1530 | result = gen_send_ex((PyGenObject*)o->ags_gen, arg, 0, 0); |
| 1531 | result = async_gen_unwrap_value(o->ags_gen, result); |
| 1532 | |
| 1533 | if (result == NULL) { |
| 1534 | o->ags_state = AWAITABLE_STATE_CLOSED; |
| 1535 | } |
| 1536 | |
| 1537 | return result; |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | static PyObject * |
| 1542 | async_gen_asend_iternext(PyAsyncGenASend *o) |
| 1543 | { |
| 1544 | return async_gen_asend_send(o, NULL); |
| 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | static PyObject * |
| 1549 | async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args) |
| 1550 | { |
| 1551 | PyObject *result; |
| 1552 | |
| 1553 | if (o->ags_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1554 | PyErr_SetString( |
| 1555 | PyExc_RuntimeError, |
| 1556 | "cannot reuse already awaited __anext__()/asend()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1557 | return NULL; |
| 1558 | } |
| 1559 | |
| 1560 | result = gen_throw((PyGenObject*)o->ags_gen, args); |
| 1561 | result = async_gen_unwrap_value(o->ags_gen, result); |
| 1562 | |
| 1563 | if (result == NULL) { |
| 1564 | o->ags_state = AWAITABLE_STATE_CLOSED; |
| 1565 | } |
| 1566 | |
| 1567 | return result; |
| 1568 | } |
| 1569 | |
| 1570 | |
| 1571 | static PyObject * |
| 1572 | async_gen_asend_close(PyAsyncGenASend *o, PyObject *args) |
| 1573 | { |
| 1574 | o->ags_state = AWAITABLE_STATE_CLOSED; |
| 1575 | Py_RETURN_NONE; |
| 1576 | } |
| 1577 | |
| 1578 | |
| 1579 | static PyMethodDef async_gen_asend_methods[] = { |
| 1580 | {"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc}, |
| 1581 | {"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc}, |
| 1582 | {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc}, |
| 1583 | {NULL, NULL} /* Sentinel */ |
| 1584 | }; |
| 1585 | |
| 1586 | |
| 1587 | static PyAsyncMethods async_gen_asend_as_async = { |
| 1588 | PyObject_SelfIter, /* am_await */ |
| 1589 | 0, /* am_aiter */ |
| 1590 | 0 /* am_anext */ |
| 1591 | }; |
| 1592 | |
| 1593 | |
| 1594 | PyTypeObject _PyAsyncGenASend_Type = { |
| 1595 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1596 | "async_generator_asend", /* tp_name */ |
| 1597 | sizeof(PyAsyncGenASend), /* tp_basicsize */ |
| 1598 | 0, /* tp_itemsize */ |
| 1599 | /* methods */ |
| 1600 | (destructor)async_gen_asend_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1601 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1602 | 0, /* tp_getattr */ |
| 1603 | 0, /* tp_setattr */ |
| 1604 | &async_gen_asend_as_async, /* tp_as_async */ |
| 1605 | 0, /* tp_repr */ |
| 1606 | 0, /* tp_as_number */ |
| 1607 | 0, /* tp_as_sequence */ |
| 1608 | 0, /* tp_as_mapping */ |
| 1609 | 0, /* tp_hash */ |
| 1610 | 0, /* tp_call */ |
| 1611 | 0, /* tp_str */ |
| 1612 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1613 | 0, /* tp_setattro */ |
| 1614 | 0, /* tp_as_buffer */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1615 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1616 | 0, /* tp_doc */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1617 | (traverseproc)async_gen_asend_traverse, /* tp_traverse */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1618 | 0, /* tp_clear */ |
| 1619 | 0, /* tp_richcompare */ |
| 1620 | 0, /* tp_weaklistoffset */ |
| 1621 | PyObject_SelfIter, /* tp_iter */ |
| 1622 | (iternextfunc)async_gen_asend_iternext, /* tp_iternext */ |
| 1623 | async_gen_asend_methods, /* tp_methods */ |
| 1624 | 0, /* tp_members */ |
| 1625 | 0, /* tp_getset */ |
| 1626 | 0, /* tp_base */ |
| 1627 | 0, /* tp_dict */ |
| 1628 | 0, /* tp_descr_get */ |
| 1629 | 0, /* tp_descr_set */ |
| 1630 | 0, /* tp_dictoffset */ |
| 1631 | 0, /* tp_init */ |
| 1632 | 0, /* tp_alloc */ |
| 1633 | 0, /* tp_new */ |
| 1634 | }; |
| 1635 | |
| 1636 | |
| 1637 | static PyObject * |
| 1638 | async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval) |
| 1639 | { |
| 1640 | PyAsyncGenASend *o; |
| 1641 | if (ag_asend_freelist_free) { |
| 1642 | ag_asend_freelist_free--; |
| 1643 | o = ag_asend_freelist[ag_asend_freelist_free]; |
| 1644 | _Py_NewReference((PyObject *)o); |
| 1645 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1646 | o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1647 | if (o == NULL) { |
| 1648 | return NULL; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | Py_INCREF(gen); |
| 1653 | o->ags_gen = gen; |
| 1654 | |
| 1655 | Py_XINCREF(sendval); |
| 1656 | o->ags_sendval = sendval; |
| 1657 | |
| 1658 | o->ags_state = AWAITABLE_STATE_INIT; |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1659 | |
| 1660 | _PyObject_GC_TRACK((PyObject*)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1661 | return (PyObject*)o; |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | /* ---------- Async Generator Value Wrapper ------------ */ |
| 1666 | |
| 1667 | |
| 1668 | static void |
| 1669 | async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o) |
| 1670 | { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1671 | _PyObject_GC_UNTRACK((PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1672 | Py_CLEAR(o->agw_val); |
| 1673 | if (ag_value_freelist_free < _PyAsyncGen_MAXFREELIST) { |
| 1674 | assert(_PyAsyncGenWrappedValue_CheckExact(o)); |
| 1675 | ag_value_freelist[ag_value_freelist_free++] = o; |
| 1676 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1677 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1682 | static int |
| 1683 | async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o, |
| 1684 | visitproc visit, void *arg) |
| 1685 | { |
| 1686 | Py_VISIT(o->agw_val); |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1691 | PyTypeObject _PyAsyncGenWrappedValue_Type = { |
| 1692 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1693 | "async_generator_wrapped_value", /* tp_name */ |
| 1694 | sizeof(_PyAsyncGenWrappedValue), /* tp_basicsize */ |
| 1695 | 0, /* tp_itemsize */ |
| 1696 | /* methods */ |
| 1697 | (destructor)async_gen_wrapped_val_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1698 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1699 | 0, /* tp_getattr */ |
| 1700 | 0, /* tp_setattr */ |
| 1701 | 0, /* tp_as_async */ |
| 1702 | 0, /* tp_repr */ |
| 1703 | 0, /* tp_as_number */ |
| 1704 | 0, /* tp_as_sequence */ |
| 1705 | 0, /* tp_as_mapping */ |
| 1706 | 0, /* tp_hash */ |
| 1707 | 0, /* tp_call */ |
| 1708 | 0, /* tp_str */ |
| 1709 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1710 | 0, /* tp_setattro */ |
| 1711 | 0, /* tp_as_buffer */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1712 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1713 | 0, /* tp_doc */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1714 | (traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1715 | 0, /* tp_clear */ |
| 1716 | 0, /* tp_richcompare */ |
| 1717 | 0, /* tp_weaklistoffset */ |
| 1718 | 0, /* tp_iter */ |
| 1719 | 0, /* tp_iternext */ |
| 1720 | 0, /* tp_methods */ |
| 1721 | 0, /* tp_members */ |
| 1722 | 0, /* tp_getset */ |
| 1723 | 0, /* tp_base */ |
| 1724 | 0, /* tp_dict */ |
| 1725 | 0, /* tp_descr_get */ |
| 1726 | 0, /* tp_descr_set */ |
| 1727 | 0, /* tp_dictoffset */ |
| 1728 | 0, /* tp_init */ |
| 1729 | 0, /* tp_alloc */ |
| 1730 | 0, /* tp_new */ |
| 1731 | }; |
| 1732 | |
| 1733 | |
| 1734 | PyObject * |
| 1735 | _PyAsyncGenValueWrapperNew(PyObject *val) |
| 1736 | { |
| 1737 | _PyAsyncGenWrappedValue *o; |
| 1738 | assert(val); |
| 1739 | |
| 1740 | if (ag_value_freelist_free) { |
| 1741 | ag_value_freelist_free--; |
| 1742 | o = ag_value_freelist[ag_value_freelist_free]; |
| 1743 | assert(_PyAsyncGenWrappedValue_CheckExact(o)); |
| 1744 | _Py_NewReference((PyObject*)o); |
| 1745 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1746 | o = PyObject_GC_New(_PyAsyncGenWrappedValue, |
| 1747 | &_PyAsyncGenWrappedValue_Type); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1748 | if (o == NULL) { |
| 1749 | return NULL; |
| 1750 | } |
| 1751 | } |
| 1752 | o->agw_val = val; |
| 1753 | Py_INCREF(val); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1754 | _PyObject_GC_TRACK((PyObject*)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1755 | return (PyObject*)o; |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | /* ---------- Async Generator AThrow awaitable ------------ */ |
| 1760 | |
| 1761 | |
| 1762 | static void |
| 1763 | async_gen_athrow_dealloc(PyAsyncGenAThrow *o) |
| 1764 | { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1765 | _PyObject_GC_UNTRACK((PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1766 | Py_CLEAR(o->agt_gen); |
| 1767 | Py_CLEAR(o->agt_args); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1768 | PyObject_GC_Del(o); |
| 1769 | } |
| 1770 | |
| 1771 | |
| 1772 | static int |
| 1773 | async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg) |
| 1774 | { |
| 1775 | Py_VISIT(o->agt_gen); |
| 1776 | Py_VISIT(o->agt_args); |
| 1777 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | static PyObject * |
| 1782 | async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg) |
| 1783 | { |
| 1784 | PyGenObject *gen = (PyGenObject*)o->agt_gen; |
| 1785 | PyFrameObject *f = gen->gi_frame; |
| 1786 | PyObject *retval; |
| 1787 | |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame] | 1788 | if (o->agt_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1789 | PyErr_SetString( |
| 1790 | PyExc_RuntimeError, |
| 1791 | "cannot reuse already awaited aclose()/athrow()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1792 | return NULL; |
| 1793 | } |
| 1794 | |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame] | 1795 | if (f == NULL || f->f_stacktop == NULL) { |
| 1796 | o->agt_state = AWAITABLE_STATE_CLOSED; |
| 1797 | PyErr_SetNone(PyExc_StopIteration); |
| 1798 | return NULL; |
| 1799 | } |
| 1800 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1801 | if (o->agt_state == AWAITABLE_STATE_INIT) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1802 | if (o->agt_gen->ag_running_async) { |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame] | 1803 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1804 | if (o->agt_args == NULL) { |
| 1805 | PyErr_SetString( |
| 1806 | PyExc_RuntimeError, |
| 1807 | "aclose(): asynchronous generator is already running"); |
| 1808 | } |
| 1809 | else { |
| 1810 | PyErr_SetString( |
| 1811 | PyExc_RuntimeError, |
| 1812 | "athrow(): asynchronous generator is already running"); |
| 1813 | } |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1817 | if (o->agt_gen->ag_closed) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1818 | o->agt_state = AWAITABLE_STATE_CLOSED; |
| 1819 | PyErr_SetNone(PyExc_StopAsyncIteration); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1820 | return NULL; |
| 1821 | } |
| 1822 | |
| 1823 | if (arg != Py_None) { |
| 1824 | PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG); |
| 1825 | return NULL; |
| 1826 | } |
| 1827 | |
| 1828 | o->agt_state = AWAITABLE_STATE_ITER; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1829 | o->agt_gen->ag_running_async = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1830 | |
| 1831 | if (o->agt_args == NULL) { |
| 1832 | /* aclose() mode */ |
| 1833 | o->agt_gen->ag_closed = 1; |
| 1834 | |
| 1835 | retval = _gen_throw((PyGenObject *)gen, |
| 1836 | 0, /* Do not close generator when |
| 1837 | PyExc_GeneratorExit is passed */ |
| 1838 | PyExc_GeneratorExit, NULL, NULL); |
| 1839 | |
| 1840 | if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) { |
| 1841 | Py_DECREF(retval); |
| 1842 | goto yield_close; |
| 1843 | } |
| 1844 | } else { |
| 1845 | PyObject *typ; |
| 1846 | PyObject *tb = NULL; |
| 1847 | PyObject *val = NULL; |
| 1848 | |
| 1849 | if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3, |
| 1850 | &typ, &val, &tb)) { |
| 1851 | return NULL; |
| 1852 | } |
| 1853 | |
| 1854 | retval = _gen_throw((PyGenObject *)gen, |
| 1855 | 0, /* Do not close generator when |
| 1856 | PyExc_GeneratorExit is passed */ |
| 1857 | typ, val, tb); |
| 1858 | retval = async_gen_unwrap_value(o->agt_gen, retval); |
| 1859 | } |
| 1860 | if (retval == NULL) { |
| 1861 | goto check_error; |
| 1862 | } |
| 1863 | return retval; |
| 1864 | } |
| 1865 | |
| 1866 | assert(o->agt_state == AWAITABLE_STATE_ITER); |
| 1867 | |
| 1868 | retval = gen_send_ex((PyGenObject *)gen, arg, 0, 0); |
| 1869 | if (o->agt_args) { |
| 1870 | return async_gen_unwrap_value(o->agt_gen, retval); |
| 1871 | } else { |
| 1872 | /* aclose() mode */ |
| 1873 | if (retval) { |
| 1874 | if (_PyAsyncGenWrappedValue_CheckExact(retval)) { |
| 1875 | Py_DECREF(retval); |
| 1876 | goto yield_close; |
| 1877 | } |
| 1878 | else { |
| 1879 | return retval; |
| 1880 | } |
| 1881 | } |
| 1882 | else { |
| 1883 | goto check_error; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | yield_close: |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1888 | o->agt_gen->ag_running_async = 0; |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame] | 1889 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1890 | PyErr_SetString( |
| 1891 | PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG); |
| 1892 | return NULL; |
| 1893 | |
| 1894 | check_error: |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1895 | o->agt_gen->ag_running_async = 0; |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame] | 1896 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | 52698c7 | 2018-06-07 20:31:26 -0400 | [diff] [blame] | 1897 | if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) || |
| 1898 | PyErr_ExceptionMatches(PyExc_GeneratorExit)) |
| 1899 | { |
Yury Selivanov | 41782e4 | 2016-11-16 18:16:17 -0500 | [diff] [blame] | 1900 | if (o->agt_args == NULL) { |
| 1901 | /* when aclose() is called we don't want to propagate |
Yury Selivanov | 52698c7 | 2018-06-07 20:31:26 -0400 | [diff] [blame] | 1902 | StopAsyncIteration or GeneratorExit; just raise |
| 1903 | StopIteration, signalling that this 'aclose()' await |
| 1904 | is done. |
| 1905 | */ |
Yury Selivanov | 41782e4 | 2016-11-16 18:16:17 -0500 | [diff] [blame] | 1906 | PyErr_Clear(); |
| 1907 | PyErr_SetNone(PyExc_StopIteration); |
| 1908 | } |
| 1909 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1910 | return NULL; |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | static PyObject * |
| 1915 | async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args) |
| 1916 | { |
| 1917 | PyObject *retval; |
| 1918 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1919 | if (o->agt_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1920 | PyErr_SetString( |
| 1921 | PyExc_RuntimeError, |
| 1922 | "cannot reuse already awaited aclose()/athrow()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1923 | return NULL; |
| 1924 | } |
| 1925 | |
| 1926 | retval = gen_throw((PyGenObject*)o->agt_gen, args); |
| 1927 | if (o->agt_args) { |
| 1928 | return async_gen_unwrap_value(o->agt_gen, retval); |
| 1929 | } else { |
| 1930 | /* aclose() mode */ |
| 1931 | if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1932 | o->agt_gen->ag_running_async = 0; |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame] | 1933 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1934 | Py_DECREF(retval); |
| 1935 | PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG); |
| 1936 | return NULL; |
| 1937 | } |
Vincent Michel | 8e0de2a | 2019-11-19 05:53:52 -0800 | [diff] [blame] | 1938 | if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) || |
| 1939 | PyErr_ExceptionMatches(PyExc_GeneratorExit)) |
| 1940 | { |
| 1941 | /* when aclose() is called we don't want to propagate |
| 1942 | StopAsyncIteration or GeneratorExit; just raise |
| 1943 | StopIteration, signalling that this 'aclose()' await |
| 1944 | is done. |
| 1945 | */ |
| 1946 | PyErr_Clear(); |
| 1947 | PyErr_SetNone(PyExc_StopIteration); |
| 1948 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1949 | return retval; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | |
| 1954 | static PyObject * |
| 1955 | async_gen_athrow_iternext(PyAsyncGenAThrow *o) |
| 1956 | { |
| 1957 | return async_gen_athrow_send(o, Py_None); |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | static PyObject * |
| 1962 | async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args) |
| 1963 | { |
| 1964 | o->agt_state = AWAITABLE_STATE_CLOSED; |
| 1965 | Py_RETURN_NONE; |
| 1966 | } |
| 1967 | |
| 1968 | |
| 1969 | static PyMethodDef async_gen_athrow_methods[] = { |
| 1970 | {"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc}, |
| 1971 | {"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc}, |
| 1972 | {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc}, |
| 1973 | {NULL, NULL} /* Sentinel */ |
| 1974 | }; |
| 1975 | |
| 1976 | |
| 1977 | static PyAsyncMethods async_gen_athrow_as_async = { |
| 1978 | PyObject_SelfIter, /* am_await */ |
| 1979 | 0, /* am_aiter */ |
| 1980 | 0 /* am_anext */ |
| 1981 | }; |
| 1982 | |
| 1983 | |
| 1984 | PyTypeObject _PyAsyncGenAThrow_Type = { |
| 1985 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1986 | "async_generator_athrow", /* tp_name */ |
| 1987 | sizeof(PyAsyncGenAThrow), /* tp_basicsize */ |
| 1988 | 0, /* tp_itemsize */ |
| 1989 | /* methods */ |
| 1990 | (destructor)async_gen_athrow_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1991 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1992 | 0, /* tp_getattr */ |
| 1993 | 0, /* tp_setattr */ |
| 1994 | &async_gen_athrow_as_async, /* tp_as_async */ |
| 1995 | 0, /* tp_repr */ |
| 1996 | 0, /* tp_as_number */ |
| 1997 | 0, /* tp_as_sequence */ |
| 1998 | 0, /* tp_as_mapping */ |
| 1999 | 0, /* tp_hash */ |
| 2000 | 0, /* tp_call */ |
| 2001 | 0, /* tp_str */ |
| 2002 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2003 | 0, /* tp_setattro */ |
| 2004 | 0, /* tp_as_buffer */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2005 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2006 | 0, /* tp_doc */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2007 | (traverseproc)async_gen_athrow_traverse, /* tp_traverse */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2008 | 0, /* tp_clear */ |
| 2009 | 0, /* tp_richcompare */ |
| 2010 | 0, /* tp_weaklistoffset */ |
| 2011 | PyObject_SelfIter, /* tp_iter */ |
| 2012 | (iternextfunc)async_gen_athrow_iternext, /* tp_iternext */ |
| 2013 | async_gen_athrow_methods, /* tp_methods */ |
| 2014 | 0, /* tp_members */ |
| 2015 | 0, /* tp_getset */ |
| 2016 | 0, /* tp_base */ |
| 2017 | 0, /* tp_dict */ |
| 2018 | 0, /* tp_descr_get */ |
| 2019 | 0, /* tp_descr_set */ |
| 2020 | 0, /* tp_dictoffset */ |
| 2021 | 0, /* tp_init */ |
| 2022 | 0, /* tp_alloc */ |
| 2023 | 0, /* tp_new */ |
| 2024 | }; |
| 2025 | |
| 2026 | |
| 2027 | static PyObject * |
| 2028 | async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args) |
| 2029 | { |
| 2030 | PyAsyncGenAThrow *o; |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2031 | o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2032 | if (o == NULL) { |
| 2033 | return NULL; |
| 2034 | } |
| 2035 | o->agt_gen = gen; |
| 2036 | o->agt_args = args; |
| 2037 | o->agt_state = AWAITABLE_STATE_INIT; |
| 2038 | Py_INCREF(gen); |
| 2039 | Py_XINCREF(args); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2040 | _PyObject_GC_TRACK((PyObject*)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2041 | return (PyObject*)o; |
| 2042 | } |