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