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); |
| 15 | return 0; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 16 | } |
| 17 | |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 18 | void |
| 19 | _PyGen_Finalize(PyObject *self) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 20 | { |
| 21 | PyGenObject *gen = (PyGenObject *)self; |
| 22 | PyObject *res; |
| 23 | PyObject *error_type, *error_value, *error_traceback; |
| 24 | |
| 25 | if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) |
| 26 | /* Generator isn't paused, so no need to close */ |
| 27 | return; |
| 28 | |
| 29 | /* Save the current exception, if any. */ |
| 30 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 31 | |
| 32 | res = gen_close(gen, NULL); |
| 33 | |
| 34 | if (res == NULL) |
| 35 | PyErr_WriteUnraisable(self); |
| 36 | else |
| 37 | Py_DECREF(res); |
| 38 | |
| 39 | /* Restore the saved exception. */ |
| 40 | PyErr_Restore(error_type, error_value, error_traceback); |
| 41 | } |
| 42 | |
| 43 | static void |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 44 | gen_dealloc(PyGenObject *gen) |
| 45 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | PyObject *self = (PyObject *) gen; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 47 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | _PyObject_GC_UNTRACK(gen); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | if (gen->gi_weakreflist != NULL) |
| 51 | PyObject_ClearWeakRefs(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 52 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 53 | _PyObject_GC_TRACK(self); |
| 54 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 55 | if (PyObject_CallFinalizerFromDealloc(self)) |
| 56 | return; /* resurrected. :( */ |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 57 | |
| 58 | _PyObject_GC_UNTRACK(self); |
| 59 | Py_CLEAR(gen->gi_frame); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | Py_CLEAR(gen->gi_code); |
| 61 | PyObject_GC_Del(gen); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 65 | gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 66 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 67 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 69 | PyObject *result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 70 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 71 | if (gen->gi_running) { |
| 72 | PyErr_SetString(PyExc_ValueError, |
| 73 | "generator already executing"); |
| 74 | return NULL; |
| 75 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 76 | if (f == NULL || f->f_stacktop == NULL) { |
| 77 | /* Only set exception if called from send() */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | if (arg && !exc) |
| 79 | PyErr_SetNone(PyExc_StopIteration); |
| 80 | return NULL; |
| 81 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 83 | if (f->f_lasti == -1) { |
| 84 | if (arg && arg != Py_None) { |
| 85 | PyErr_SetString(PyExc_TypeError, |
| 86 | "can't send non-None value to a " |
| 87 | "just-started generator"); |
| 88 | return NULL; |
| 89 | } |
| 90 | } else { |
| 91 | /* Push arg onto the frame's value stack */ |
| 92 | result = arg ? arg : Py_None; |
| 93 | Py_INCREF(result); |
| 94 | *(f->f_stacktop++) = result; |
| 95 | } |
| 96 | |
| 97 | /* Generators always return to their most recent caller, not |
| 98 | * necessarily their creator. */ |
| 99 | Py_XINCREF(tstate->frame); |
| 100 | assert(f->f_back == NULL); |
| 101 | f->f_back = tstate->frame; |
| 102 | |
| 103 | gen->gi_running = 1; |
| 104 | result = PyEval_EvalFrameEx(f, exc); |
| 105 | gen->gi_running = 0; |
| 106 | |
| 107 | /* Don't keep the reference to f_back any longer than necessary. It |
| 108 | * may keep a chain of frames alive or it could create a reference |
| 109 | * cycle. */ |
| 110 | assert(f->f_back == tstate->frame); |
| 111 | Py_CLEAR(f->f_back); |
| 112 | |
| 113 | /* If the generator just returned (as opposed to yielding), signal |
| 114 | * that the generator is exhausted. */ |
| 115 | if (result && f->f_stacktop == NULL) { |
| 116 | if (result == Py_None) { |
| 117 | /* Delay exception instantiation if we can */ |
| 118 | PyErr_SetNone(PyExc_StopIteration); |
| 119 | } else { |
| 120 | PyObject *e = PyObject_CallFunctionObjArgs( |
| 121 | PyExc_StopIteration, result, NULL); |
| 122 | if (e != NULL) { |
| 123 | PyErr_SetObject(PyExc_StopIteration, e); |
| 124 | Py_DECREF(e); |
| 125 | } |
| 126 | } |
| 127 | Py_CLEAR(result); |
| 128 | } |
| 129 | |
| 130 | if (!result || f->f_stacktop == NULL) { |
| 131 | /* generator can't be rerun, so release the frame */ |
| 132 | /* first clean reference cycle through stored exception traceback */ |
| 133 | PyObject *t, *v, *tb; |
| 134 | t = f->f_exc_type; |
| 135 | v = f->f_exc_value; |
| 136 | tb = f->f_exc_traceback; |
| 137 | f->f_exc_type = NULL; |
| 138 | f->f_exc_value = NULL; |
| 139 | f->f_exc_traceback = NULL; |
| 140 | Py_XDECREF(t); |
| 141 | Py_XDECREF(v); |
| 142 | Py_XDECREF(tb); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 143 | gen->gi_frame->f_gen = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 144 | gen->gi_frame = NULL; |
| 145 | Py_DECREF(f); |
| 146 | } |
| 147 | |
| 148 | return result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 151 | PyDoc_STRVAR(send_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 152 | "send(arg) -> send 'arg' into generator,\n\ |
| 153 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 154 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 155 | PyObject * |
| 156 | _PyGen_Send(PyGenObject *gen, PyObject *arg) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 157 | { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 158 | return gen_send_ex(gen, arg, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | PyDoc_STRVAR(close_doc, |
Benjamin Peterson | ab3da29 | 2012-05-03 18:44:09 -0400 | [diff] [blame] | 162 | "close() -> raise GeneratorExit inside generator."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 163 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 164 | /* |
| 165 | * This helper function is used by gen_close and gen_throw to |
| 166 | * close a subiterator being delegated to by yield-from. |
| 167 | */ |
| 168 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 169 | static int |
| 170 | gen_close_iter(PyObject *yf) |
| 171 | { |
| 172 | PyObject *retval = NULL; |
| 173 | _Py_IDENTIFIER(close); |
| 174 | |
| 175 | if (PyGen_CheckExact(yf)) { |
| 176 | retval = gen_close((PyGenObject *)yf, NULL); |
| 177 | if (retval == NULL) |
| 178 | return -1; |
| 179 | } else { |
| 180 | PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close); |
| 181 | if (meth == NULL) { |
| 182 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 183 | PyErr_WriteUnraisable(yf); |
| 184 | PyErr_Clear(); |
| 185 | } else { |
| 186 | retval = PyObject_CallFunction(meth, ""); |
| 187 | Py_DECREF(meth); |
| 188 | if (retval == NULL) |
| 189 | return -1; |
| 190 | } |
| 191 | } |
| 192 | Py_XDECREF(retval); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 196 | static PyObject * |
| 197 | gen_yf(PyGenObject *gen) |
| 198 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 199 | PyObject *yf = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 200 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 201 | |
| 202 | if (f && f->f_stacktop) { |
| 203 | PyObject *bytecode = f->f_code->co_code; |
| 204 | unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode); |
| 205 | |
| 206 | if (code[f->f_lasti + 1] != YIELD_FROM) |
| 207 | return NULL; |
| 208 | yf = f->f_stacktop[-1]; |
| 209 | Py_INCREF(yf); |
| 210 | } |
| 211 | |
| 212 | return yf; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 213 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 214 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 215 | static PyObject * |
| 216 | gen_close(PyGenObject *gen, PyObject *args) |
| 217 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 218 | PyObject *retval; |
| 219 | PyObject *yf = gen_yf(gen); |
| 220 | int err = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 221 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 222 | if (yf) { |
| 223 | gen->gi_running = 1; |
| 224 | err = gen_close_iter(yf); |
| 225 | gen->gi_running = 0; |
| 226 | Py_DECREF(yf); |
| 227 | } |
| 228 | if (err == 0) |
| 229 | PyErr_SetNone(PyExc_GeneratorExit); |
| 230 | retval = gen_send_ex(gen, Py_None, 1); |
| 231 | if (retval) { |
| 232 | Py_DECREF(retval); |
| 233 | PyErr_SetString(PyExc_RuntimeError, |
| 234 | "generator ignored GeneratorExit"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | return NULL; |
| 236 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 237 | if (PyErr_ExceptionMatches(PyExc_StopIteration) |
| 238 | || PyErr_ExceptionMatches(PyExc_GeneratorExit)) { |
| 239 | PyErr_Clear(); /* ignore these errors */ |
| 240 | Py_INCREF(Py_None); |
| 241 | return Py_None; |
| 242 | } |
| 243 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 246 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 247 | PyDoc_STRVAR(throw_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 248 | "throw(typ[,val[,tb]]) -> raise exception in generator,\n\ |
| 249 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 250 | |
| 251 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 252 | gen_throw(PyGenObject *gen, PyObject *args) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | PyObject *typ; |
| 255 | PyObject *tb = NULL; |
| 256 | PyObject *val = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 257 | PyObject *yf = gen_yf(gen); |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 258 | _Py_IDENTIFIER(throw); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) |
| 261 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 262 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 263 | if (yf) { |
| 264 | PyObject *ret; |
| 265 | int err; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 266 | if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 267 | gen->gi_running = 1; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 268 | err = gen_close_iter(yf); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 269 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 270 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 271 | if (err < 0) |
| 272 | return gen_send_ex(gen, Py_None, 1); |
| 273 | goto throw_here; |
| 274 | } |
| 275 | if (PyGen_CheckExact(yf)) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 276 | gen->gi_running = 1; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 277 | ret = gen_throw((PyGenObject *)yf, args); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 278 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 279 | } else { |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 280 | PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 281 | if (meth == NULL) { |
| 282 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 283 | Py_DECREF(yf); |
| 284 | return NULL; |
| 285 | } |
| 286 | PyErr_Clear(); |
| 287 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 288 | goto throw_here; |
| 289 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 290 | gen->gi_running = 1; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 291 | ret = PyObject_CallObject(meth, args); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 292 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 293 | Py_DECREF(meth); |
| 294 | } |
| 295 | Py_DECREF(yf); |
| 296 | if (!ret) { |
| 297 | PyObject *val; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 298 | /* Pop subiterator from stack */ |
| 299 | ret = *(--gen->gi_frame->f_stacktop); |
| 300 | assert(ret == yf); |
| 301 | Py_DECREF(ret); |
| 302 | /* Termination repetition of YIELD_FROM */ |
| 303 | gen->gi_frame->f_lasti++; |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 304 | if (_PyGen_FetchStopIterationValue(&val) == 0) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 305 | ret = gen_send_ex(gen, val, 0); |
| 306 | Py_DECREF(val); |
| 307 | } else { |
| 308 | ret = gen_send_ex(gen, Py_None, 1); |
| 309 | } |
| 310 | } |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | throw_here: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | /* First, check the traceback argument, replacing None with |
| 316 | NULL. */ |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 317 | if (tb == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | tb = NULL; |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 319 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 321 | PyErr_SetString(PyExc_TypeError, |
| 322 | "throw() third argument must be a traceback object"); |
| 323 | return NULL; |
| 324 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | Py_INCREF(typ); |
| 327 | Py_XINCREF(val); |
| 328 | Py_XINCREF(tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 329 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 330 | if (PyExceptionClass_Check(typ)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | PyErr_NormalizeException(&typ, &val, &tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | else if (PyExceptionInstance_Check(typ)) { |
| 334 | /* Raising an instance. The value should be a dummy. */ |
| 335 | if (val && val != Py_None) { |
| 336 | PyErr_SetString(PyExc_TypeError, |
| 337 | "instance exception may not have a separate value"); |
| 338 | goto failed_throw; |
| 339 | } |
| 340 | else { |
| 341 | /* Normalize to raise <class>, <instance> */ |
| 342 | Py_XDECREF(val); |
| 343 | val = typ; |
| 344 | typ = PyExceptionInstance_Class(typ); |
| 345 | Py_INCREF(typ); |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 346 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 347 | if (tb == NULL) |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 348 | /* Returns NULL if there's no traceback */ |
| 349 | tb = PyException_GetTraceback(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | else { |
| 353 | /* Not something you can raise. throw() fails. */ |
| 354 | PyErr_Format(PyExc_TypeError, |
| 355 | "exceptions must be classes or instances " |
| 356 | "deriving from BaseException, not %s", |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 357 | Py_TYPE(typ)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | goto failed_throw; |
| 359 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | PyErr_Restore(typ, val, tb); |
| 362 | return gen_send_ex(gen, Py_None, 1); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 363 | |
| 364 | failed_throw: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | /* Didn't use our arguments, so restore their original refcounts */ |
| 366 | Py_DECREF(typ); |
| 367 | Py_XDECREF(val); |
| 368 | Py_XDECREF(tb); |
| 369 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
| 373 | static PyObject * |
| 374 | gen_iternext(PyGenObject *gen) |
| 375 | { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 376 | PyObject *val = NULL; |
| 377 | PyObject *ret; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 378 | ret = gen_send_ex(gen, val, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 379 | Py_XDECREF(val); |
| 380 | return ret; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 383 | /* |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 384 | * If StopIteration exception is set, fetches its 'value' |
| 385 | * attribute if any, otherwise sets pvalue to None. |
| 386 | * |
| 387 | * Returns 0 if no exception or StopIteration is set. |
| 388 | * If any other exception is set, returns -1 and leaves |
| 389 | * pvalue unchanged. |
| 390 | */ |
| 391 | |
| 392 | int |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 393 | _PyGen_FetchStopIterationValue(PyObject **pvalue) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 394 | PyObject *et, *ev, *tb; |
| 395 | PyObject *value = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 396 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 397 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 398 | PyErr_Fetch(&et, &ev, &tb); |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame^] | 399 | if (ev) { |
| 400 | /* exception will usually be normalised already */ |
| 401 | if (Py_TYPE(ev) == (PyTypeObject *) et |
| 402 | || PyObject_IsInstance(ev, PyExc_StopIteration)) { |
| 403 | value = ((PyStopIterationObject *)ev)->value; |
| 404 | Py_INCREF(value); |
| 405 | Py_DECREF(ev); |
| 406 | } else if (et == PyExc_StopIteration) { |
| 407 | /* avoid normalisation and take ev as value */ |
| 408 | value = ev; |
| 409 | } else { |
| 410 | /* normalisation required */ |
| 411 | PyErr_NormalizeException(&et, &ev, &tb); |
| 412 | if (!PyObject_IsInstance(ev, PyExc_StopIteration)) { |
| 413 | PyErr_Restore(et, ev, tb); |
| 414 | return -1; |
| 415 | } |
| 416 | value = ((PyStopIterationObject *)ev)->value; |
| 417 | Py_INCREF(value); |
| 418 | Py_DECREF(ev); |
| 419 | } |
| 420 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 421 | Py_XDECREF(et); |
| 422 | Py_XDECREF(tb); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 423 | } else if (PyErr_Occurred()) { |
| 424 | return -1; |
| 425 | } |
| 426 | if (value == NULL) { |
| 427 | value = Py_None; |
Amaury Forgeot d'Arc | e557da8 | 2012-01-13 21:06:12 +0100 | [diff] [blame] | 428 | Py_INCREF(value); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 429 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 430 | *pvalue = value; |
| 431 | return 0; |
| 432 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 433 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 434 | static PyObject * |
| 435 | gen_repr(PyGenObject *gen) |
| 436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | return PyUnicode_FromFormat("<generator object %S at %p>", |
| 438 | ((PyCodeObject *)gen->gi_code)->co_name, |
| 439 | gen); |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
| 443 | static PyObject * |
| 444 | gen_get_name(PyGenObject *gen) |
| 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 447 | Py_INCREF(name); |
| 448 | return name; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
| 452 | PyDoc_STRVAR(gen__name__doc__, |
| 453 | "Return the name of the generator's associated code object."); |
| 454 | |
| 455 | static PyGetSetDef gen_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | {"__name__", (getter)gen_get_name, NULL, gen__name__doc__}, |
| 457 | {NULL} |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 461 | static PyMemberDef gen_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
Benjamin Peterson | 657e9eb | 2012-03-07 18:17:03 -0600 | [diff] [blame] | 463 | {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
| 465 | {NULL} /* Sentinel */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 468 | static PyMethodDef gen_methods[] = { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 469 | {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 471 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 472 | {NULL, NULL} /* Sentinel */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 475 | PyTypeObject PyGen_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 477 | "generator", /* tp_name */ |
| 478 | sizeof(PyGenObject), /* tp_basicsize */ |
| 479 | 0, /* tp_itemsize */ |
| 480 | /* methods */ |
| 481 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 482 | 0, /* tp_print */ |
| 483 | 0, /* tp_getattr */ |
| 484 | 0, /* tp_setattr */ |
| 485 | 0, /* tp_reserved */ |
| 486 | (reprfunc)gen_repr, /* tp_repr */ |
| 487 | 0, /* tp_as_number */ |
| 488 | 0, /* tp_as_sequence */ |
| 489 | 0, /* tp_as_mapping */ |
| 490 | 0, /* tp_hash */ |
| 491 | 0, /* tp_call */ |
| 492 | 0, /* tp_str */ |
| 493 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 494 | 0, /* tp_setattro */ |
| 495 | 0, /* tp_as_buffer */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 496 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 497 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | 0, /* tp_doc */ |
| 499 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 500 | 0, /* tp_clear */ |
| 501 | 0, /* tp_richcompare */ |
| 502 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
| 503 | PyObject_SelfIter, /* tp_iter */ |
| 504 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 505 | gen_methods, /* tp_methods */ |
| 506 | gen_memberlist, /* tp_members */ |
| 507 | gen_getsetlist, /* tp_getset */ |
| 508 | 0, /* tp_base */ |
| 509 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | 0, /* tp_descr_get */ |
| 512 | 0, /* tp_descr_set */ |
| 513 | 0, /* tp_dictoffset */ |
| 514 | 0, /* tp_init */ |
| 515 | 0, /* tp_alloc */ |
| 516 | 0, /* tp_new */ |
| 517 | 0, /* tp_free */ |
| 518 | 0, /* tp_is_gc */ |
| 519 | 0, /* tp_bases */ |
| 520 | 0, /* tp_mro */ |
| 521 | 0, /* tp_cache */ |
| 522 | 0, /* tp_subclasses */ |
| 523 | 0, /* tp_weaklist */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 524 | 0, /* tp_del */ |
| 525 | 0, /* tp_version_tag */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 526 | _PyGen_Finalize, /* tp_finalize */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 527 | }; |
| 528 | |
| 529 | PyObject * |
| 530 | PyGen_New(PyFrameObject *f) |
| 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type); |
| 533 | if (gen == NULL) { |
| 534 | Py_DECREF(f); |
| 535 | return NULL; |
| 536 | } |
| 537 | gen->gi_frame = f; |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 538 | f->f_gen = (PyObject *) gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | Py_INCREF(f->f_code); |
| 540 | gen->gi_code = (PyObject *)(f->f_code); |
| 541 | gen->gi_running = 0; |
| 542 | gen->gi_weakreflist = NULL; |
| 543 | _PyObject_GC_TRACK(gen); |
| 544 | return (PyObject *)gen; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 545 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 546 | |
| 547 | int |
| 548 | PyGen_NeedsFinalizing(PyGenObject *gen) |
| 549 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 550 | int i; |
| 551 | PyFrameObject *f = gen->gi_frame; |
| 552 | |
| 553 | if (f == NULL || f->f_stacktop == NULL) |
| 554 | return 0; /* no frame or empty blockstack == no finalization */ |
| 555 | |
| 556 | /* Any block type besides a loop requires cleanup. */ |
| 557 | for (i = 0; i < f->f_iblock; i++) |
| 558 | if (f->f_blockstack[i].b_type != SETUP_LOOP) |
| 559 | return 1; |
| 560 | |
| 561 | /* No blocks except loops, it's safe to skip finalization. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 563 | } |