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); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 15 | Py_VISIT(gen->gi_name); |
| 16 | Py_VISIT(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | return 0; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 20 | void |
| 21 | _PyGen_Finalize(PyObject *self) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 22 | { |
| 23 | PyGenObject *gen = (PyGenObject *)self; |
| 24 | PyObject *res; |
| 25 | PyObject *error_type, *error_value, *error_traceback; |
| 26 | |
| 27 | if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) |
| 28 | /* Generator isn't paused, so no need to close */ |
| 29 | return; |
| 30 | |
| 31 | /* Save the current exception, if any. */ |
| 32 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 33 | |
Benjamin Peterson | 2f40ed4 | 2016-09-05 10:14:54 -0700 | [diff] [blame] | 34 | /* If `gen` is a coroutine, and if it was never awaited on, |
| 35 | issue a RuntimeWarning. */ |
| 36 | if (gen->gi_code != NULL |
| 37 | && ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE |
| 38 | && gen->gi_frame->f_lasti == -1 |
| 39 | && !PyErr_Occurred() |
| 40 | && PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 41 | "coroutine '%.50S' was never awaited", |
| 42 | gen->gi_qualname)) { |
| 43 | res = NULL; /* oops, exception */ |
| 44 | } |
| 45 | else { |
| 46 | res = gen_close(gen, NULL); |
| 47 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 48 | |
| 49 | if (res == NULL) |
| 50 | PyErr_WriteUnraisable(self); |
| 51 | else |
| 52 | Py_DECREF(res); |
| 53 | |
| 54 | /* Restore the saved exception. */ |
| 55 | PyErr_Restore(error_type, error_value, error_traceback); |
| 56 | } |
| 57 | |
| 58 | static void |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 59 | gen_dealloc(PyGenObject *gen) |
| 60 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | PyObject *self = (PyObject *) gen; |
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 | _PyObject_GC_UNTRACK(gen); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (gen->gi_weakreflist != NULL) |
| 66 | PyObject_ClearWeakRefs(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 68 | _PyObject_GC_TRACK(self); |
| 69 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 70 | if (PyObject_CallFinalizerFromDealloc(self)) |
| 71 | return; /* resurrected. :( */ |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 72 | |
| 73 | _PyObject_GC_UNTRACK(self); |
Benjamin Peterson | bdddb11 | 2016-09-05 10:39:57 -0700 | [diff] [blame^] | 74 | if (gen->gi_frame != NULL) { |
| 75 | gen->gi_frame->f_gen = NULL; |
| 76 | Py_CLEAR(gen->gi_frame); |
| 77 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | Py_CLEAR(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 79 | Py_CLEAR(gen->gi_name); |
| 80 | Py_CLEAR(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | PyObject_GC_Del(gen); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static PyObject * |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 85 | gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 87 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 89 | PyObject *result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 91 | if (gen->gi_running) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 92 | char *msg = "generator already executing"; |
| 93 | if (PyCoro_CheckExact(gen)) |
| 94 | msg = "coroutine already executing"; |
| 95 | PyErr_SetString(PyExc_ValueError, msg); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 98 | if (f == NULL || f->f_stacktop == NULL) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 99 | if (PyCoro_CheckExact(gen) && !closing) { |
| 100 | /* `gen` is an exhausted coroutine: raise an error, |
| 101 | except when called from gen_close(), which should |
| 102 | always be a silent method. */ |
| 103 | PyErr_SetString( |
| 104 | PyExc_RuntimeError, |
| 105 | "cannot reuse already awaited coroutine"); |
| 106 | } else if (arg && !exc) { |
| 107 | /* `gen` is an exhausted generator: |
| 108 | only set exception if called from send(). */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | PyErr_SetNone(PyExc_StopIteration); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 110 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 114 | if (f->f_lasti == -1) { |
| 115 | if (arg && arg != Py_None) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 116 | char *msg = "can't send non-None value to a " |
| 117 | "just-started generator"; |
| 118 | if (PyCoro_CheckExact(gen)) |
| 119 | msg = "can't send non-None value to a " |
| 120 | "just-started coroutine"; |
| 121 | PyErr_SetString(PyExc_TypeError, msg); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 122 | return NULL; |
| 123 | } |
| 124 | } else { |
| 125 | /* Push arg onto the frame's value stack */ |
| 126 | result = arg ? arg : Py_None; |
| 127 | Py_INCREF(result); |
| 128 | *(f->f_stacktop++) = result; |
| 129 | } |
| 130 | |
| 131 | /* Generators always return to their most recent caller, not |
| 132 | * necessarily their creator. */ |
| 133 | Py_XINCREF(tstate->frame); |
| 134 | assert(f->f_back == NULL); |
| 135 | f->f_back = tstate->frame; |
| 136 | |
| 137 | gen->gi_running = 1; |
| 138 | result = PyEval_EvalFrameEx(f, exc); |
| 139 | gen->gi_running = 0; |
| 140 | |
| 141 | /* Don't keep the reference to f_back any longer than necessary. It |
| 142 | * may keep a chain of frames alive or it could create a reference |
| 143 | * cycle. */ |
| 144 | assert(f->f_back == tstate->frame); |
| 145 | Py_CLEAR(f->f_back); |
| 146 | |
| 147 | /* If the generator just returned (as opposed to yielding), signal |
| 148 | * that the generator is exhausted. */ |
| 149 | if (result && f->f_stacktop == NULL) { |
| 150 | if (result == Py_None) { |
| 151 | /* Delay exception instantiation if we can */ |
| 152 | PyErr_SetNone(PyExc_StopIteration); |
| 153 | } else { |
| 154 | PyObject *e = PyObject_CallFunctionObjArgs( |
| 155 | PyExc_StopIteration, result, NULL); |
| 156 | if (e != NULL) { |
| 157 | PyErr_SetObject(PyExc_StopIteration, e); |
| 158 | Py_DECREF(e); |
| 159 | } |
| 160 | } |
| 161 | Py_CLEAR(result); |
| 162 | } |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 163 | else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) { |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 164 | /* Check for __future__ generator_stop and conditionally turn |
| 165 | * a leaking StopIteration into RuntimeError (with its cause |
| 166 | * set appropriately). */ |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 167 | if (((PyCodeObject *)gen->gi_code)->co_flags & |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 168 | (CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE)) |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 169 | { |
| 170 | PyObject *exc, *val, *val2, *tb; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 171 | char *msg = "generator raised StopIteration"; |
| 172 | if (PyCoro_CheckExact(gen)) |
| 173 | msg = "coroutine raised StopIteration"; |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 174 | PyErr_Fetch(&exc, &val, &tb); |
| 175 | PyErr_NormalizeException(&exc, &val, &tb); |
| 176 | if (tb != NULL) |
| 177 | PyException_SetTraceback(val, tb); |
| 178 | Py_DECREF(exc); |
| 179 | Py_XDECREF(tb); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 180 | PyErr_SetString(PyExc_RuntimeError, msg); |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 181 | PyErr_Fetch(&exc, &val2, &tb); |
| 182 | PyErr_NormalizeException(&exc, &val2, &tb); |
Yury Selivanov | 18c30a2 | 2015-05-10 15:09:46 -0400 | [diff] [blame] | 183 | Py_INCREF(val); |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 184 | PyException_SetCause(val2, val); |
| 185 | PyException_SetContext(val2, val); |
| 186 | PyErr_Restore(exc, val2, tb); |
| 187 | } |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 188 | else { |
| 189 | PyObject *exc, *val, *tb; |
| 190 | |
| 191 | /* Pop the exception before issuing a warning. */ |
| 192 | PyErr_Fetch(&exc, &val, &tb); |
| 193 | |
| 194 | if (PyErr_WarnFormat(PyExc_PendingDeprecationWarning, 1, |
| 195 | "generator '%.50S' raised StopIteration", |
| 196 | gen->gi_qualname)) { |
| 197 | /* Warning was converted to an error. */ |
| 198 | Py_XDECREF(exc); |
| 199 | Py_XDECREF(val); |
| 200 | Py_XDECREF(tb); |
| 201 | } |
| 202 | else { |
| 203 | PyErr_Restore(exc, val, tb); |
| 204 | } |
| 205 | } |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 206 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 207 | |
| 208 | if (!result || f->f_stacktop == NULL) { |
| 209 | /* generator can't be rerun, so release the frame */ |
| 210 | /* first clean reference cycle through stored exception traceback */ |
| 211 | PyObject *t, *v, *tb; |
| 212 | t = f->f_exc_type; |
| 213 | v = f->f_exc_value; |
| 214 | tb = f->f_exc_traceback; |
| 215 | f->f_exc_type = NULL; |
| 216 | f->f_exc_value = NULL; |
| 217 | f->f_exc_traceback = NULL; |
| 218 | Py_XDECREF(t); |
| 219 | Py_XDECREF(v); |
| 220 | Py_XDECREF(tb); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 221 | gen->gi_frame->f_gen = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 222 | gen->gi_frame = NULL; |
| 223 | Py_DECREF(f); |
| 224 | } |
| 225 | |
| 226 | return result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 229 | PyDoc_STRVAR(send_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 230 | "send(arg) -> send 'arg' into generator,\n\ |
| 231 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 232 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 233 | PyObject * |
| 234 | _PyGen_Send(PyGenObject *gen, PyObject *arg) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 235 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 236 | return gen_send_ex(gen, arg, 0, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | PyDoc_STRVAR(close_doc, |
Benjamin Peterson | ab3da29 | 2012-05-03 18:44:09 -0400 | [diff] [blame] | 240 | "close() -> raise GeneratorExit inside generator."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 241 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 242 | /* |
| 243 | * This helper function is used by gen_close and gen_throw to |
| 244 | * close a subiterator being delegated to by yield-from. |
| 245 | */ |
| 246 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 247 | static int |
| 248 | gen_close_iter(PyObject *yf) |
| 249 | { |
| 250 | PyObject *retval = NULL; |
| 251 | _Py_IDENTIFIER(close); |
| 252 | |
| 253 | if (PyGen_CheckExact(yf)) { |
| 254 | retval = gen_close((PyGenObject *)yf, NULL); |
| 255 | if (retval == NULL) |
| 256 | return -1; |
| 257 | } else { |
| 258 | PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close); |
| 259 | if (meth == NULL) { |
| 260 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 261 | PyErr_WriteUnraisable(yf); |
| 262 | PyErr_Clear(); |
| 263 | } else { |
| 264 | retval = PyObject_CallFunction(meth, ""); |
| 265 | Py_DECREF(meth); |
| 266 | if (retval == NULL) |
| 267 | return -1; |
| 268 | } |
| 269 | } |
| 270 | Py_XDECREF(retval); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 274 | PyObject * |
| 275 | _PyGen_yf(PyGenObject *gen) |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 276 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 277 | PyObject *yf = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 278 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 279 | |
| 280 | if (f && f->f_stacktop) { |
| 281 | PyObject *bytecode = f->f_code->co_code; |
| 282 | unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode); |
| 283 | |
| 284 | if (code[f->f_lasti + 1] != YIELD_FROM) |
| 285 | return NULL; |
| 286 | yf = f->f_stacktop[-1]; |
| 287 | Py_INCREF(yf); |
| 288 | } |
| 289 | |
| 290 | return yf; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 291 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 292 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 293 | static PyObject * |
| 294 | gen_close(PyGenObject *gen, PyObject *args) |
| 295 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 296 | PyObject *retval; |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 297 | PyObject *yf = _PyGen_yf(gen); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 298 | int err = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 299 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 300 | if (yf) { |
| 301 | gen->gi_running = 1; |
| 302 | err = gen_close_iter(yf); |
| 303 | gen->gi_running = 0; |
| 304 | Py_DECREF(yf); |
| 305 | } |
| 306 | if (err == 0) |
| 307 | PyErr_SetNone(PyExc_GeneratorExit); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 308 | retval = gen_send_ex(gen, Py_None, 1, 1); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 309 | if (retval) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 310 | char *msg = "generator ignored GeneratorExit"; |
| 311 | if (PyCoro_CheckExact(gen)) |
| 312 | msg = "coroutine ignored GeneratorExit"; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 313 | Py_DECREF(retval); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 314 | PyErr_SetString(PyExc_RuntimeError, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | return NULL; |
| 316 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 317 | if (PyErr_ExceptionMatches(PyExc_StopIteration) |
| 318 | || PyErr_ExceptionMatches(PyExc_GeneratorExit)) { |
| 319 | PyErr_Clear(); /* ignore these errors */ |
| 320 | Py_INCREF(Py_None); |
| 321 | return Py_None; |
| 322 | } |
| 323 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 326 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 327 | PyDoc_STRVAR(throw_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 328 | "throw(typ[,val[,tb]]) -> raise exception in generator,\n\ |
| 329 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 330 | |
| 331 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 332 | gen_throw(PyGenObject *gen, PyObject *args) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | PyObject *typ; |
| 335 | PyObject *tb = NULL; |
| 336 | PyObject *val = NULL; |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 337 | PyObject *yf = _PyGen_yf(gen); |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 338 | _Py_IDENTIFIER(throw); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) |
| 341 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 342 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 343 | if (yf) { |
| 344 | PyObject *ret; |
| 345 | int err; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 346 | if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 347 | gen->gi_running = 1; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 348 | err = gen_close_iter(yf); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 349 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 350 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 351 | if (err < 0) |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 352 | return gen_send_ex(gen, Py_None, 1, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 353 | goto throw_here; |
| 354 | } |
| 355 | if (PyGen_CheckExact(yf)) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 356 | gen->gi_running = 1; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 357 | ret = gen_throw((PyGenObject *)yf, args); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 358 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 359 | } else { |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 360 | PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 361 | if (meth == NULL) { |
| 362 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 363 | Py_DECREF(yf); |
| 364 | return NULL; |
| 365 | } |
| 366 | PyErr_Clear(); |
| 367 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 368 | goto throw_here; |
| 369 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 370 | gen->gi_running = 1; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 371 | ret = PyObject_CallObject(meth, args); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 372 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 373 | Py_DECREF(meth); |
| 374 | } |
| 375 | Py_DECREF(yf); |
| 376 | if (!ret) { |
| 377 | PyObject *val; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 378 | /* Pop subiterator from stack */ |
| 379 | ret = *(--gen->gi_frame->f_stacktop); |
| 380 | assert(ret == yf); |
| 381 | Py_DECREF(ret); |
| 382 | /* Termination repetition of YIELD_FROM */ |
| 383 | gen->gi_frame->f_lasti++; |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 384 | if (_PyGen_FetchStopIterationValue(&val) == 0) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 385 | ret = gen_send_ex(gen, val, 0, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 386 | Py_DECREF(val); |
| 387 | } else { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 388 | ret = gen_send_ex(gen, Py_None, 1, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | throw_here: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | /* First, check the traceback argument, replacing None with |
| 396 | NULL. */ |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 397 | if (tb == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | tb = NULL; |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 399 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 401 | PyErr_SetString(PyExc_TypeError, |
| 402 | "throw() third argument must be a traceback object"); |
| 403 | return NULL; |
| 404 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | Py_INCREF(typ); |
| 407 | Py_XINCREF(val); |
| 408 | Py_XINCREF(tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 409 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 410 | if (PyExceptionClass_Check(typ)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | PyErr_NormalizeException(&typ, &val, &tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | else if (PyExceptionInstance_Check(typ)) { |
| 414 | /* Raising an instance. The value should be a dummy. */ |
| 415 | if (val && val != Py_None) { |
| 416 | PyErr_SetString(PyExc_TypeError, |
| 417 | "instance exception may not have a separate value"); |
| 418 | goto failed_throw; |
| 419 | } |
| 420 | else { |
| 421 | /* Normalize to raise <class>, <instance> */ |
| 422 | Py_XDECREF(val); |
| 423 | val = typ; |
| 424 | typ = PyExceptionInstance_Class(typ); |
| 425 | Py_INCREF(typ); |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 426 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 427 | if (tb == NULL) |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 428 | /* Returns NULL if there's no traceback */ |
| 429 | tb = PyException_GetTraceback(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | else { |
| 433 | /* Not something you can raise. throw() fails. */ |
| 434 | PyErr_Format(PyExc_TypeError, |
| 435 | "exceptions must be classes or instances " |
| 436 | "deriving from BaseException, not %s", |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 437 | Py_TYPE(typ)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | goto failed_throw; |
| 439 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | PyErr_Restore(typ, val, tb); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 442 | return gen_send_ex(gen, Py_None, 1, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 443 | |
| 444 | failed_throw: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | /* Didn't use our arguments, so restore their original refcounts */ |
| 446 | Py_DECREF(typ); |
| 447 | Py_XDECREF(val); |
| 448 | Py_XDECREF(tb); |
| 449 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
| 453 | static PyObject * |
| 454 | gen_iternext(PyGenObject *gen) |
| 455 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 456 | return gen_send_ex(gen, NULL, 0, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 459 | /* |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 460 | * If StopIteration exception is set, fetches its 'value' |
| 461 | * attribute if any, otherwise sets pvalue to None. |
| 462 | * |
| 463 | * Returns 0 if no exception or StopIteration is set. |
| 464 | * If any other exception is set, returns -1 and leaves |
| 465 | * pvalue unchanged. |
| 466 | */ |
| 467 | |
| 468 | int |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 469 | _PyGen_FetchStopIterationValue(PyObject **pvalue) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 470 | PyObject *et, *ev, *tb; |
| 471 | PyObject *value = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 472 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 473 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 474 | PyErr_Fetch(&et, &ev, &tb); |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 475 | if (ev) { |
| 476 | /* exception will usually be normalised already */ |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 477 | if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 478 | value = ((PyStopIterationObject *)ev)->value; |
| 479 | Py_INCREF(value); |
| 480 | Py_DECREF(ev); |
| 481 | } else if (et == PyExc_StopIteration) { |
| 482 | /* avoid normalisation and take ev as value */ |
| 483 | value = ev; |
| 484 | } else { |
| 485 | /* normalisation required */ |
| 486 | PyErr_NormalizeException(&et, &ev, &tb); |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 487 | if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 488 | PyErr_Restore(et, ev, tb); |
| 489 | return -1; |
| 490 | } |
| 491 | value = ((PyStopIterationObject *)ev)->value; |
| 492 | Py_INCREF(value); |
| 493 | Py_DECREF(ev); |
| 494 | } |
| 495 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 496 | Py_XDECREF(et); |
| 497 | Py_XDECREF(tb); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 498 | } else if (PyErr_Occurred()) { |
| 499 | return -1; |
| 500 | } |
| 501 | if (value == NULL) { |
| 502 | value = Py_None; |
Amaury Forgeot d'Arc | e557da8 | 2012-01-13 21:06:12 +0100 | [diff] [blame] | 503 | Py_INCREF(value); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 504 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 505 | *pvalue = value; |
| 506 | return 0; |
| 507 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 508 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 509 | static PyObject * |
| 510 | gen_repr(PyGenObject *gen) |
| 511 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 512 | return PyUnicode_FromFormat("<generator object %S at %p>", |
| 513 | gen->gi_qualname, gen); |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 516 | static PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 517 | gen_get_name(PyGenObject *op) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 518 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 519 | Py_INCREF(op->gi_name); |
| 520 | return op->gi_name; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 523 | static int |
| 524 | gen_set_name(PyGenObject *op, PyObject *value) |
| 525 | { |
| 526 | PyObject *tmp; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 527 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 528 | /* Not legal to del gen.gi_name or to set it to anything |
| 529 | * other than a string object. */ |
| 530 | if (value == NULL || !PyUnicode_Check(value)) { |
| 531 | PyErr_SetString(PyExc_TypeError, |
| 532 | "__name__ must be set to a string object"); |
| 533 | return -1; |
| 534 | } |
| 535 | tmp = op->gi_name; |
| 536 | Py_INCREF(value); |
| 537 | op->gi_name = value; |
| 538 | Py_DECREF(tmp); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static PyObject * |
| 543 | gen_get_qualname(PyGenObject *op) |
| 544 | { |
| 545 | Py_INCREF(op->gi_qualname); |
| 546 | return op->gi_qualname; |
| 547 | } |
| 548 | |
| 549 | static int |
| 550 | gen_set_qualname(PyGenObject *op, PyObject *value) |
| 551 | { |
| 552 | PyObject *tmp; |
| 553 | |
| 554 | /* Not legal to del gen.__qualname__ or to set it to anything |
| 555 | * other than a string object. */ |
| 556 | if (value == NULL || !PyUnicode_Check(value)) { |
| 557 | PyErr_SetString(PyExc_TypeError, |
| 558 | "__qualname__ must be set to a string object"); |
| 559 | return -1; |
| 560 | } |
| 561 | tmp = op->gi_qualname; |
| 562 | Py_INCREF(value); |
| 563 | op->gi_qualname = value; |
| 564 | Py_DECREF(tmp); |
| 565 | return 0; |
| 566 | } |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 567 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 568 | static PyObject * |
| 569 | gen_getyieldfrom(PyGenObject *gen) |
| 570 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 571 | PyObject *yf = _PyGen_yf(gen); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 572 | if (yf == NULL) |
| 573 | Py_RETURN_NONE; |
| 574 | return yf; |
| 575 | } |
| 576 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 577 | static PyGetSetDef gen_getsetlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 578 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 579 | PyDoc_STR("name of the generator")}, |
| 580 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 581 | PyDoc_STR("qualified name of the generator")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 582 | {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL, |
| 583 | PyDoc_STR("object being iterated by yield from, or None")}, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 584 | {NULL} /* Sentinel */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 587 | static PyMemberDef gen_memberlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 588 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
| 589 | {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY}, |
| 590 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | {NULL} /* Sentinel */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 594 | static PyMethodDef gen_methods[] = { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 595 | {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 597 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 598 | {NULL, NULL} /* Sentinel */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 601 | PyTypeObject PyGen_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 603 | "generator", /* tp_name */ |
| 604 | sizeof(PyGenObject), /* tp_basicsize */ |
| 605 | 0, /* tp_itemsize */ |
| 606 | /* methods */ |
| 607 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 608 | 0, /* tp_print */ |
| 609 | 0, /* tp_getattr */ |
| 610 | 0, /* tp_setattr */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 611 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | (reprfunc)gen_repr, /* tp_repr */ |
| 613 | 0, /* tp_as_number */ |
| 614 | 0, /* tp_as_sequence */ |
| 615 | 0, /* tp_as_mapping */ |
| 616 | 0, /* tp_hash */ |
| 617 | 0, /* tp_call */ |
| 618 | 0, /* tp_str */ |
| 619 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 620 | 0, /* tp_setattro */ |
| 621 | 0, /* tp_as_buffer */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 622 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 623 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | 0, /* tp_doc */ |
| 625 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 626 | 0, /* tp_clear */ |
| 627 | 0, /* tp_richcompare */ |
| 628 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 629 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 631 | gen_methods, /* tp_methods */ |
| 632 | gen_memberlist, /* tp_members */ |
| 633 | gen_getsetlist, /* tp_getset */ |
| 634 | 0, /* tp_base */ |
| 635 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | 0, /* tp_descr_get */ |
| 638 | 0, /* tp_descr_set */ |
| 639 | 0, /* tp_dictoffset */ |
| 640 | 0, /* tp_init */ |
| 641 | 0, /* tp_alloc */ |
| 642 | 0, /* tp_new */ |
| 643 | 0, /* tp_free */ |
| 644 | 0, /* tp_is_gc */ |
| 645 | 0, /* tp_bases */ |
| 646 | 0, /* tp_mro */ |
| 647 | 0, /* tp_cache */ |
| 648 | 0, /* tp_subclasses */ |
| 649 | 0, /* tp_weaklist */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 650 | 0, /* tp_del */ |
| 651 | 0, /* tp_version_tag */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 652 | _PyGen_Finalize, /* tp_finalize */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 653 | }; |
| 654 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 655 | static PyObject * |
| 656 | gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f, |
| 657 | PyObject *name, PyObject *qualname) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 658 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 659 | PyGenObject *gen = PyObject_GC_New(PyGenObject, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | if (gen == NULL) { |
| 661 | Py_DECREF(f); |
| 662 | return NULL; |
| 663 | } |
| 664 | gen->gi_frame = f; |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 665 | f->f_gen = (PyObject *) gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | Py_INCREF(f->f_code); |
| 667 | gen->gi_code = (PyObject *)(f->f_code); |
| 668 | gen->gi_running = 0; |
| 669 | gen->gi_weakreflist = NULL; |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 670 | if (name != NULL) |
| 671 | gen->gi_name = name; |
| 672 | else |
| 673 | gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 674 | Py_INCREF(gen->gi_name); |
| 675 | if (qualname != NULL) |
| 676 | gen->gi_qualname = qualname; |
| 677 | else |
| 678 | gen->gi_qualname = gen->gi_name; |
| 679 | Py_INCREF(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | _PyObject_GC_TRACK(gen); |
| 681 | return (PyObject *)gen; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 682 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 683 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 684 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 685 | PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 686 | { |
| 687 | return gen_new_with_qualname(&PyGen_Type, f, name, qualname); |
| 688 | } |
| 689 | |
| 690 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 691 | PyGen_New(PyFrameObject *f) |
| 692 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 693 | return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 696 | int |
| 697 | PyGen_NeedsFinalizing(PyGenObject *gen) |
| 698 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 699 | int i; |
| 700 | PyFrameObject *f = gen->gi_frame; |
| 701 | |
| 702 | if (f == NULL || f->f_stacktop == NULL) |
| 703 | return 0; /* no frame or empty blockstack == no finalization */ |
| 704 | |
| 705 | /* Any block type besides a loop requires cleanup. */ |
| 706 | for (i = 0; i < f->f_iblock; i++) |
| 707 | if (f->f_blockstack[i].b_type != SETUP_LOOP) |
| 708 | return 1; |
| 709 | |
| 710 | /* No blocks except loops, it's safe to skip finalization. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 712 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 713 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 714 | /* Coroutine Object */ |
| 715 | |
| 716 | typedef struct { |
| 717 | PyObject_HEAD |
| 718 | PyCoroObject *cw_coroutine; |
| 719 | } PyCoroWrapper; |
| 720 | |
| 721 | static int |
| 722 | gen_is_coroutine(PyObject *o) |
| 723 | { |
| 724 | if (PyGen_CheckExact(o)) { |
| 725 | PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code; |
| 726 | if (code->co_flags & CO_ITERABLE_COROUTINE) { |
| 727 | return 1; |
| 728 | } |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 733 | /* |
| 734 | * This helper function returns an awaitable for `o`: |
| 735 | * - `o` if `o` is a coroutine-object; |
| 736 | * - `type(o)->tp_as_async->am_await(o)` |
| 737 | * |
| 738 | * Raises a TypeError if it's not possible to return |
| 739 | * an awaitable and returns NULL. |
| 740 | */ |
| 741 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 742 | _PyCoro_GetAwaitableIter(PyObject *o) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 743 | { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 744 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 745 | PyTypeObject *ot; |
| 746 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 747 | if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) { |
| 748 | /* 'o' is a coroutine. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 749 | Py_INCREF(o); |
| 750 | return o; |
| 751 | } |
| 752 | |
| 753 | ot = Py_TYPE(o); |
| 754 | if (ot->tp_as_async != NULL) { |
| 755 | getter = ot->tp_as_async->am_await; |
| 756 | } |
| 757 | if (getter != NULL) { |
| 758 | PyObject *res = (*getter)(o); |
| 759 | if (res != NULL) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 760 | if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) { |
| 761 | /* __await__ must return an *iterator*, not |
| 762 | a coroutine or another awaitable (see PEP 492) */ |
| 763 | PyErr_SetString(PyExc_TypeError, |
| 764 | "__await__() returned a coroutine"); |
| 765 | Py_CLEAR(res); |
| 766 | } else if (!PyIter_Check(res)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 767 | PyErr_Format(PyExc_TypeError, |
| 768 | "__await__() returned non-iterator " |
| 769 | "of type '%.100s'", |
| 770 | Py_TYPE(res)->tp_name); |
| 771 | Py_CLEAR(res); |
| 772 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 773 | } |
| 774 | return res; |
| 775 | } |
| 776 | |
| 777 | PyErr_Format(PyExc_TypeError, |
| 778 | "object %.100s can't be used in 'await' expression", |
| 779 | ot->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 780 | return NULL; |
| 781 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 782 | |
| 783 | static PyObject * |
| 784 | coro_repr(PyCoroObject *coro) |
| 785 | { |
| 786 | return PyUnicode_FromFormat("<coroutine object %S at %p>", |
| 787 | coro->cr_qualname, coro); |
| 788 | } |
| 789 | |
| 790 | static PyObject * |
| 791 | coro_await(PyCoroObject *coro) |
| 792 | { |
| 793 | PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type); |
| 794 | if (cw == NULL) { |
| 795 | return NULL; |
| 796 | } |
| 797 | Py_INCREF(coro); |
| 798 | cw->cw_coroutine = coro; |
| 799 | _PyObject_GC_TRACK(cw); |
| 800 | return (PyObject *)cw; |
| 801 | } |
| 802 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 803 | static PyObject * |
| 804 | coro_get_cr_await(PyCoroObject *coro) |
| 805 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 806 | PyObject *yf = _PyGen_yf((PyGenObject *) coro); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 807 | if (yf == NULL) |
| 808 | Py_RETURN_NONE; |
| 809 | return yf; |
| 810 | } |
| 811 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 812 | static PyGetSetDef coro_getsetlist[] = { |
| 813 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 814 | PyDoc_STR("name of the coroutine")}, |
| 815 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 816 | PyDoc_STR("qualified name of the coroutine")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 817 | {"cr_await", (getter)coro_get_cr_await, NULL, |
| 818 | PyDoc_STR("object being awaited on, or None")}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 819 | {NULL} /* Sentinel */ |
| 820 | }; |
| 821 | |
| 822 | static PyMemberDef coro_memberlist[] = { |
| 823 | {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY}, |
| 824 | {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY}, |
| 825 | {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY}, |
| 826 | {NULL} /* Sentinel */ |
| 827 | }; |
| 828 | |
| 829 | PyDoc_STRVAR(coro_send_doc, |
| 830 | "send(arg) -> send 'arg' into coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 831 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 832 | |
| 833 | PyDoc_STRVAR(coro_throw_doc, |
| 834 | "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 835 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 836 | |
| 837 | PyDoc_STRVAR(coro_close_doc, |
| 838 | "close() -> raise GeneratorExit inside coroutine."); |
| 839 | |
| 840 | static PyMethodDef coro_methods[] = { |
| 841 | {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc}, |
| 842 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc}, |
| 843 | {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc}, |
| 844 | {NULL, NULL} /* Sentinel */ |
| 845 | }; |
| 846 | |
| 847 | static PyAsyncMethods coro_as_async = { |
| 848 | (unaryfunc)coro_await, /* am_await */ |
| 849 | 0, /* am_aiter */ |
| 850 | 0 /* am_anext */ |
| 851 | }; |
| 852 | |
| 853 | PyTypeObject PyCoro_Type = { |
| 854 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 855 | "coroutine", /* tp_name */ |
| 856 | sizeof(PyCoroObject), /* tp_basicsize */ |
| 857 | 0, /* tp_itemsize */ |
| 858 | /* methods */ |
| 859 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 860 | 0, /* tp_print */ |
| 861 | 0, /* tp_getattr */ |
| 862 | 0, /* tp_setattr */ |
| 863 | &coro_as_async, /* tp_as_async */ |
| 864 | (reprfunc)coro_repr, /* tp_repr */ |
| 865 | 0, /* tp_as_number */ |
| 866 | 0, /* tp_as_sequence */ |
| 867 | 0, /* tp_as_mapping */ |
| 868 | 0, /* tp_hash */ |
| 869 | 0, /* tp_call */ |
| 870 | 0, /* tp_str */ |
| 871 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 872 | 0, /* tp_setattro */ |
| 873 | 0, /* tp_as_buffer */ |
| 874 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 875 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
| 876 | 0, /* tp_doc */ |
| 877 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 878 | 0, /* tp_clear */ |
| 879 | 0, /* tp_richcompare */ |
| 880 | offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */ |
| 881 | 0, /* tp_iter */ |
| 882 | 0, /* tp_iternext */ |
| 883 | coro_methods, /* tp_methods */ |
| 884 | coro_memberlist, /* tp_members */ |
| 885 | coro_getsetlist, /* tp_getset */ |
| 886 | 0, /* tp_base */ |
| 887 | 0, /* tp_dict */ |
| 888 | 0, /* tp_descr_get */ |
| 889 | 0, /* tp_descr_set */ |
| 890 | 0, /* tp_dictoffset */ |
| 891 | 0, /* tp_init */ |
| 892 | 0, /* tp_alloc */ |
| 893 | 0, /* tp_new */ |
| 894 | 0, /* tp_free */ |
| 895 | 0, /* tp_is_gc */ |
| 896 | 0, /* tp_bases */ |
| 897 | 0, /* tp_mro */ |
| 898 | 0, /* tp_cache */ |
| 899 | 0, /* tp_subclasses */ |
| 900 | 0, /* tp_weaklist */ |
| 901 | 0, /* tp_del */ |
| 902 | 0, /* tp_version_tag */ |
| 903 | _PyGen_Finalize, /* tp_finalize */ |
| 904 | }; |
| 905 | |
| 906 | static void |
| 907 | coro_wrapper_dealloc(PyCoroWrapper *cw) |
| 908 | { |
| 909 | _PyObject_GC_UNTRACK((PyObject *)cw); |
| 910 | Py_CLEAR(cw->cw_coroutine); |
| 911 | PyObject_GC_Del(cw); |
| 912 | } |
| 913 | |
| 914 | static PyObject * |
| 915 | coro_wrapper_iternext(PyCoroWrapper *cw) |
| 916 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 917 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static PyObject * |
| 921 | coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg) |
| 922 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 923 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static PyObject * |
| 927 | coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args) |
| 928 | { |
| 929 | return gen_throw((PyGenObject *)cw->cw_coroutine, args); |
| 930 | } |
| 931 | |
| 932 | static PyObject * |
| 933 | coro_wrapper_close(PyCoroWrapper *cw, PyObject *args) |
| 934 | { |
| 935 | return gen_close((PyGenObject *)cw->cw_coroutine, args); |
| 936 | } |
| 937 | |
| 938 | static int |
| 939 | coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg) |
| 940 | { |
| 941 | Py_VISIT((PyObject *)cw->cw_coroutine); |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static PyMethodDef coro_wrapper_methods[] = { |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 946 | {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc}, |
| 947 | {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc}, |
| 948 | {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 949 | {NULL, NULL} /* Sentinel */ |
| 950 | }; |
| 951 | |
| 952 | PyTypeObject _PyCoroWrapper_Type = { |
| 953 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 954 | "coroutine_wrapper", |
| 955 | sizeof(PyCoroWrapper), /* tp_basicsize */ |
| 956 | 0, /* tp_itemsize */ |
| 957 | (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */ |
| 958 | 0, /* tp_print */ |
| 959 | 0, /* tp_getattr */ |
| 960 | 0, /* tp_setattr */ |
| 961 | 0, /* tp_as_async */ |
| 962 | 0, /* tp_repr */ |
| 963 | 0, /* tp_as_number */ |
| 964 | 0, /* tp_as_sequence */ |
| 965 | 0, /* tp_as_mapping */ |
| 966 | 0, /* tp_hash */ |
| 967 | 0, /* tp_call */ |
| 968 | 0, /* tp_str */ |
| 969 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 970 | 0, /* tp_setattro */ |
| 971 | 0, /* tp_as_buffer */ |
| 972 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 973 | "A wrapper object implementing __await__ for coroutines.", |
| 974 | (traverseproc)coro_wrapper_traverse, /* tp_traverse */ |
| 975 | 0, /* tp_clear */ |
| 976 | 0, /* tp_richcompare */ |
| 977 | 0, /* tp_weaklistoffset */ |
| 978 | PyObject_SelfIter, /* tp_iter */ |
| 979 | (iternextfunc)coro_wrapper_iternext, /* tp_iternext */ |
| 980 | coro_wrapper_methods, /* tp_methods */ |
| 981 | 0, /* tp_members */ |
| 982 | 0, /* tp_getset */ |
| 983 | 0, /* tp_base */ |
| 984 | 0, /* tp_dict */ |
| 985 | 0, /* tp_descr_get */ |
| 986 | 0, /* tp_descr_set */ |
| 987 | 0, /* tp_dictoffset */ |
| 988 | 0, /* tp_init */ |
| 989 | 0, /* tp_alloc */ |
| 990 | 0, /* tp_new */ |
| 991 | PyObject_Del, /* tp_free */ |
| 992 | }; |
| 993 | |
| 994 | PyObject * |
| 995 | PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 996 | { |
| 997 | return gen_new_with_qualname(&PyCoro_Type, f, name, qualname); |
| 998 | } |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 999 | |
| 1000 | |
| 1001 | /* __aiter__ wrapper; see http://bugs.python.org/issue27243 for details. */ |
| 1002 | |
| 1003 | typedef struct { |
| 1004 | PyObject_HEAD |
| 1005 | PyObject *aw_aiter; |
| 1006 | } PyAIterWrapper; |
| 1007 | |
| 1008 | |
| 1009 | static PyObject * |
| 1010 | aiter_wrapper_iternext(PyAIterWrapper *aw) |
| 1011 | { |
| 1012 | PyErr_SetObject(PyExc_StopIteration, aw->aw_aiter); |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | |
| 1016 | static int |
| 1017 | aiter_wrapper_traverse(PyAIterWrapper *aw, visitproc visit, void *arg) |
| 1018 | { |
| 1019 | Py_VISIT((PyObject *)aw->aw_aiter); |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static void |
| 1024 | aiter_wrapper_dealloc(PyAIterWrapper *aw) |
| 1025 | { |
| 1026 | _PyObject_GC_UNTRACK((PyObject *)aw); |
| 1027 | Py_CLEAR(aw->aw_aiter); |
| 1028 | PyObject_GC_Del(aw); |
| 1029 | } |
| 1030 | |
| 1031 | static PyAsyncMethods aiter_wrapper_as_async = { |
| 1032 | PyObject_SelfIter, /* am_await */ |
| 1033 | 0, /* am_aiter */ |
| 1034 | 0 /* am_anext */ |
| 1035 | }; |
| 1036 | |
| 1037 | PyTypeObject _PyAIterWrapper_Type = { |
| 1038 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1039 | "aiter_wrapper", |
| 1040 | sizeof(PyAIterWrapper), /* tp_basicsize */ |
| 1041 | 0, /* tp_itemsize */ |
| 1042 | (destructor)aiter_wrapper_dealloc, /* destructor tp_dealloc */ |
| 1043 | 0, /* tp_print */ |
| 1044 | 0, /* tp_getattr */ |
| 1045 | 0, /* tp_setattr */ |
| 1046 | &aiter_wrapper_as_async, /* tp_as_async */ |
| 1047 | 0, /* tp_repr */ |
| 1048 | 0, /* tp_as_number */ |
| 1049 | 0, /* tp_as_sequence */ |
| 1050 | 0, /* tp_as_mapping */ |
| 1051 | 0, /* tp_hash */ |
| 1052 | 0, /* tp_call */ |
| 1053 | 0, /* tp_str */ |
| 1054 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1055 | 0, /* tp_setattro */ |
| 1056 | 0, /* tp_as_buffer */ |
| 1057 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1058 | "A wrapper object for __aiter__ bakwards compatibility.", |
| 1059 | (traverseproc)aiter_wrapper_traverse, /* tp_traverse */ |
| 1060 | 0, /* tp_clear */ |
| 1061 | 0, /* tp_richcompare */ |
| 1062 | 0, /* tp_weaklistoffset */ |
| 1063 | PyObject_SelfIter, /* tp_iter */ |
| 1064 | (iternextfunc)aiter_wrapper_iternext, /* tp_iternext */ |
| 1065 | 0, /* tp_methods */ |
| 1066 | 0, /* tp_members */ |
| 1067 | 0, /* tp_getset */ |
| 1068 | 0, /* tp_base */ |
| 1069 | 0, /* tp_dict */ |
| 1070 | 0, /* tp_descr_get */ |
| 1071 | 0, /* tp_descr_set */ |
| 1072 | 0, /* tp_dictoffset */ |
| 1073 | 0, /* tp_init */ |
| 1074 | 0, /* tp_alloc */ |
| 1075 | 0, /* tp_new */ |
| 1076 | PyObject_Del, /* tp_free */ |
| 1077 | }; |
| 1078 | |
| 1079 | |
| 1080 | PyObject * |
| 1081 | _PyAIterWrapper_New(PyObject *aiter) |
| 1082 | { |
| 1083 | PyAIterWrapper *aw = PyObject_GC_New(PyAIterWrapper, |
| 1084 | &_PyAIterWrapper_Type); |
| 1085 | if (aw == NULL) { |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | Py_INCREF(aiter); |
| 1089 | aw->aw_aiter = aiter; |
| 1090 | _PyObject_GC_TRACK(aw); |
| 1091 | return (PyObject *)aw; |
| 1092 | } |