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" |
| 5 | #include "genobject.h" |
| 6 | #include "ceval.h" |
| 7 | #include "structmember.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8 | #include "opcode.h" |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 9 | |
| 10 | static int |
| 11 | gen_traverse(PyGenObject *gen, visitproc visit, void *arg) |
| 12 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13 | Py_VISIT((PyObject *)gen->gi_frame); |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 14 | Py_VISIT(gen->gi_code); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15 | return 0; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | static void |
| 19 | gen_dealloc(PyGenObject *gen) |
| 20 | { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 21 | PyObject *self = (PyObject *) gen; |
| 22 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 23 | _PyObject_GC_UNTRACK(gen); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 24 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 25 | if (gen->gi_weakreflist != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 26 | PyObject_ClearWeakRefs(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 27 | |
| 28 | _PyObject_GC_TRACK(self); |
| 29 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 30 | if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 31 | /* Generator is paused, so we need to close */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 32 | Py_TYPE(gen)->tp_del(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 33 | if (self->ob_refcnt > 0) |
| 34 | return; /* resurrected. :( */ |
| 35 | } |
| 36 | |
| 37 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | Py_CLEAR(gen->gi_frame); |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 39 | Py_CLEAR(gen->gi_code); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 40 | PyObject_GC_Del(gen); |
| 41 | } |
| 42 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 44 | static PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 45 | gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 46 | { |
| 47 | PyThreadState *tstate = PyThreadState_GET(); |
| 48 | PyFrameObject *f = gen->gi_frame; |
| 49 | PyObject *result; |
| 50 | |
| 51 | if (gen->gi_running) { |
| 52 | PyErr_SetString(PyExc_ValueError, |
| 53 | "generator already executing"); |
| 54 | return NULL; |
| 55 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 56 | if (f==NULL || f->f_stacktop == NULL) { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 57 | /* Only set exception if called from send() */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | if (arg && !exc) |
| 59 | PyErr_SetNone(PyExc_StopIteration); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 60 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (f->f_lasti == -1) { |
| 64 | if (arg && arg != Py_None) { |
| 65 | PyErr_SetString(PyExc_TypeError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | "can't send non-None value to a " |
| 67 | "just-started generator"); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | } else { |
| 71 | /* Push arg onto the frame's value stack */ |
| 72 | result = arg ? arg : Py_None; |
| 73 | Py_INCREF(result); |
| 74 | *(f->f_stacktop++) = result; |
| 75 | } |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 76 | |
| 77 | /* Generators always return to their most recent caller, not |
| 78 | * necessarily their creator. */ |
| 79 | Py_XINCREF(tstate->frame); |
| 80 | assert(f->f_back == NULL); |
| 81 | f->f_back = tstate->frame; |
| 82 | |
| 83 | gen->gi_running = 1; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 84 | result = PyEval_EvalFrameEx(f, exc); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 85 | gen->gi_running = 0; |
| 86 | |
| 87 | /* Don't keep the reference to f_back any longer than necessary. It |
| 88 | * may keep a chain of frames alive or it could create a reference |
| 89 | * cycle. */ |
Phillip J. Eby | 0014822 | 2005-08-13 03:29:00 +0000 | [diff] [blame] | 90 | assert(f->f_back == tstate->frame); |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 91 | Py_CLEAR(f->f_back); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 92 | |
| 93 | /* If the generator just returned (as opposed to yielding), signal |
| 94 | * that the generator is exhausted. */ |
| 95 | if (result == Py_None && f->f_stacktop == NULL) { |
| 96 | Py_DECREF(result); |
| 97 | result = NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 98 | /* Set exception if not called by gen_iternext() */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | if (arg) |
| 100 | PyErr_SetNone(PyExc_StopIteration); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (!result || f->f_stacktop == NULL) { |
| 104 | /* generator can't be rerun, so release the frame */ |
| 105 | Py_DECREF(f); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 106 | gen->gi_frame = NULL; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return result; |
| 110 | } |
| 111 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 112 | PyDoc_STRVAR(send_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | "send(arg) -> send 'arg' into generator,\n\ |
| 114 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 115 | |
| 116 | static PyObject * |
| 117 | gen_send(PyGenObject *gen, PyObject *arg) |
| 118 | { |
| 119 | return gen_send_ex(gen, arg, 0); |
| 120 | } |
| 121 | |
| 122 | PyDoc_STRVAR(close_doc, |
| 123 | "close(arg) -> raise GeneratorExit inside generator."); |
| 124 | |
| 125 | static PyObject * |
| 126 | gen_close(PyGenObject *gen, PyObject *args) |
| 127 | { |
| 128 | PyObject *retval; |
| 129 | PyErr_SetNone(PyExc_GeneratorExit); |
| 130 | retval = gen_send_ex(gen, Py_None, 1); |
| 131 | if (retval) { |
| 132 | Py_DECREF(retval); |
| 133 | PyErr_SetString(PyExc_RuntimeError, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 134 | "generator ignored GeneratorExit"); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 137 | if (PyErr_ExceptionMatches(PyExc_StopIteration) |
| 138 | || PyErr_ExceptionMatches(PyExc_GeneratorExit)) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 139 | { |
| 140 | PyErr_Clear(); /* ignore these errors */ |
| 141 | Py_INCREF(Py_None); |
| 142 | return Py_None; |
| 143 | } |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | gen_del(PyObject *self) |
| 149 | { |
| 150 | PyObject *res; |
| 151 | PyObject *error_type, *error_value, *error_traceback; |
| 152 | PyGenObject *gen = (PyGenObject *)self; |
| 153 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 154 | if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 155 | /* Generator isn't paused, so no need to close */ |
| 156 | return; |
| 157 | |
| 158 | /* Temporarily resurrect the object. */ |
| 159 | assert(self->ob_refcnt == 0); |
| 160 | self->ob_refcnt = 1; |
| 161 | |
| 162 | /* Save the current exception, if any. */ |
| 163 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 164 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 165 | res = gen_close(gen, NULL); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 166 | |
| 167 | if (res == NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | PyErr_WriteUnraisable(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 169 | else |
| 170 | Py_DECREF(res); |
| 171 | |
| 172 | /* Restore the saved exception. */ |
| 173 | PyErr_Restore(error_type, error_value, error_traceback); |
| 174 | |
| 175 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 176 | * cause a recursive call. |
| 177 | */ |
| 178 | assert(self->ob_refcnt > 0); |
| 179 | if (--self->ob_refcnt == 0) |
| 180 | return; /* this is the normal path out */ |
| 181 | |
| 182 | /* close() resurrected it! Make it look like the original Py_DECREF |
| 183 | * never happened. |
| 184 | */ |
| 185 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 186 | Py_ssize_t refcnt = self->ob_refcnt; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 187 | _Py_NewReference(self); |
| 188 | self->ob_refcnt = refcnt; |
| 189 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 190 | assert(PyType_IS_GC(self->ob_type) && |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 191 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 192 | |
| 193 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 194 | * we need to undo that. */ |
| 195 | _Py_DEC_REFTOTAL; |
| 196 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 197 | * chain, so no more to do there. |
| 198 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 199 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 200 | * undone. |
| 201 | */ |
| 202 | #ifdef COUNT_ALLOCS |
| 203 | --self->ob_type->tp_frees; |
| 204 | --self->ob_type->tp_allocs; |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | PyDoc_STRVAR(throw_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 211 | "throw(typ[,val[,tb]]) -> raise exception in generator,\n\ |
| 212 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 213 | |
| 214 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 215 | gen_throw(PyGenObject *gen, PyObject *args) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 216 | { |
| 217 | PyObject *typ; |
| 218 | PyObject *tb = NULL; |
| 219 | PyObject *val = NULL; |
| 220 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 221 | if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 222 | return NULL; |
| 223 | |
Armin Rigo | 967aa8b | 2006-02-14 15:50:44 +0000 | [diff] [blame] | 224 | /* First, check the traceback argument, replacing None with |
| 225 | NULL. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 226 | if (tb == Py_None) |
Armin Rigo | 967aa8b | 2006-02-14 15:50:44 +0000 | [diff] [blame] | 227 | tb = NULL; |
Armin Rigo | 967aa8b | 2006-02-14 15:50:44 +0000 | [diff] [blame] | 228 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 229 | PyErr_SetString(PyExc_TypeError, |
| 230 | "throw() third argument must be a traceback object"); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | Py_INCREF(typ); |
| 235 | Py_XINCREF(val); |
| 236 | Py_XINCREF(tb); |
| 237 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 238 | if (PyExceptionClass_Check(typ)) { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 239 | PyErr_NormalizeException(&typ, &val, &tb); |
| 240 | } |
| 241 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 242 | else if (PyExceptionInstance_Check(typ)) { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 243 | /* Raising an instance. The value should be a dummy. */ |
| 244 | if (val && val != Py_None) { |
| 245 | PyErr_SetString(PyExc_TypeError, |
| 246 | "instance exception may not have a separate value"); |
| 247 | goto failed_throw; |
| 248 | } |
| 249 | else { |
| 250 | /* Normalize to raise <class>, <instance> */ |
Armin Rigo | 967aa8b | 2006-02-14 15:50:44 +0000 | [diff] [blame] | 251 | Py_XDECREF(val); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 252 | val = typ; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 253 | typ = PyExceptionInstance_Class(typ); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 254 | Py_INCREF(typ); |
| 255 | } |
| 256 | } |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 257 | else { |
Armin Rigo | 967aa8b | 2006-02-14 15:50:44 +0000 | [diff] [blame] | 258 | /* Not something you can raise. throw() fails. */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 259 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 260 | "exceptions must be classes or instances " |
| 261 | "deriving from BaseException, not %s", |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 262 | typ->ob_type->tp_name); |
| 263 | goto failed_throw; |
| 264 | } |
| 265 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 266 | PyErr_Restore(typ, val, tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 267 | return gen_send_ex(gen, Py_None, 1); |
| 268 | |
| 269 | failed_throw: |
| 270 | /* Didn't use our arguments, so restore their original refcounts */ |
| 271 | Py_DECREF(typ); |
| 272 | Py_XDECREF(val); |
| 273 | Py_XDECREF(tb); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | static PyObject * |
| 279 | gen_iternext(PyGenObject *gen) |
| 280 | { |
| 281 | return gen_send_ex(gen, NULL, 0); |
| 282 | } |
| 283 | |
| 284 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 285 | static PyObject * |
| 286 | gen_repr(PyGenObject *gen) |
| 287 | { |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 288 | return PyUnicode_FromFormat("<generator object %S at %p>", |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 289 | ((PyCodeObject *)gen->gi_code)->co_name, |
| 290 | gen); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | static PyObject * |
| 295 | gen_get_name(PyGenObject *gen) |
| 296 | { |
| 297 | PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 298 | Py_INCREF(name); |
| 299 | return name; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | PyDoc_STRVAR(gen__name__doc__, |
| 304 | "Return the name of the generator's associated code object."); |
| 305 | |
| 306 | static PyGetSetDef gen_getsetlist[] = { |
| 307 | {"__name__", (getter)gen_get_name, NULL, NULL, gen__name__doc__}, |
| 308 | {NULL} |
| 309 | }; |
| 310 | |
| 311 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 312 | static PyMemberDef gen_memberlist[] = { |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 313 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
| 314 | {"gi_running", T_INT, offsetof(PyGenObject, gi_running), READONLY}, |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 315 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 316 | {NULL} /* Sentinel */ |
| 317 | }; |
| 318 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 319 | static PyMethodDef gen_methods[] = { |
| 320 | {"send",(PyCFunction)gen_send, METH_O, send_doc}, |
| 321 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 322 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 323 | {NULL, NULL} /* Sentinel */ |
| 324 | }; |
| 325 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 326 | PyTypeObject PyGen_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 327 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 328 | "generator", /* tp_name */ |
| 329 | sizeof(PyGenObject), /* tp_basicsize */ |
| 330 | 0, /* tp_itemsize */ |
| 331 | /* methods */ |
| 332 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 333 | 0, /* tp_print */ |
| 334 | 0, /* tp_getattr */ |
| 335 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame^] | 336 | 0, /* tp_reserved */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 337 | (reprfunc)gen_repr, /* tp_repr */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 338 | 0, /* tp_as_number */ |
| 339 | 0, /* tp_as_sequence */ |
| 340 | 0, /* tp_as_mapping */ |
| 341 | 0, /* tp_hash */ |
| 342 | 0, /* tp_call */ |
| 343 | 0, /* tp_str */ |
| 344 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 345 | 0, /* tp_setattro */ |
| 346 | 0, /* tp_as_buffer */ |
| 347 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 348 | 0, /* tp_doc */ |
| 349 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 350 | 0, /* tp_clear */ |
| 351 | 0, /* tp_richcompare */ |
| 352 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 6c7a00f | 2004-06-12 05:17:55 +0000 | [diff] [blame] | 353 | PyObject_SelfIter, /* tp_iter */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 354 | (iternextfunc)gen_iternext, /* tp_iternext */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 355 | gen_methods, /* tp_methods */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 356 | gen_memberlist, /* tp_members */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 357 | gen_getsetlist, /* tp_getset */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 358 | 0, /* tp_base */ |
| 359 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 361 | 0, /* tp_descr_get */ |
| 362 | 0, /* tp_descr_set */ |
| 363 | 0, /* tp_dictoffset */ |
| 364 | 0, /* tp_init */ |
| 365 | 0, /* tp_alloc */ |
| 366 | 0, /* tp_new */ |
| 367 | 0, /* tp_free */ |
| 368 | 0, /* tp_is_gc */ |
| 369 | 0, /* tp_bases */ |
| 370 | 0, /* tp_mro */ |
| 371 | 0, /* tp_cache */ |
| 372 | 0, /* tp_subclasses */ |
| 373 | 0, /* tp_weaklist */ |
| 374 | gen_del, /* tp_del */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | PyObject * |
| 378 | PyGen_New(PyFrameObject *f) |
| 379 | { |
| 380 | PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type); |
| 381 | if (gen == NULL) { |
| 382 | Py_DECREF(f); |
| 383 | return NULL; |
| 384 | } |
| 385 | gen->gi_frame = f; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 386 | Py_INCREF(f->f_code); |
| 387 | gen->gi_code = (PyObject *)(f->f_code); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 388 | gen->gi_running = 0; |
| 389 | gen->gi_weakreflist = NULL; |
| 390 | _PyObject_GC_TRACK(gen); |
| 391 | return (PyObject *)gen; |
| 392 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 393 | |
| 394 | int |
| 395 | PyGen_NeedsFinalizing(PyGenObject *gen) |
| 396 | { |
| 397 | int i; |
| 398 | PyFrameObject *f = gen->gi_frame; |
| 399 | |
| 400 | if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0) |
| 401 | return 0; /* no frame or empty blockstack == no finalization */ |
| 402 | |
| 403 | /* Any block type besides a loop requires cleanup. */ |
| 404 | i = f->f_iblock; |
| 405 | while (--i >= 0) { |
| 406 | if (f->f_blockstack[i].b_type != SETUP_LOOP) |
| 407 | return 1; |
| 408 | } |
| 409 | |
| 410 | /* No blocks except loops, it's safe to skip finalization. */ |
| 411 | return 0; |
| 412 | } |