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