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