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