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 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 8 | static PyObject *gen_close(PyGenObject *gen, PyObject *args); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 9 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 10 | static int |
| 11 | gen_traverse(PyGenObject *gen, visitproc visit, void *arg) |
| 12 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | Py_VISIT((PyObject *)gen->gi_frame); |
| 14 | Py_VISIT(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 15 | Py_VISIT(gen->gi_name); |
| 16 | Py_VISIT(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | return 0; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 20 | void |
| 21 | _PyGen_Finalize(PyObject *self) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 22 | { |
| 23 | PyGenObject *gen = (PyGenObject *)self; |
| 24 | PyObject *res; |
| 25 | PyObject *error_type, *error_value, *error_traceback; |
| 26 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 27 | /* If `gen` is a coroutine, and if it was never awaited on, |
| 28 | issue a RuntimeWarning. */ |
| 29 | if (gen->gi_code != NULL |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 30 | && ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 31 | && gen->gi_frame != NULL |
| 32 | && gen->gi_frame->f_lasti == -1 |
| 33 | && !PyErr_Occurred() |
| 34 | && PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 35 | "coroutine '%.50S' was never awaited", |
| 36 | gen->gi_qualname)) |
| 37 | return; |
| 38 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 39 | if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) |
| 40 | /* Generator isn't paused, so no need to close */ |
| 41 | return; |
| 42 | |
| 43 | /* Save the current exception, if any. */ |
| 44 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 45 | |
| 46 | res = gen_close(gen, 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 | } |
| 56 | |
| 57 | static void |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 58 | gen_dealloc(PyGenObject *gen) |
| 59 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | PyObject *self = (PyObject *) gen; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | _PyObject_GC_UNTRACK(gen); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 63 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | if (gen->gi_weakreflist != NULL) |
| 65 | PyObject_ClearWeakRefs(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 67 | _PyObject_GC_TRACK(self); |
| 68 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 69 | if (PyObject_CallFinalizerFromDealloc(self)) |
| 70 | return; /* resurrected. :( */ |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 71 | |
| 72 | _PyObject_GC_UNTRACK(self); |
| 73 | Py_CLEAR(gen->gi_frame); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | Py_CLEAR(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 75 | Py_CLEAR(gen->gi_name); |
| 76 | Py_CLEAR(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | PyObject_GC_Del(gen); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 81 | gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 82 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 83 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 85 | PyObject *result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 86 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 87 | if (gen->gi_running) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 88 | char *msg = "generator already executing"; |
| 89 | if (PyCoro_CheckExact(gen)) |
| 90 | msg = "coroutine already executing"; |
| 91 | PyErr_SetString(PyExc_ValueError, msg); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 94 | if (f == NULL || f->f_stacktop == NULL) { |
| 95 | /* Only set exception if called from send() */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (arg && !exc) |
| 97 | PyErr_SetNone(PyExc_StopIteration); |
| 98 | return NULL; |
| 99 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 100 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 101 | if (f->f_lasti == -1) { |
| 102 | if (arg && arg != Py_None) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 103 | char *msg = "can't send non-None value to a " |
| 104 | "just-started generator"; |
| 105 | if (PyCoro_CheckExact(gen)) |
| 106 | msg = "can't send non-None value to a " |
| 107 | "just-started coroutine"; |
| 108 | PyErr_SetString(PyExc_TypeError, msg); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | } else { |
| 112 | /* Push arg onto the frame's value stack */ |
| 113 | result = arg ? arg : Py_None; |
| 114 | Py_INCREF(result); |
| 115 | *(f->f_stacktop++) = result; |
| 116 | } |
| 117 | |
| 118 | /* Generators always return to their most recent caller, not |
| 119 | * necessarily their creator. */ |
| 120 | Py_XINCREF(tstate->frame); |
| 121 | assert(f->f_back == NULL); |
| 122 | f->f_back = tstate->frame; |
| 123 | |
| 124 | gen->gi_running = 1; |
| 125 | result = PyEval_EvalFrameEx(f, exc); |
| 126 | gen->gi_running = 0; |
| 127 | |
| 128 | /* Don't keep the reference to f_back any longer than necessary. It |
| 129 | * may keep a chain of frames alive or it could create a reference |
| 130 | * cycle. */ |
| 131 | assert(f->f_back == tstate->frame); |
| 132 | Py_CLEAR(f->f_back); |
| 133 | |
| 134 | /* If the generator just returned (as opposed to yielding), signal |
| 135 | * that the generator is exhausted. */ |
| 136 | if (result && f->f_stacktop == NULL) { |
| 137 | if (result == Py_None) { |
| 138 | /* Delay exception instantiation if we can */ |
| 139 | PyErr_SetNone(PyExc_StopIteration); |
| 140 | } else { |
| 141 | PyObject *e = PyObject_CallFunctionObjArgs( |
| 142 | PyExc_StopIteration, result, NULL); |
| 143 | if (e != NULL) { |
| 144 | PyErr_SetObject(PyExc_StopIteration, e); |
| 145 | Py_DECREF(e); |
| 146 | } |
| 147 | } |
| 148 | Py_CLEAR(result); |
| 149 | } |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 150 | else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) { |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 151 | /* Check for __future__ generator_stop and conditionally turn |
| 152 | * a leaking StopIteration into RuntimeError (with its cause |
| 153 | * set appropriately). */ |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 154 | if (((PyCodeObject *)gen->gi_code)->co_flags & |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 155 | (CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE)) |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 156 | { |
| 157 | PyObject *exc, *val, *val2, *tb; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 158 | char *msg = "generator raised StopIteration"; |
| 159 | if (PyCoro_CheckExact(gen)) |
| 160 | msg = "coroutine raised StopIteration"; |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 161 | PyErr_Fetch(&exc, &val, &tb); |
| 162 | PyErr_NormalizeException(&exc, &val, &tb); |
| 163 | if (tb != NULL) |
| 164 | PyException_SetTraceback(val, tb); |
| 165 | Py_DECREF(exc); |
| 166 | Py_XDECREF(tb); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 167 | PyErr_SetString(PyExc_RuntimeError, msg); |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 168 | PyErr_Fetch(&exc, &val2, &tb); |
| 169 | PyErr_NormalizeException(&exc, &val2, &tb); |
Yury Selivanov | 18c30a2 | 2015-05-10 15:09:46 -0400 | [diff] [blame] | 170 | Py_INCREF(val); |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 171 | PyException_SetCause(val2, val); |
| 172 | PyException_SetContext(val2, val); |
| 173 | PyErr_Restore(exc, val2, tb); |
| 174 | } |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 175 | else { |
| 176 | PyObject *exc, *val, *tb; |
| 177 | |
| 178 | /* Pop the exception before issuing a warning. */ |
| 179 | PyErr_Fetch(&exc, &val, &tb); |
| 180 | |
| 181 | if (PyErr_WarnFormat(PyExc_PendingDeprecationWarning, 1, |
| 182 | "generator '%.50S' raised StopIteration", |
| 183 | gen->gi_qualname)) { |
| 184 | /* Warning was converted to an error. */ |
| 185 | Py_XDECREF(exc); |
| 186 | Py_XDECREF(val); |
| 187 | Py_XDECREF(tb); |
| 188 | } |
| 189 | else { |
| 190 | PyErr_Restore(exc, val, tb); |
| 191 | } |
| 192 | } |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 193 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 194 | |
| 195 | if (!result || f->f_stacktop == NULL) { |
| 196 | /* generator can't be rerun, so release the frame */ |
| 197 | /* first clean reference cycle through stored exception traceback */ |
| 198 | PyObject *t, *v, *tb; |
| 199 | t = f->f_exc_type; |
| 200 | v = f->f_exc_value; |
| 201 | tb = f->f_exc_traceback; |
| 202 | f->f_exc_type = NULL; |
| 203 | f->f_exc_value = NULL; |
| 204 | f->f_exc_traceback = NULL; |
| 205 | Py_XDECREF(t); |
| 206 | Py_XDECREF(v); |
| 207 | Py_XDECREF(tb); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 208 | gen->gi_frame->f_gen = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 209 | gen->gi_frame = NULL; |
| 210 | Py_DECREF(f); |
| 211 | } |
| 212 | |
| 213 | return result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 216 | PyDoc_STRVAR(send_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 217 | "send(arg) -> send 'arg' into generator,\n\ |
| 218 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 219 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 220 | PyObject * |
| 221 | _PyGen_Send(PyGenObject *gen, PyObject *arg) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 222 | { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 223 | return gen_send_ex(gen, arg, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | PyDoc_STRVAR(close_doc, |
Benjamin Peterson | ab3da29 | 2012-05-03 18:44:09 -0400 | [diff] [blame] | 227 | "close() -> raise GeneratorExit inside generator."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 228 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 229 | /* |
| 230 | * This helper function is used by gen_close and gen_throw to |
| 231 | * close a subiterator being delegated to by yield-from. |
| 232 | */ |
| 233 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 234 | static int |
| 235 | gen_close_iter(PyObject *yf) |
| 236 | { |
| 237 | PyObject *retval = NULL; |
| 238 | _Py_IDENTIFIER(close); |
| 239 | |
| 240 | if (PyGen_CheckExact(yf)) { |
| 241 | retval = gen_close((PyGenObject *)yf, NULL); |
| 242 | if (retval == NULL) |
| 243 | return -1; |
| 244 | } else { |
| 245 | PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close); |
| 246 | if (meth == NULL) { |
| 247 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 248 | PyErr_WriteUnraisable(yf); |
| 249 | PyErr_Clear(); |
| 250 | } else { |
| 251 | retval = PyObject_CallFunction(meth, ""); |
| 252 | Py_DECREF(meth); |
| 253 | if (retval == NULL) |
| 254 | return -1; |
| 255 | } |
| 256 | } |
| 257 | Py_XDECREF(retval); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 261 | static PyObject * |
| 262 | gen_yf(PyGenObject *gen) |
| 263 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 264 | PyObject *yf = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 265 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 266 | |
| 267 | if (f && f->f_stacktop) { |
| 268 | PyObject *bytecode = f->f_code->co_code; |
| 269 | unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode); |
| 270 | |
| 271 | if (code[f->f_lasti + 1] != YIELD_FROM) |
| 272 | return NULL; |
| 273 | yf = f->f_stacktop[-1]; |
| 274 | Py_INCREF(yf); |
| 275 | } |
| 276 | |
| 277 | return yf; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 278 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 279 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 280 | static PyObject * |
| 281 | gen_close(PyGenObject *gen, PyObject *args) |
| 282 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 283 | PyObject *retval; |
| 284 | PyObject *yf = gen_yf(gen); |
| 285 | int err = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 286 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 287 | if (yf) { |
| 288 | gen->gi_running = 1; |
| 289 | err = gen_close_iter(yf); |
| 290 | gen->gi_running = 0; |
| 291 | Py_DECREF(yf); |
| 292 | } |
| 293 | if (err == 0) |
| 294 | PyErr_SetNone(PyExc_GeneratorExit); |
| 295 | retval = gen_send_ex(gen, Py_None, 1); |
| 296 | if (retval) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 297 | char *msg = "generator ignored GeneratorExit"; |
| 298 | if (PyCoro_CheckExact(gen)) |
| 299 | msg = "coroutine ignored GeneratorExit"; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 300 | Py_DECREF(retval); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 301 | PyErr_SetString(PyExc_RuntimeError, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | return NULL; |
| 303 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 304 | if (PyErr_ExceptionMatches(PyExc_StopIteration) |
| 305 | || PyErr_ExceptionMatches(PyExc_GeneratorExit)) { |
| 306 | PyErr_Clear(); /* ignore these errors */ |
| 307 | Py_INCREF(Py_None); |
| 308 | return Py_None; |
| 309 | } |
| 310 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 313 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 314 | PyDoc_STRVAR(throw_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 315 | "throw(typ[,val[,tb]]) -> raise exception in generator,\n\ |
| 316 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 317 | |
| 318 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 319 | gen_throw(PyGenObject *gen, PyObject *args) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | PyObject *typ; |
| 322 | PyObject *tb = NULL; |
| 323 | PyObject *val = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 324 | PyObject *yf = gen_yf(gen); |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 325 | _Py_IDENTIFIER(throw); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) |
| 328 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 329 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 330 | if (yf) { |
| 331 | PyObject *ret; |
| 332 | int err; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 333 | if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 334 | gen->gi_running = 1; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 335 | err = gen_close_iter(yf); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 336 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 337 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 338 | if (err < 0) |
| 339 | return gen_send_ex(gen, Py_None, 1); |
| 340 | goto throw_here; |
| 341 | } |
| 342 | if (PyGen_CheckExact(yf)) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 343 | gen->gi_running = 1; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 344 | ret = gen_throw((PyGenObject *)yf, args); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 345 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 346 | } else { |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 347 | PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 348 | if (meth == NULL) { |
| 349 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 350 | Py_DECREF(yf); |
| 351 | return NULL; |
| 352 | } |
| 353 | PyErr_Clear(); |
| 354 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 355 | goto throw_here; |
| 356 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 357 | gen->gi_running = 1; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 358 | ret = PyObject_CallObject(meth, args); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 359 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 360 | Py_DECREF(meth); |
| 361 | } |
| 362 | Py_DECREF(yf); |
| 363 | if (!ret) { |
| 364 | PyObject *val; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 365 | /* Pop subiterator from stack */ |
| 366 | ret = *(--gen->gi_frame->f_stacktop); |
| 367 | assert(ret == yf); |
| 368 | Py_DECREF(ret); |
| 369 | /* Termination repetition of YIELD_FROM */ |
| 370 | gen->gi_frame->f_lasti++; |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 371 | if (_PyGen_FetchStopIterationValue(&val) == 0) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 372 | ret = gen_send_ex(gen, val, 0); |
| 373 | Py_DECREF(val); |
| 374 | } else { |
| 375 | ret = gen_send_ex(gen, Py_None, 1); |
| 376 | } |
| 377 | } |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | throw_here: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | /* First, check the traceback argument, replacing None with |
| 383 | NULL. */ |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 384 | if (tb == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | tb = NULL; |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 386 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 388 | PyErr_SetString(PyExc_TypeError, |
| 389 | "throw() third argument must be a traceback object"); |
| 390 | return NULL; |
| 391 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | Py_INCREF(typ); |
| 394 | Py_XINCREF(val); |
| 395 | Py_XINCREF(tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 397 | if (PyExceptionClass_Check(typ)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | PyErr_NormalizeException(&typ, &val, &tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | else if (PyExceptionInstance_Check(typ)) { |
| 401 | /* Raising an instance. The value should be a dummy. */ |
| 402 | if (val && val != Py_None) { |
| 403 | PyErr_SetString(PyExc_TypeError, |
| 404 | "instance exception may not have a separate value"); |
| 405 | goto failed_throw; |
| 406 | } |
| 407 | else { |
| 408 | /* Normalize to raise <class>, <instance> */ |
| 409 | Py_XDECREF(val); |
| 410 | val = typ; |
| 411 | typ = PyExceptionInstance_Class(typ); |
| 412 | Py_INCREF(typ); |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 413 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 414 | if (tb == NULL) |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 415 | /* Returns NULL if there's no traceback */ |
| 416 | tb = PyException_GetTraceback(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | else { |
| 420 | /* Not something you can raise. throw() fails. */ |
| 421 | PyErr_Format(PyExc_TypeError, |
| 422 | "exceptions must be classes or instances " |
| 423 | "deriving from BaseException, not %s", |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 424 | Py_TYPE(typ)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | goto failed_throw; |
| 426 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | PyErr_Restore(typ, val, tb); |
| 429 | return gen_send_ex(gen, Py_None, 1); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 430 | |
| 431 | failed_throw: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | /* Didn't use our arguments, so restore their original refcounts */ |
| 433 | Py_DECREF(typ); |
| 434 | Py_XDECREF(val); |
| 435 | Py_XDECREF(tb); |
| 436 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
| 440 | static PyObject * |
| 441 | gen_iternext(PyGenObject *gen) |
| 442 | { |
Antoine Pitrou | 667f545 | 2014-07-08 18:43:23 -0400 | [diff] [blame] | 443 | return gen_send_ex(gen, NULL, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 446 | /* |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 447 | * If StopIteration exception is set, fetches its 'value' |
| 448 | * attribute if any, otherwise sets pvalue to None. |
| 449 | * |
| 450 | * Returns 0 if no exception or StopIteration is set. |
| 451 | * If any other exception is set, returns -1 and leaves |
| 452 | * pvalue unchanged. |
| 453 | */ |
| 454 | |
| 455 | int |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 456 | _PyGen_FetchStopIterationValue(PyObject **pvalue) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 457 | PyObject *et, *ev, *tb; |
| 458 | PyObject *value = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 459 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 460 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 461 | PyErr_Fetch(&et, &ev, &tb); |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 462 | if (ev) { |
| 463 | /* exception will usually be normalised already */ |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 464 | if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 465 | value = ((PyStopIterationObject *)ev)->value; |
| 466 | Py_INCREF(value); |
| 467 | Py_DECREF(ev); |
| 468 | } else if (et == PyExc_StopIteration) { |
| 469 | /* avoid normalisation and take ev as value */ |
| 470 | value = ev; |
| 471 | } else { |
| 472 | /* normalisation required */ |
| 473 | PyErr_NormalizeException(&et, &ev, &tb); |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 474 | if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 475 | PyErr_Restore(et, ev, tb); |
| 476 | return -1; |
| 477 | } |
| 478 | value = ((PyStopIterationObject *)ev)->value; |
| 479 | Py_INCREF(value); |
| 480 | Py_DECREF(ev); |
| 481 | } |
| 482 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 483 | Py_XDECREF(et); |
| 484 | Py_XDECREF(tb); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 485 | } else if (PyErr_Occurred()) { |
| 486 | return -1; |
| 487 | } |
| 488 | if (value == NULL) { |
| 489 | value = Py_None; |
Amaury Forgeot d'Arc | e557da8 | 2012-01-13 21:06:12 +0100 | [diff] [blame] | 490 | Py_INCREF(value); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 491 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 492 | *pvalue = value; |
| 493 | return 0; |
| 494 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 495 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 496 | static PyObject * |
| 497 | gen_repr(PyGenObject *gen) |
| 498 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 499 | return PyUnicode_FromFormat("<generator object %S at %p>", |
| 500 | gen->gi_qualname, gen); |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 503 | static PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 504 | gen_get_name(PyGenObject *op) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 505 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 506 | Py_INCREF(op->gi_name); |
| 507 | return op->gi_name; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 510 | static int |
| 511 | gen_set_name(PyGenObject *op, PyObject *value) |
| 512 | { |
| 513 | PyObject *tmp; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 514 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 515 | /* Not legal to del gen.gi_name or to set it to anything |
| 516 | * other than a string object. */ |
| 517 | if (value == NULL || !PyUnicode_Check(value)) { |
| 518 | PyErr_SetString(PyExc_TypeError, |
| 519 | "__name__ must be set to a string object"); |
| 520 | return -1; |
| 521 | } |
| 522 | tmp = op->gi_name; |
| 523 | Py_INCREF(value); |
| 524 | op->gi_name = value; |
| 525 | Py_DECREF(tmp); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static PyObject * |
| 530 | gen_get_qualname(PyGenObject *op) |
| 531 | { |
| 532 | Py_INCREF(op->gi_qualname); |
| 533 | return op->gi_qualname; |
| 534 | } |
| 535 | |
| 536 | static int |
| 537 | gen_set_qualname(PyGenObject *op, PyObject *value) |
| 538 | { |
| 539 | PyObject *tmp; |
| 540 | |
| 541 | /* Not legal to del gen.__qualname__ or to set it to anything |
| 542 | * other than a string object. */ |
| 543 | if (value == NULL || !PyUnicode_Check(value)) { |
| 544 | PyErr_SetString(PyExc_TypeError, |
| 545 | "__qualname__ must be set to a string object"); |
| 546 | return -1; |
| 547 | } |
| 548 | tmp = op->gi_qualname; |
| 549 | Py_INCREF(value); |
| 550 | op->gi_qualname = value; |
| 551 | Py_DECREF(tmp); |
| 552 | return 0; |
| 553 | } |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 554 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 555 | static PyObject * |
| 556 | gen_getyieldfrom(PyGenObject *gen) |
| 557 | { |
| 558 | PyObject *yf = gen_yf(gen); |
| 559 | if (yf == NULL) |
| 560 | Py_RETURN_NONE; |
| 561 | return yf; |
| 562 | } |
| 563 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 564 | static PyGetSetDef gen_getsetlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 565 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 566 | PyDoc_STR("name of the generator")}, |
| 567 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 568 | PyDoc_STR("qualified name of the generator")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 569 | {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL, |
| 570 | PyDoc_STR("object being iterated by yield from, or None")}, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 571 | {NULL} /* Sentinel */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 572 | }; |
| 573 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 574 | static PyMemberDef gen_memberlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 575 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
| 576 | {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY}, |
| 577 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | {NULL} /* Sentinel */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 579 | }; |
| 580 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 581 | static PyMethodDef gen_methods[] = { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 582 | {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 584 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 585 | {NULL, NULL} /* Sentinel */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 586 | }; |
| 587 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 588 | PyTypeObject PyGen_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 590 | "generator", /* tp_name */ |
| 591 | sizeof(PyGenObject), /* tp_basicsize */ |
| 592 | 0, /* tp_itemsize */ |
| 593 | /* methods */ |
| 594 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 595 | 0, /* tp_print */ |
| 596 | 0, /* tp_getattr */ |
| 597 | 0, /* tp_setattr */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 598 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | (reprfunc)gen_repr, /* tp_repr */ |
| 600 | 0, /* tp_as_number */ |
| 601 | 0, /* tp_as_sequence */ |
| 602 | 0, /* tp_as_mapping */ |
| 603 | 0, /* tp_hash */ |
| 604 | 0, /* tp_call */ |
| 605 | 0, /* tp_str */ |
| 606 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 607 | 0, /* tp_setattro */ |
| 608 | 0, /* tp_as_buffer */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 609 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 610 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | 0, /* tp_doc */ |
| 612 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 613 | 0, /* tp_clear */ |
| 614 | 0, /* tp_richcompare */ |
| 615 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 616 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 618 | gen_methods, /* tp_methods */ |
| 619 | gen_memberlist, /* tp_members */ |
| 620 | gen_getsetlist, /* tp_getset */ |
| 621 | 0, /* tp_base */ |
| 622 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | 0, /* tp_descr_get */ |
| 625 | 0, /* tp_descr_set */ |
| 626 | 0, /* tp_dictoffset */ |
| 627 | 0, /* tp_init */ |
| 628 | 0, /* tp_alloc */ |
| 629 | 0, /* tp_new */ |
| 630 | 0, /* tp_free */ |
| 631 | 0, /* tp_is_gc */ |
| 632 | 0, /* tp_bases */ |
| 633 | 0, /* tp_mro */ |
| 634 | 0, /* tp_cache */ |
| 635 | 0, /* tp_subclasses */ |
| 636 | 0, /* tp_weaklist */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 637 | 0, /* tp_del */ |
| 638 | 0, /* tp_version_tag */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 639 | _PyGen_Finalize, /* tp_finalize */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 642 | static PyObject * |
| 643 | gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f, |
| 644 | PyObject *name, PyObject *qualname) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 645 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 646 | PyGenObject *gen = PyObject_GC_New(PyGenObject, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | if (gen == NULL) { |
| 648 | Py_DECREF(f); |
| 649 | return NULL; |
| 650 | } |
| 651 | gen->gi_frame = f; |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 652 | f->f_gen = (PyObject *) gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | Py_INCREF(f->f_code); |
| 654 | gen->gi_code = (PyObject *)(f->f_code); |
| 655 | gen->gi_running = 0; |
| 656 | gen->gi_weakreflist = NULL; |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 657 | if (name != NULL) |
| 658 | gen->gi_name = name; |
| 659 | else |
| 660 | gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 661 | Py_INCREF(gen->gi_name); |
| 662 | if (qualname != NULL) |
| 663 | gen->gi_qualname = qualname; |
| 664 | else |
| 665 | gen->gi_qualname = gen->gi_name; |
| 666 | Py_INCREF(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | _PyObject_GC_TRACK(gen); |
| 668 | return (PyObject *)gen; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 669 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 670 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 671 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 672 | PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 673 | { |
| 674 | return gen_new_with_qualname(&PyGen_Type, f, name, qualname); |
| 675 | } |
| 676 | |
| 677 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 678 | PyGen_New(PyFrameObject *f) |
| 679 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 680 | return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 683 | int |
| 684 | PyGen_NeedsFinalizing(PyGenObject *gen) |
| 685 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 686 | int i; |
| 687 | PyFrameObject *f = gen->gi_frame; |
| 688 | |
| 689 | if (f == NULL || f->f_stacktop == NULL) |
| 690 | return 0; /* no frame or empty blockstack == no finalization */ |
| 691 | |
| 692 | /* Any block type besides a loop requires cleanup. */ |
| 693 | for (i = 0; i < f->f_iblock; i++) |
| 694 | if (f->f_blockstack[i].b_type != SETUP_LOOP) |
| 695 | return 1; |
| 696 | |
| 697 | /* No blocks except loops, it's safe to skip finalization. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 699 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 700 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 701 | /* Coroutine Object */ |
| 702 | |
| 703 | typedef struct { |
| 704 | PyObject_HEAD |
| 705 | PyCoroObject *cw_coroutine; |
| 706 | } PyCoroWrapper; |
| 707 | |
| 708 | static int |
| 709 | gen_is_coroutine(PyObject *o) |
| 710 | { |
| 711 | if (PyGen_CheckExact(o)) { |
| 712 | PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code; |
| 713 | if (code->co_flags & CO_ITERABLE_COROUTINE) { |
| 714 | return 1; |
| 715 | } |
| 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 720 | /* |
| 721 | * This helper function returns an awaitable for `o`: |
| 722 | * - `o` if `o` is a coroutine-object; |
| 723 | * - `type(o)->tp_as_async->am_await(o)` |
| 724 | * |
| 725 | * Raises a TypeError if it's not possible to return |
| 726 | * an awaitable and returns NULL. |
| 727 | */ |
| 728 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 729 | _PyCoro_GetAwaitableIter(PyObject *o) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 730 | { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 731 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 732 | PyTypeObject *ot; |
| 733 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 734 | if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) { |
| 735 | /* 'o' is a coroutine. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 736 | Py_INCREF(o); |
| 737 | return o; |
| 738 | } |
| 739 | |
| 740 | ot = Py_TYPE(o); |
| 741 | if (ot->tp_as_async != NULL) { |
| 742 | getter = ot->tp_as_async->am_await; |
| 743 | } |
| 744 | if (getter != NULL) { |
| 745 | PyObject *res = (*getter)(o); |
| 746 | if (res != NULL) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 747 | if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) { |
| 748 | /* __await__ must return an *iterator*, not |
| 749 | a coroutine or another awaitable (see PEP 492) */ |
| 750 | PyErr_SetString(PyExc_TypeError, |
| 751 | "__await__() returned a coroutine"); |
| 752 | Py_CLEAR(res); |
| 753 | } else if (!PyIter_Check(res)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 754 | PyErr_Format(PyExc_TypeError, |
| 755 | "__await__() returned non-iterator " |
| 756 | "of type '%.100s'", |
| 757 | Py_TYPE(res)->tp_name); |
| 758 | Py_CLEAR(res); |
| 759 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 760 | } |
| 761 | return res; |
| 762 | } |
| 763 | |
| 764 | PyErr_Format(PyExc_TypeError, |
| 765 | "object %.100s can't be used in 'await' expression", |
| 766 | ot->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 767 | return NULL; |
| 768 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 769 | |
| 770 | static PyObject * |
| 771 | coro_repr(PyCoroObject *coro) |
| 772 | { |
| 773 | return PyUnicode_FromFormat("<coroutine object %S at %p>", |
| 774 | coro->cr_qualname, coro); |
| 775 | } |
| 776 | |
| 777 | static PyObject * |
| 778 | coro_await(PyCoroObject *coro) |
| 779 | { |
| 780 | PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type); |
| 781 | if (cw == NULL) { |
| 782 | return NULL; |
| 783 | } |
| 784 | Py_INCREF(coro); |
| 785 | cw->cw_coroutine = coro; |
| 786 | _PyObject_GC_TRACK(cw); |
| 787 | return (PyObject *)cw; |
| 788 | } |
| 789 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 790 | static PyObject * |
| 791 | coro_get_cr_await(PyCoroObject *coro) |
| 792 | { |
| 793 | PyObject *yf = gen_yf((PyGenObject *) coro); |
| 794 | if (yf == NULL) |
| 795 | Py_RETURN_NONE; |
| 796 | return yf; |
| 797 | } |
| 798 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 799 | static PyGetSetDef coro_getsetlist[] = { |
| 800 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 801 | PyDoc_STR("name of the coroutine")}, |
| 802 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 803 | PyDoc_STR("qualified name of the coroutine")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 804 | {"cr_await", (getter)coro_get_cr_await, NULL, |
| 805 | PyDoc_STR("object being awaited on, or None")}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 806 | {NULL} /* Sentinel */ |
| 807 | }; |
| 808 | |
| 809 | static PyMemberDef coro_memberlist[] = { |
| 810 | {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY}, |
| 811 | {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY}, |
| 812 | {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY}, |
| 813 | {NULL} /* Sentinel */ |
| 814 | }; |
| 815 | |
| 816 | PyDoc_STRVAR(coro_send_doc, |
| 817 | "send(arg) -> send 'arg' into coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 818 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 819 | |
| 820 | PyDoc_STRVAR(coro_throw_doc, |
| 821 | "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 822 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 823 | |
| 824 | PyDoc_STRVAR(coro_close_doc, |
| 825 | "close() -> raise GeneratorExit inside coroutine."); |
| 826 | |
| 827 | static PyMethodDef coro_methods[] = { |
| 828 | {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc}, |
| 829 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc}, |
| 830 | {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc}, |
| 831 | {NULL, NULL} /* Sentinel */ |
| 832 | }; |
| 833 | |
| 834 | static PyAsyncMethods coro_as_async = { |
| 835 | (unaryfunc)coro_await, /* am_await */ |
| 836 | 0, /* am_aiter */ |
| 837 | 0 /* am_anext */ |
| 838 | }; |
| 839 | |
| 840 | PyTypeObject PyCoro_Type = { |
| 841 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 842 | "coroutine", /* tp_name */ |
| 843 | sizeof(PyCoroObject), /* tp_basicsize */ |
| 844 | 0, /* tp_itemsize */ |
| 845 | /* methods */ |
| 846 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 847 | 0, /* tp_print */ |
| 848 | 0, /* tp_getattr */ |
| 849 | 0, /* tp_setattr */ |
| 850 | &coro_as_async, /* tp_as_async */ |
| 851 | (reprfunc)coro_repr, /* tp_repr */ |
| 852 | 0, /* tp_as_number */ |
| 853 | 0, /* tp_as_sequence */ |
| 854 | 0, /* tp_as_mapping */ |
| 855 | 0, /* tp_hash */ |
| 856 | 0, /* tp_call */ |
| 857 | 0, /* tp_str */ |
| 858 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 859 | 0, /* tp_setattro */ |
| 860 | 0, /* tp_as_buffer */ |
| 861 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 862 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
| 863 | 0, /* tp_doc */ |
| 864 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 865 | 0, /* tp_clear */ |
| 866 | 0, /* tp_richcompare */ |
| 867 | offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */ |
| 868 | 0, /* tp_iter */ |
| 869 | 0, /* tp_iternext */ |
| 870 | coro_methods, /* tp_methods */ |
| 871 | coro_memberlist, /* tp_members */ |
| 872 | coro_getsetlist, /* tp_getset */ |
| 873 | 0, /* tp_base */ |
| 874 | 0, /* tp_dict */ |
| 875 | 0, /* tp_descr_get */ |
| 876 | 0, /* tp_descr_set */ |
| 877 | 0, /* tp_dictoffset */ |
| 878 | 0, /* tp_init */ |
| 879 | 0, /* tp_alloc */ |
| 880 | 0, /* tp_new */ |
| 881 | 0, /* tp_free */ |
| 882 | 0, /* tp_is_gc */ |
| 883 | 0, /* tp_bases */ |
| 884 | 0, /* tp_mro */ |
| 885 | 0, /* tp_cache */ |
| 886 | 0, /* tp_subclasses */ |
| 887 | 0, /* tp_weaklist */ |
| 888 | 0, /* tp_del */ |
| 889 | 0, /* tp_version_tag */ |
| 890 | _PyGen_Finalize, /* tp_finalize */ |
| 891 | }; |
| 892 | |
| 893 | static void |
| 894 | coro_wrapper_dealloc(PyCoroWrapper *cw) |
| 895 | { |
| 896 | _PyObject_GC_UNTRACK((PyObject *)cw); |
| 897 | Py_CLEAR(cw->cw_coroutine); |
| 898 | PyObject_GC_Del(cw); |
| 899 | } |
| 900 | |
| 901 | static PyObject * |
| 902 | coro_wrapper_iternext(PyCoroWrapper *cw) |
| 903 | { |
| 904 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0); |
| 905 | } |
| 906 | |
| 907 | static PyObject * |
| 908 | coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg) |
| 909 | { |
| 910 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0); |
| 911 | } |
| 912 | |
| 913 | static PyObject * |
| 914 | coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args) |
| 915 | { |
| 916 | return gen_throw((PyGenObject *)cw->cw_coroutine, args); |
| 917 | } |
| 918 | |
| 919 | static PyObject * |
| 920 | coro_wrapper_close(PyCoroWrapper *cw, PyObject *args) |
| 921 | { |
| 922 | return gen_close((PyGenObject *)cw->cw_coroutine, args); |
| 923 | } |
| 924 | |
| 925 | static int |
| 926 | coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg) |
| 927 | { |
| 928 | Py_VISIT((PyObject *)cw->cw_coroutine); |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static PyMethodDef coro_wrapper_methods[] = { |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 933 | {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc}, |
| 934 | {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc}, |
| 935 | {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 936 | {NULL, NULL} /* Sentinel */ |
| 937 | }; |
| 938 | |
| 939 | PyTypeObject _PyCoroWrapper_Type = { |
| 940 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 941 | "coroutine_wrapper", |
| 942 | sizeof(PyCoroWrapper), /* tp_basicsize */ |
| 943 | 0, /* tp_itemsize */ |
| 944 | (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */ |
| 945 | 0, /* tp_print */ |
| 946 | 0, /* tp_getattr */ |
| 947 | 0, /* tp_setattr */ |
| 948 | 0, /* tp_as_async */ |
| 949 | 0, /* tp_repr */ |
| 950 | 0, /* tp_as_number */ |
| 951 | 0, /* tp_as_sequence */ |
| 952 | 0, /* tp_as_mapping */ |
| 953 | 0, /* tp_hash */ |
| 954 | 0, /* tp_call */ |
| 955 | 0, /* tp_str */ |
| 956 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 957 | 0, /* tp_setattro */ |
| 958 | 0, /* tp_as_buffer */ |
| 959 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 960 | "A wrapper object implementing __await__ for coroutines.", |
| 961 | (traverseproc)coro_wrapper_traverse, /* tp_traverse */ |
| 962 | 0, /* tp_clear */ |
| 963 | 0, /* tp_richcompare */ |
| 964 | 0, /* tp_weaklistoffset */ |
| 965 | PyObject_SelfIter, /* tp_iter */ |
| 966 | (iternextfunc)coro_wrapper_iternext, /* tp_iternext */ |
| 967 | coro_wrapper_methods, /* tp_methods */ |
| 968 | 0, /* tp_members */ |
| 969 | 0, /* tp_getset */ |
| 970 | 0, /* tp_base */ |
| 971 | 0, /* tp_dict */ |
| 972 | 0, /* tp_descr_get */ |
| 973 | 0, /* tp_descr_set */ |
| 974 | 0, /* tp_dictoffset */ |
| 975 | 0, /* tp_init */ |
| 976 | 0, /* tp_alloc */ |
| 977 | 0, /* tp_new */ |
| 978 | PyObject_Del, /* tp_free */ |
| 979 | }; |
| 980 | |
| 981 | PyObject * |
| 982 | PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 983 | { |
| 984 | return gen_new_with_qualname(&PyCoro_Type, f, name, qualname); |
| 985 | } |