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