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