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