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