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