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