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