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 | |
Martin Panter | 7e3a91a | 2016-02-10 04:40:48 +0000 | [diff] [blame] | 194 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 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 { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame^] | 264 | retval = _PyObject_CallNoArg(meth); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 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 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 284 | if (code[f->f_lasti + 2] != YIELD_FROM) |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 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 */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 383 | gen->gi_frame->f_lasti += 2; |
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 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 526 | /* Not legal to del gen.gi_name or to set it to anything |
| 527 | * other than a string object. */ |
| 528 | if (value == NULL || !PyUnicode_Check(value)) { |
| 529 | PyErr_SetString(PyExc_TypeError, |
| 530 | "__name__ must be set to a string object"); |
| 531 | return -1; |
| 532 | } |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 533 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 534 | Py_XSETREF(op->gi_name, value); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static PyObject * |
| 539 | gen_get_qualname(PyGenObject *op) |
| 540 | { |
| 541 | Py_INCREF(op->gi_qualname); |
| 542 | return op->gi_qualname; |
| 543 | } |
| 544 | |
| 545 | static int |
| 546 | gen_set_qualname(PyGenObject *op, PyObject *value) |
| 547 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 548 | /* Not legal to del gen.__qualname__ or to set it to anything |
| 549 | * other than a string object. */ |
| 550 | if (value == NULL || !PyUnicode_Check(value)) { |
| 551 | PyErr_SetString(PyExc_TypeError, |
| 552 | "__qualname__ must be set to a string object"); |
| 553 | return -1; |
| 554 | } |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 555 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 556 | Py_XSETREF(op->gi_qualname, value); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 557 | return 0; |
| 558 | } |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 559 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 560 | static PyObject * |
| 561 | gen_getyieldfrom(PyGenObject *gen) |
| 562 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 563 | PyObject *yf = _PyGen_yf(gen); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 564 | if (yf == NULL) |
| 565 | Py_RETURN_NONE; |
| 566 | return yf; |
| 567 | } |
| 568 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 569 | static PyGetSetDef gen_getsetlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 570 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 571 | PyDoc_STR("name of the generator")}, |
| 572 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 573 | PyDoc_STR("qualified name of the generator")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 574 | {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL, |
| 575 | PyDoc_STR("object being iterated by yield from, or None")}, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 576 | {NULL} /* Sentinel */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 577 | }; |
| 578 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 579 | static PyMemberDef gen_memberlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 580 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
| 581 | {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY}, |
| 582 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | {NULL} /* Sentinel */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 584 | }; |
| 585 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 586 | static PyMethodDef gen_methods[] = { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 587 | {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 589 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 590 | {NULL, NULL} /* Sentinel */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 591 | }; |
| 592 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 593 | PyTypeObject PyGen_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 595 | "generator", /* tp_name */ |
| 596 | sizeof(PyGenObject), /* tp_basicsize */ |
| 597 | 0, /* tp_itemsize */ |
| 598 | /* methods */ |
| 599 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 600 | 0, /* tp_print */ |
| 601 | 0, /* tp_getattr */ |
| 602 | 0, /* tp_setattr */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 603 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | (reprfunc)gen_repr, /* tp_repr */ |
| 605 | 0, /* tp_as_number */ |
| 606 | 0, /* tp_as_sequence */ |
| 607 | 0, /* tp_as_mapping */ |
| 608 | 0, /* tp_hash */ |
| 609 | 0, /* tp_call */ |
| 610 | 0, /* tp_str */ |
| 611 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 612 | 0, /* tp_setattro */ |
| 613 | 0, /* tp_as_buffer */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 614 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 615 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | 0, /* tp_doc */ |
| 617 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 618 | 0, /* tp_clear */ |
| 619 | 0, /* tp_richcompare */ |
| 620 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 621 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 623 | gen_methods, /* tp_methods */ |
| 624 | gen_memberlist, /* tp_members */ |
| 625 | gen_getsetlist, /* tp_getset */ |
| 626 | 0, /* tp_base */ |
| 627 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | 0, /* tp_descr_get */ |
| 630 | 0, /* tp_descr_set */ |
| 631 | 0, /* tp_dictoffset */ |
| 632 | 0, /* tp_init */ |
| 633 | 0, /* tp_alloc */ |
| 634 | 0, /* tp_new */ |
| 635 | 0, /* tp_free */ |
| 636 | 0, /* tp_is_gc */ |
| 637 | 0, /* tp_bases */ |
| 638 | 0, /* tp_mro */ |
| 639 | 0, /* tp_cache */ |
| 640 | 0, /* tp_subclasses */ |
| 641 | 0, /* tp_weaklist */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 642 | 0, /* tp_del */ |
| 643 | 0, /* tp_version_tag */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 644 | _PyGen_Finalize, /* tp_finalize */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 645 | }; |
| 646 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 647 | static PyObject * |
| 648 | gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f, |
| 649 | PyObject *name, PyObject *qualname) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 650 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 651 | PyGenObject *gen = PyObject_GC_New(PyGenObject, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | if (gen == NULL) { |
| 653 | Py_DECREF(f); |
| 654 | return NULL; |
| 655 | } |
| 656 | gen->gi_frame = f; |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 657 | f->f_gen = (PyObject *) gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | Py_INCREF(f->f_code); |
| 659 | gen->gi_code = (PyObject *)(f->f_code); |
| 660 | gen->gi_running = 0; |
| 661 | gen->gi_weakreflist = NULL; |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 662 | if (name != NULL) |
| 663 | gen->gi_name = name; |
| 664 | else |
| 665 | gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 666 | Py_INCREF(gen->gi_name); |
| 667 | if (qualname != NULL) |
| 668 | gen->gi_qualname = qualname; |
| 669 | else |
| 670 | gen->gi_qualname = gen->gi_name; |
| 671 | Py_INCREF(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | _PyObject_GC_TRACK(gen); |
| 673 | return (PyObject *)gen; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 674 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 675 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 676 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 677 | PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 678 | { |
| 679 | return gen_new_with_qualname(&PyGen_Type, f, name, qualname); |
| 680 | } |
| 681 | |
| 682 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 683 | PyGen_New(PyFrameObject *f) |
| 684 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 685 | return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 688 | int |
| 689 | PyGen_NeedsFinalizing(PyGenObject *gen) |
| 690 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 691 | int i; |
| 692 | PyFrameObject *f = gen->gi_frame; |
| 693 | |
| 694 | if (f == NULL || f->f_stacktop == NULL) |
| 695 | return 0; /* no frame or empty blockstack == no finalization */ |
| 696 | |
| 697 | /* Any block type besides a loop requires cleanup. */ |
| 698 | for (i = 0; i < f->f_iblock; i++) |
| 699 | if (f->f_blockstack[i].b_type != SETUP_LOOP) |
| 700 | return 1; |
| 701 | |
| 702 | /* No blocks except loops, it's safe to skip finalization. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 704 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 705 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 706 | /* Coroutine Object */ |
| 707 | |
| 708 | typedef struct { |
| 709 | PyObject_HEAD |
| 710 | PyCoroObject *cw_coroutine; |
| 711 | } PyCoroWrapper; |
| 712 | |
| 713 | static int |
| 714 | gen_is_coroutine(PyObject *o) |
| 715 | { |
| 716 | if (PyGen_CheckExact(o)) { |
| 717 | PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code; |
| 718 | if (code->co_flags & CO_ITERABLE_COROUTINE) { |
| 719 | return 1; |
| 720 | } |
| 721 | } |
| 722 | return 0; |
| 723 | } |
| 724 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 725 | /* |
| 726 | * This helper function returns an awaitable for `o`: |
| 727 | * - `o` if `o` is a coroutine-object; |
| 728 | * - `type(o)->tp_as_async->am_await(o)` |
| 729 | * |
| 730 | * Raises a TypeError if it's not possible to return |
| 731 | * an awaitable and returns NULL. |
| 732 | */ |
| 733 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 734 | _PyCoro_GetAwaitableIter(PyObject *o) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 735 | { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 736 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 737 | PyTypeObject *ot; |
| 738 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 739 | if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) { |
| 740 | /* 'o' is a coroutine. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 741 | Py_INCREF(o); |
| 742 | return o; |
| 743 | } |
| 744 | |
| 745 | ot = Py_TYPE(o); |
| 746 | if (ot->tp_as_async != NULL) { |
| 747 | getter = ot->tp_as_async->am_await; |
| 748 | } |
| 749 | if (getter != NULL) { |
| 750 | PyObject *res = (*getter)(o); |
| 751 | if (res != NULL) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 752 | if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) { |
| 753 | /* __await__ must return an *iterator*, not |
| 754 | a coroutine or another awaitable (see PEP 492) */ |
| 755 | PyErr_SetString(PyExc_TypeError, |
| 756 | "__await__() returned a coroutine"); |
| 757 | Py_CLEAR(res); |
| 758 | } else if (!PyIter_Check(res)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 759 | PyErr_Format(PyExc_TypeError, |
| 760 | "__await__() returned non-iterator " |
| 761 | "of type '%.100s'", |
| 762 | Py_TYPE(res)->tp_name); |
| 763 | Py_CLEAR(res); |
| 764 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 765 | } |
| 766 | return res; |
| 767 | } |
| 768 | |
| 769 | PyErr_Format(PyExc_TypeError, |
| 770 | "object %.100s can't be used in 'await' expression", |
| 771 | ot->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 772 | return NULL; |
| 773 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 774 | |
| 775 | static PyObject * |
| 776 | coro_repr(PyCoroObject *coro) |
| 777 | { |
| 778 | return PyUnicode_FromFormat("<coroutine object %S at %p>", |
| 779 | coro->cr_qualname, coro); |
| 780 | } |
| 781 | |
| 782 | static PyObject * |
| 783 | coro_await(PyCoroObject *coro) |
| 784 | { |
| 785 | PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type); |
| 786 | if (cw == NULL) { |
| 787 | return NULL; |
| 788 | } |
| 789 | Py_INCREF(coro); |
| 790 | cw->cw_coroutine = coro; |
| 791 | _PyObject_GC_TRACK(cw); |
| 792 | return (PyObject *)cw; |
| 793 | } |
| 794 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 795 | static PyObject * |
| 796 | coro_get_cr_await(PyCoroObject *coro) |
| 797 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 798 | PyObject *yf = _PyGen_yf((PyGenObject *) coro); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 799 | if (yf == NULL) |
| 800 | Py_RETURN_NONE; |
| 801 | return yf; |
| 802 | } |
| 803 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 804 | static PyGetSetDef coro_getsetlist[] = { |
| 805 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 806 | PyDoc_STR("name of the coroutine")}, |
| 807 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 808 | PyDoc_STR("qualified name of the coroutine")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 809 | {"cr_await", (getter)coro_get_cr_await, NULL, |
| 810 | PyDoc_STR("object being awaited on, or None")}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 811 | {NULL} /* Sentinel */ |
| 812 | }; |
| 813 | |
| 814 | static PyMemberDef coro_memberlist[] = { |
| 815 | {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY}, |
| 816 | {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY}, |
| 817 | {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY}, |
| 818 | {NULL} /* Sentinel */ |
| 819 | }; |
| 820 | |
| 821 | PyDoc_STRVAR(coro_send_doc, |
| 822 | "send(arg) -> send 'arg' into coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 823 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 824 | |
| 825 | PyDoc_STRVAR(coro_throw_doc, |
| 826 | "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 827 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 828 | |
| 829 | PyDoc_STRVAR(coro_close_doc, |
| 830 | "close() -> raise GeneratorExit inside coroutine."); |
| 831 | |
| 832 | static PyMethodDef coro_methods[] = { |
| 833 | {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc}, |
| 834 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc}, |
| 835 | {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc}, |
| 836 | {NULL, NULL} /* Sentinel */ |
| 837 | }; |
| 838 | |
| 839 | static PyAsyncMethods coro_as_async = { |
| 840 | (unaryfunc)coro_await, /* am_await */ |
| 841 | 0, /* am_aiter */ |
| 842 | 0 /* am_anext */ |
| 843 | }; |
| 844 | |
| 845 | PyTypeObject PyCoro_Type = { |
| 846 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 847 | "coroutine", /* tp_name */ |
| 848 | sizeof(PyCoroObject), /* tp_basicsize */ |
| 849 | 0, /* tp_itemsize */ |
| 850 | /* methods */ |
| 851 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 852 | 0, /* tp_print */ |
| 853 | 0, /* tp_getattr */ |
| 854 | 0, /* tp_setattr */ |
| 855 | &coro_as_async, /* tp_as_async */ |
| 856 | (reprfunc)coro_repr, /* tp_repr */ |
| 857 | 0, /* tp_as_number */ |
| 858 | 0, /* tp_as_sequence */ |
| 859 | 0, /* tp_as_mapping */ |
| 860 | 0, /* tp_hash */ |
| 861 | 0, /* tp_call */ |
| 862 | 0, /* tp_str */ |
| 863 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 864 | 0, /* tp_setattro */ |
| 865 | 0, /* tp_as_buffer */ |
| 866 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 867 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
| 868 | 0, /* tp_doc */ |
| 869 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 870 | 0, /* tp_clear */ |
| 871 | 0, /* tp_richcompare */ |
| 872 | offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */ |
| 873 | 0, /* tp_iter */ |
| 874 | 0, /* tp_iternext */ |
| 875 | coro_methods, /* tp_methods */ |
| 876 | coro_memberlist, /* tp_members */ |
| 877 | coro_getsetlist, /* tp_getset */ |
| 878 | 0, /* tp_base */ |
| 879 | 0, /* tp_dict */ |
| 880 | 0, /* tp_descr_get */ |
| 881 | 0, /* tp_descr_set */ |
| 882 | 0, /* tp_dictoffset */ |
| 883 | 0, /* tp_init */ |
| 884 | 0, /* tp_alloc */ |
| 885 | 0, /* tp_new */ |
| 886 | 0, /* tp_free */ |
| 887 | 0, /* tp_is_gc */ |
| 888 | 0, /* tp_bases */ |
| 889 | 0, /* tp_mro */ |
| 890 | 0, /* tp_cache */ |
| 891 | 0, /* tp_subclasses */ |
| 892 | 0, /* tp_weaklist */ |
| 893 | 0, /* tp_del */ |
| 894 | 0, /* tp_version_tag */ |
| 895 | _PyGen_Finalize, /* tp_finalize */ |
| 896 | }; |
| 897 | |
| 898 | static void |
| 899 | coro_wrapper_dealloc(PyCoroWrapper *cw) |
| 900 | { |
| 901 | _PyObject_GC_UNTRACK((PyObject *)cw); |
| 902 | Py_CLEAR(cw->cw_coroutine); |
| 903 | PyObject_GC_Del(cw); |
| 904 | } |
| 905 | |
| 906 | static PyObject * |
| 907 | coro_wrapper_iternext(PyCoroWrapper *cw) |
| 908 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 909 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static PyObject * |
| 913 | coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg) |
| 914 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 915 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static PyObject * |
| 919 | coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args) |
| 920 | { |
| 921 | return gen_throw((PyGenObject *)cw->cw_coroutine, args); |
| 922 | } |
| 923 | |
| 924 | static PyObject * |
| 925 | coro_wrapper_close(PyCoroWrapper *cw, PyObject *args) |
| 926 | { |
| 927 | return gen_close((PyGenObject *)cw->cw_coroutine, args); |
| 928 | } |
| 929 | |
| 930 | static int |
| 931 | coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg) |
| 932 | { |
| 933 | Py_VISIT((PyObject *)cw->cw_coroutine); |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static PyMethodDef coro_wrapper_methods[] = { |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 938 | {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc}, |
| 939 | {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc}, |
| 940 | {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 941 | {NULL, NULL} /* Sentinel */ |
| 942 | }; |
| 943 | |
| 944 | PyTypeObject _PyCoroWrapper_Type = { |
| 945 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 946 | "coroutine_wrapper", |
| 947 | sizeof(PyCoroWrapper), /* tp_basicsize */ |
| 948 | 0, /* tp_itemsize */ |
| 949 | (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */ |
| 950 | 0, /* tp_print */ |
| 951 | 0, /* tp_getattr */ |
| 952 | 0, /* tp_setattr */ |
| 953 | 0, /* tp_as_async */ |
| 954 | 0, /* tp_repr */ |
| 955 | 0, /* tp_as_number */ |
| 956 | 0, /* tp_as_sequence */ |
| 957 | 0, /* tp_as_mapping */ |
| 958 | 0, /* tp_hash */ |
| 959 | 0, /* tp_call */ |
| 960 | 0, /* tp_str */ |
| 961 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 962 | 0, /* tp_setattro */ |
| 963 | 0, /* tp_as_buffer */ |
| 964 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 965 | "A wrapper object implementing __await__ for coroutines.", |
| 966 | (traverseproc)coro_wrapper_traverse, /* tp_traverse */ |
| 967 | 0, /* tp_clear */ |
| 968 | 0, /* tp_richcompare */ |
| 969 | 0, /* tp_weaklistoffset */ |
| 970 | PyObject_SelfIter, /* tp_iter */ |
| 971 | (iternextfunc)coro_wrapper_iternext, /* tp_iternext */ |
| 972 | coro_wrapper_methods, /* tp_methods */ |
| 973 | 0, /* tp_members */ |
| 974 | 0, /* tp_getset */ |
| 975 | 0, /* tp_base */ |
| 976 | 0, /* tp_dict */ |
| 977 | 0, /* tp_descr_get */ |
| 978 | 0, /* tp_descr_set */ |
| 979 | 0, /* tp_dictoffset */ |
| 980 | 0, /* tp_init */ |
| 981 | 0, /* tp_alloc */ |
| 982 | 0, /* tp_new */ |
| 983 | PyObject_Del, /* tp_free */ |
| 984 | }; |
| 985 | |
| 986 | PyObject * |
| 987 | PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 988 | { |
| 989 | return gen_new_with_qualname(&PyCoro_Type, f, name, qualname); |
| 990 | } |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 991 | |
| 992 | |
| 993 | /* __aiter__ wrapper; see http://bugs.python.org/issue27243 for details. */ |
| 994 | |
| 995 | typedef struct { |
| 996 | PyObject_HEAD |
| 997 | PyObject *aw_aiter; |
| 998 | } PyAIterWrapper; |
| 999 | |
| 1000 | |
| 1001 | static PyObject * |
| 1002 | aiter_wrapper_iternext(PyAIterWrapper *aw) |
| 1003 | { |
| 1004 | PyErr_SetObject(PyExc_StopIteration, aw->aw_aiter); |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
| 1008 | static int |
| 1009 | aiter_wrapper_traverse(PyAIterWrapper *aw, visitproc visit, void *arg) |
| 1010 | { |
| 1011 | Py_VISIT((PyObject *)aw->aw_aiter); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | static void |
| 1016 | aiter_wrapper_dealloc(PyAIterWrapper *aw) |
| 1017 | { |
| 1018 | _PyObject_GC_UNTRACK((PyObject *)aw); |
| 1019 | Py_CLEAR(aw->aw_aiter); |
| 1020 | PyObject_GC_Del(aw); |
| 1021 | } |
| 1022 | |
| 1023 | static PyAsyncMethods aiter_wrapper_as_async = { |
| 1024 | PyObject_SelfIter, /* am_await */ |
| 1025 | 0, /* am_aiter */ |
| 1026 | 0 /* am_anext */ |
| 1027 | }; |
| 1028 | |
| 1029 | PyTypeObject _PyAIterWrapper_Type = { |
| 1030 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1031 | "aiter_wrapper", |
| 1032 | sizeof(PyAIterWrapper), /* tp_basicsize */ |
| 1033 | 0, /* tp_itemsize */ |
| 1034 | (destructor)aiter_wrapper_dealloc, /* destructor tp_dealloc */ |
| 1035 | 0, /* tp_print */ |
| 1036 | 0, /* tp_getattr */ |
| 1037 | 0, /* tp_setattr */ |
| 1038 | &aiter_wrapper_as_async, /* tp_as_async */ |
| 1039 | 0, /* tp_repr */ |
| 1040 | 0, /* tp_as_number */ |
| 1041 | 0, /* tp_as_sequence */ |
| 1042 | 0, /* tp_as_mapping */ |
| 1043 | 0, /* tp_hash */ |
| 1044 | 0, /* tp_call */ |
| 1045 | 0, /* tp_str */ |
| 1046 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1047 | 0, /* tp_setattro */ |
| 1048 | 0, /* tp_as_buffer */ |
| 1049 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1050 | "A wrapper object for __aiter__ bakwards compatibility.", |
| 1051 | (traverseproc)aiter_wrapper_traverse, /* tp_traverse */ |
| 1052 | 0, /* tp_clear */ |
| 1053 | 0, /* tp_richcompare */ |
| 1054 | 0, /* tp_weaklistoffset */ |
| 1055 | PyObject_SelfIter, /* tp_iter */ |
| 1056 | (iternextfunc)aiter_wrapper_iternext, /* tp_iternext */ |
| 1057 | 0, /* tp_methods */ |
| 1058 | 0, /* tp_members */ |
| 1059 | 0, /* tp_getset */ |
| 1060 | 0, /* tp_base */ |
| 1061 | 0, /* tp_dict */ |
| 1062 | 0, /* tp_descr_get */ |
| 1063 | 0, /* tp_descr_set */ |
| 1064 | 0, /* tp_dictoffset */ |
| 1065 | 0, /* tp_init */ |
| 1066 | 0, /* tp_alloc */ |
| 1067 | 0, /* tp_new */ |
| 1068 | PyObject_Del, /* tp_free */ |
| 1069 | }; |
| 1070 | |
| 1071 | |
| 1072 | PyObject * |
| 1073 | _PyAIterWrapper_New(PyObject *aiter) |
| 1074 | { |
| 1075 | PyAIterWrapper *aw = PyObject_GC_New(PyAIterWrapper, |
| 1076 | &_PyAIterWrapper_Type); |
| 1077 | if (aw == NULL) { |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | Py_INCREF(aiter); |
| 1081 | aw->aw_aiter = aiter; |
| 1082 | _PyObject_GC_TRACK(aw); |
| 1083 | return (PyObject *)aw; |
| 1084 | } |