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