blob: 00ebbf1cf8dc11ff71f2f4f4c21823a1547f4315 [file] [log] [blame]
Martin v. Löwise440e472004-06-01 15:22:42 +00001/* Generator object implementation */
2
3#include "Python.h"
4#include "frameobject.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00005#include "structmember.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006#include "opcode.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00007
Nick Coghlan1f7ce622012-01-13 21:43:40 +10008static PyObject *gen_close(PyGenObject *gen, PyObject *args);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10009
Martin v. Löwise440e472004-06-01 15:22:42 +000010static int
11gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
12{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 Py_VISIT((PyObject *)gen->gi_frame);
14 Py_VISIT(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +020015 Py_VISIT(gen->gi_name);
16 Py_VISIT(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 return 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000018}
19
Antoine Pitrou58720d62013-08-05 23:26:40 +020020void
21_PyGen_Finalize(PyObject *self)
Antoine Pitrou796564c2013-07-30 19:59:21 +020022{
23 PyGenObject *gen = (PyGenObject *)self;
24 PyObject *res;
25 PyObject *error_type, *error_value, *error_traceback;
26
Yury Selivanov75445082015-05-11 22:57:16 -040027 /* If `gen` is a coroutine, and if it was never awaited on,
28 issue a RuntimeWarning. */
29 if (gen->gi_code != NULL
Yury Selivanov5376ba92015-06-22 12:19:30 -040030 && ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -040031 && gen->gi_frame != NULL
32 && gen->gi_frame->f_lasti == -1
33 && !PyErr_Occurred()
34 && PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
35 "coroutine '%.50S' was never awaited",
36 gen->gi_qualname))
37 return;
38
Antoine Pitrou796564c2013-07-30 19:59:21 +020039 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
40 /* Generator isn't paused, so no need to close */
41 return;
42
43 /* Save the current exception, if any. */
44 PyErr_Fetch(&error_type, &error_value, &error_traceback);
45
46 res = gen_close(gen, NULL);
47
48 if (res == NULL)
49 PyErr_WriteUnraisable(self);
50 else
51 Py_DECREF(res);
52
53 /* Restore the saved exception. */
54 PyErr_Restore(error_type, error_value, error_traceback);
55}
56
57static void
Martin v. Löwise440e472004-06-01 15:22:42 +000058gen_dealloc(PyGenObject *gen)
59{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 PyObject *self = (PyObject *) gen;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 _PyObject_GC_UNTRACK(gen);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if (gen->gi_weakreflist != NULL)
65 PyObject_ClearWeakRefs(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000066
Antoine Pitrou93963562013-05-14 20:37:52 +020067 _PyObject_GC_TRACK(self);
68
Antoine Pitrou796564c2013-07-30 19:59:21 +020069 if (PyObject_CallFinalizerFromDealloc(self))
70 return; /* resurrected. :( */
Antoine Pitrou93963562013-05-14 20:37:52 +020071
72 _PyObject_GC_UNTRACK(self);
73 Py_CLEAR(gen->gi_frame);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 Py_CLEAR(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +020075 Py_CLEAR(gen->gi_name);
76 Py_CLEAR(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 PyObject_GC_Del(gen);
Martin v. Löwise440e472004-06-01 15:22:42 +000078}
79
80static PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000081gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
Martin v. Löwise440e472004-06-01 15:22:42 +000082{
Antoine Pitrou93963562013-05-14 20:37:52 +020083 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +020085 PyObject *result;
Martin v. Löwise440e472004-06-01 15:22:42 +000086
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -050087 if (gen->gi_running) {
Yury Selivanov5376ba92015-06-22 12:19:30 -040088 char *msg = "generator already executing";
89 if (PyCoro_CheckExact(gen))
90 msg = "coroutine already executing";
91 PyErr_SetString(PyExc_ValueError, msg);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -050092 return NULL;
93 }
Antoine Pitrou93963562013-05-14 20:37:52 +020094 if (f == NULL || f->f_stacktop == NULL) {
95 /* Only set exception if called from send() */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (arg && !exc)
97 PyErr_SetNone(PyExc_StopIteration);
98 return NULL;
99 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000100
Antoine Pitrou93963562013-05-14 20:37:52 +0200101 if (f->f_lasti == -1) {
102 if (arg && arg != Py_None) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400103 char *msg = "can't send non-None value to a "
104 "just-started generator";
105 if (PyCoro_CheckExact(gen))
106 msg = "can't send non-None value to a "
107 "just-started coroutine";
108 PyErr_SetString(PyExc_TypeError, msg);
Antoine Pitrou93963562013-05-14 20:37:52 +0200109 return NULL;
110 }
111 } else {
112 /* Push arg onto the frame's value stack */
113 result = arg ? arg : Py_None;
114 Py_INCREF(result);
115 *(f->f_stacktop++) = result;
116 }
117
118 /* Generators always return to their most recent caller, not
119 * necessarily their creator. */
120 Py_XINCREF(tstate->frame);
121 assert(f->f_back == NULL);
122 f->f_back = tstate->frame;
123
124 gen->gi_running = 1;
125 result = PyEval_EvalFrameEx(f, exc);
126 gen->gi_running = 0;
127
128 /* Don't keep the reference to f_back any longer than necessary. It
129 * may keep a chain of frames alive or it could create a reference
130 * cycle. */
131 assert(f->f_back == tstate->frame);
132 Py_CLEAR(f->f_back);
133
134 /* If the generator just returned (as opposed to yielding), signal
135 * that the generator is exhausted. */
136 if (result && f->f_stacktop == NULL) {
137 if (result == Py_None) {
138 /* Delay exception instantiation if we can */
139 PyErr_SetNone(PyExc_StopIteration);
140 } else {
141 PyObject *e = PyObject_CallFunctionObjArgs(
142 PyExc_StopIteration, result, NULL);
143 if (e != NULL) {
144 PyErr_SetObject(PyExc_StopIteration, e);
145 Py_DECREF(e);
146 }
147 }
148 Py_CLEAR(result);
149 }
Yury Selivanov68333392015-05-22 11:16:47 -0400150 else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400151 /* Check for __future__ generator_stop and conditionally turn
152 * a leaking StopIteration into RuntimeError (with its cause
153 * set appropriately). */
Yury Selivanov68333392015-05-22 11:16:47 -0400154 if (((PyCodeObject *)gen->gi_code)->co_flags &
Yury Selivanov75445082015-05-11 22:57:16 -0400155 (CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE))
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400156 {
157 PyObject *exc, *val, *val2, *tb;
Yury Selivanov5376ba92015-06-22 12:19:30 -0400158 char *msg = "generator raised StopIteration";
159 if (PyCoro_CheckExact(gen))
160 msg = "coroutine raised StopIteration";
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400161 PyErr_Fetch(&exc, &val, &tb);
162 PyErr_NormalizeException(&exc, &val, &tb);
163 if (tb != NULL)
164 PyException_SetTraceback(val, tb);
165 Py_DECREF(exc);
166 Py_XDECREF(tb);
Yury Selivanov5376ba92015-06-22 12:19:30 -0400167 PyErr_SetString(PyExc_RuntimeError, msg);
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400168 PyErr_Fetch(&exc, &val2, &tb);
169 PyErr_NormalizeException(&exc, &val2, &tb);
Yury Selivanov18c30a22015-05-10 15:09:46 -0400170 Py_INCREF(val);
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400171 PyException_SetCause(val2, val);
172 PyException_SetContext(val2, val);
173 PyErr_Restore(exc, val2, tb);
174 }
Yury Selivanov68333392015-05-22 11:16:47 -0400175 else {
176 PyObject *exc, *val, *tb;
177
178 /* Pop the exception before issuing a warning. */
179 PyErr_Fetch(&exc, &val, &tb);
180
181 if (PyErr_WarnFormat(PyExc_PendingDeprecationWarning, 1,
182 "generator '%.50S' raised StopIteration",
183 gen->gi_qualname)) {
184 /* Warning was converted to an error. */
185 Py_XDECREF(exc);
186 Py_XDECREF(val);
187 Py_XDECREF(tb);
188 }
189 else {
190 PyErr_Restore(exc, val, tb);
191 }
192 }
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400193 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200194
195 if (!result || f->f_stacktop == NULL) {
196 /* generator can't be rerun, so release the frame */
197 /* first clean reference cycle through stored exception traceback */
198 PyObject *t, *v, *tb;
199 t = f->f_exc_type;
200 v = f->f_exc_value;
201 tb = f->f_exc_traceback;
202 f->f_exc_type = NULL;
203 f->f_exc_value = NULL;
204 f->f_exc_traceback = NULL;
205 Py_XDECREF(t);
206 Py_XDECREF(v);
207 Py_XDECREF(tb);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200208 gen->gi_frame->f_gen = NULL;
Antoine Pitrou93963562013-05-14 20:37:52 +0200209 gen->gi_frame = NULL;
210 Py_DECREF(f);
211 }
212
213 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000214}
215
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000216PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217"send(arg) -> send 'arg' into generator,\n\
218return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000219
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500220PyObject *
221_PyGen_Send(PyGenObject *gen, PyObject *arg)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000222{
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500223 return gen_send_ex(gen, arg, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000224}
225
226PyDoc_STRVAR(close_doc,
Benjamin Petersonab3da292012-05-03 18:44:09 -0400227"close() -> raise GeneratorExit inside generator.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000228
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000229/*
230 * This helper function is used by gen_close and gen_throw to
231 * close a subiterator being delegated to by yield-from.
232 */
233
Antoine Pitrou93963562013-05-14 20:37:52 +0200234static int
235gen_close_iter(PyObject *yf)
236{
237 PyObject *retval = NULL;
238 _Py_IDENTIFIER(close);
239
240 if (PyGen_CheckExact(yf)) {
241 retval = gen_close((PyGenObject *)yf, NULL);
242 if (retval == NULL)
243 return -1;
244 } else {
245 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
246 if (meth == NULL) {
247 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
248 PyErr_WriteUnraisable(yf);
249 PyErr_Clear();
250 } else {
251 retval = PyObject_CallFunction(meth, "");
252 Py_DECREF(meth);
253 if (retval == NULL)
254 return -1;
255 }
256 }
257 Py_XDECREF(retval);
258 return 0;
259}
260
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500261static PyObject *
262gen_yf(PyGenObject *gen)
263{
Antoine Pitrou93963562013-05-14 20:37:52 +0200264 PyObject *yf = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500265 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200266
267 if (f && f->f_stacktop) {
268 PyObject *bytecode = f->f_code->co_code;
269 unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
270
271 if (code[f->f_lasti + 1] != YIELD_FROM)
272 return NULL;
273 yf = f->f_stacktop[-1];
274 Py_INCREF(yf);
275 }
276
277 return yf;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500278}
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000279
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000280static PyObject *
281gen_close(PyGenObject *gen, PyObject *args)
282{
Antoine Pitrou93963562013-05-14 20:37:52 +0200283 PyObject *retval;
284 PyObject *yf = gen_yf(gen);
285 int err = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000286
Antoine Pitrou93963562013-05-14 20:37:52 +0200287 if (yf) {
288 gen->gi_running = 1;
289 err = gen_close_iter(yf);
290 gen->gi_running = 0;
291 Py_DECREF(yf);
292 }
293 if (err == 0)
294 PyErr_SetNone(PyExc_GeneratorExit);
295 retval = gen_send_ex(gen, Py_None, 1);
296 if (retval) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400297 char *msg = "generator ignored GeneratorExit";
298 if (PyCoro_CheckExact(gen))
299 msg = "coroutine ignored GeneratorExit";
Antoine Pitrou93963562013-05-14 20:37:52 +0200300 Py_DECREF(retval);
Yury Selivanov5376ba92015-06-22 12:19:30 -0400301 PyErr_SetString(PyExc_RuntimeError, msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return NULL;
303 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200304 if (PyErr_ExceptionMatches(PyExc_StopIteration)
305 || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
306 PyErr_Clear(); /* ignore these errors */
307 Py_INCREF(Py_None);
308 return Py_None;
309 }
310 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000311}
312
Antoine Pitrou93963562013-05-14 20:37:52 +0200313
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000314PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
316return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000317
318static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319gen_throw(PyGenObject *gen, PyObject *args)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 PyObject *typ;
322 PyObject *tb = NULL;
323 PyObject *val = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500324 PyObject *yf = gen_yf(gen);
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000325 _Py_IDENTIFIER(throw);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
328 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000329
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000330 if (yf) {
331 PyObject *ret;
332 int err;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000333 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500334 gen->gi_running = 1;
Antoine Pitrou93963562013-05-14 20:37:52 +0200335 err = gen_close_iter(yf);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500336 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000337 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000338 if (err < 0)
339 return gen_send_ex(gen, Py_None, 1);
340 goto throw_here;
341 }
342 if (PyGen_CheckExact(yf)) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500343 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000344 ret = gen_throw((PyGenObject *)yf, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500345 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000346 } else {
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000347 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000348 if (meth == NULL) {
349 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
350 Py_DECREF(yf);
351 return NULL;
352 }
353 PyErr_Clear();
354 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000355 goto throw_here;
356 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500357 gen->gi_running = 1;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000358 ret = PyObject_CallObject(meth, args);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500359 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000360 Py_DECREF(meth);
361 }
362 Py_DECREF(yf);
363 if (!ret) {
364 PyObject *val;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500365 /* Pop subiterator from stack */
366 ret = *(--gen->gi_frame->f_stacktop);
367 assert(ret == yf);
368 Py_DECREF(ret);
369 /* Termination repetition of YIELD_FROM */
370 gen->gi_frame->f_lasti++;
Nick Coghlanc40bc092012-06-17 15:15:49 +1000371 if (_PyGen_FetchStopIterationValue(&val) == 0) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000372 ret = gen_send_ex(gen, val, 0);
373 Py_DECREF(val);
374 } else {
375 ret = gen_send_ex(gen, Py_None, 1);
376 }
377 }
378 return ret;
379 }
380
381throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 /* First, check the traceback argument, replacing None with
383 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400384 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 else if (tb != NULL && !PyTraceBack_Check(tb)) {
388 PyErr_SetString(PyExc_TypeError,
389 "throw() third argument must be a traceback object");
390 return NULL;
391 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_INCREF(typ);
394 Py_XINCREF(val);
395 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000396
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400397 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 else if (PyExceptionInstance_Check(typ)) {
401 /* Raising an instance. The value should be a dummy. */
402 if (val && val != Py_None) {
403 PyErr_SetString(PyExc_TypeError,
404 "instance exception may not have a separate value");
405 goto failed_throw;
406 }
407 else {
408 /* Normalize to raise <class>, <instance> */
409 Py_XDECREF(val);
410 val = typ;
411 typ = PyExceptionInstance_Class(typ);
412 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200413
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400414 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200415 /* Returns NULL if there's no traceback */
416 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
418 }
419 else {
420 /* Not something you can raise. throw() fails. */
421 PyErr_Format(PyExc_TypeError,
422 "exceptions must be classes or instances "
423 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000424 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 goto failed_throw;
426 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyErr_Restore(typ, val, tb);
429 return gen_send_ex(gen, Py_None, 1);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000430
431failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* Didn't use our arguments, so restore their original refcounts */
433 Py_DECREF(typ);
434 Py_XDECREF(val);
435 Py_XDECREF(tb);
436 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000437}
438
439
440static PyObject *
441gen_iternext(PyGenObject *gen)
442{
Antoine Pitrou667f5452014-07-08 18:43:23 -0400443 return gen_send_ex(gen, NULL, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000444}
445
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000446/*
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000447 * If StopIteration exception is set, fetches its 'value'
448 * attribute if any, otherwise sets pvalue to None.
449 *
450 * Returns 0 if no exception or StopIteration is set.
451 * If any other exception is set, returns -1 and leaves
452 * pvalue unchanged.
453 */
454
455int
Nick Coghlanc40bc092012-06-17 15:15:49 +1000456_PyGen_FetchStopIterationValue(PyObject **pvalue) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000457 PyObject *et, *ev, *tb;
458 PyObject *value = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500459
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000460 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
461 PyErr_Fetch(&et, &ev, &tb);
Antoine Pitrou7403e912015-04-26 18:46:40 +0200462 if (ev) {
463 /* exception will usually be normalised already */
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300464 if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200465 value = ((PyStopIterationObject *)ev)->value;
466 Py_INCREF(value);
467 Py_DECREF(ev);
468 } else if (et == PyExc_StopIteration) {
469 /* avoid normalisation and take ev as value */
470 value = ev;
471 } else {
472 /* normalisation required */
473 PyErr_NormalizeException(&et, &ev, &tb);
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300474 if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200475 PyErr_Restore(et, ev, tb);
476 return -1;
477 }
478 value = ((PyStopIterationObject *)ev)->value;
479 Py_INCREF(value);
480 Py_DECREF(ev);
481 }
482 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000483 Py_XDECREF(et);
484 Py_XDECREF(tb);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000485 } else if (PyErr_Occurred()) {
486 return -1;
487 }
488 if (value == NULL) {
489 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100490 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000491 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000492 *pvalue = value;
493 return 0;
494}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000495
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000496static PyObject *
497gen_repr(PyGenObject *gen)
498{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400499 return PyUnicode_FromFormat("<generator object %S at %p>",
500 gen->gi_qualname, gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000501}
502
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000503static PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200504gen_get_name(PyGenObject *op)
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000505{
Victor Stinner40ee3012014-06-16 15:59:28 +0200506 Py_INCREF(op->gi_name);
507 return op->gi_name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000508}
509
Victor Stinner40ee3012014-06-16 15:59:28 +0200510static int
511gen_set_name(PyGenObject *op, PyObject *value)
512{
513 PyObject *tmp;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000514
Victor Stinner40ee3012014-06-16 15:59:28 +0200515 /* Not legal to del gen.gi_name or to set it to anything
516 * other than a string object. */
517 if (value == NULL || !PyUnicode_Check(value)) {
518 PyErr_SetString(PyExc_TypeError,
519 "__name__ must be set to a string object");
520 return -1;
521 }
522 tmp = op->gi_name;
523 Py_INCREF(value);
524 op->gi_name = value;
525 Py_DECREF(tmp);
526 return 0;
527}
528
529static PyObject *
530gen_get_qualname(PyGenObject *op)
531{
532 Py_INCREF(op->gi_qualname);
533 return op->gi_qualname;
534}
535
536static int
537gen_set_qualname(PyGenObject *op, PyObject *value)
538{
539 PyObject *tmp;
540
541 /* Not legal to del gen.__qualname__ or to set it to anything
542 * other than a string object. */
543 if (value == NULL || !PyUnicode_Check(value)) {
544 PyErr_SetString(PyExc_TypeError,
545 "__qualname__ must be set to a string object");
546 return -1;
547 }
548 tmp = op->gi_qualname;
549 Py_INCREF(value);
550 op->gi_qualname = value;
551 Py_DECREF(tmp);
552 return 0;
553}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000554
Yury Selivanove13f8f32015-07-03 00:23:30 -0400555static PyObject *
556gen_getyieldfrom(PyGenObject *gen)
557{
558 PyObject *yf = gen_yf(gen);
559 if (yf == NULL)
560 Py_RETURN_NONE;
561 return yf;
562}
563
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000564static PyGetSetDef gen_getsetlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200565 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
566 PyDoc_STR("name of the generator")},
567 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
568 PyDoc_STR("qualified name of the generator")},
Yury Selivanove13f8f32015-07-03 00:23:30 -0400569 {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
570 PyDoc_STR("object being iterated by yield from, or None")},
Victor Stinner40ee3012014-06-16 15:59:28 +0200571 {NULL} /* Sentinel */
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000572};
573
Martin v. Löwise440e472004-06-01 15:22:42 +0000574static PyMemberDef gen_memberlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200575 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
576 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
577 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000579};
580
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000581static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500582 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
584 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
585 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000586};
587
Martin v. Löwise440e472004-06-01 15:22:42 +0000588PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyVarObject_HEAD_INIT(&PyType_Type, 0)
590 "generator", /* tp_name */
591 sizeof(PyGenObject), /* tp_basicsize */
592 0, /* tp_itemsize */
593 /* methods */
594 (destructor)gen_dealloc, /* tp_dealloc */
595 0, /* tp_print */
596 0, /* tp_getattr */
597 0, /* tp_setattr */
Yury Selivanov75445082015-05-11 22:57:16 -0400598 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 (reprfunc)gen_repr, /* tp_repr */
600 0, /* tp_as_number */
601 0, /* tp_as_sequence */
602 0, /* tp_as_mapping */
603 0, /* tp_hash */
604 0, /* tp_call */
605 0, /* tp_str */
606 PyObject_GenericGetAttr, /* tp_getattro */
607 0, /* tp_setattro */
608 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
610 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 0, /* tp_doc */
612 (traverseproc)gen_traverse, /* tp_traverse */
613 0, /* tp_clear */
614 0, /* tp_richcompare */
615 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
Yury Selivanov5376ba92015-06-22 12:19:30 -0400616 PyObject_SelfIter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 (iternextfunc)gen_iternext, /* tp_iternext */
618 gen_methods, /* tp_methods */
619 gen_memberlist, /* tp_members */
620 gen_getsetlist, /* tp_getset */
621 0, /* tp_base */
622 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 0, /* tp_descr_get */
625 0, /* tp_descr_set */
626 0, /* tp_dictoffset */
627 0, /* tp_init */
628 0, /* tp_alloc */
629 0, /* tp_new */
630 0, /* tp_free */
631 0, /* tp_is_gc */
632 0, /* tp_bases */
633 0, /* tp_mro */
634 0, /* tp_cache */
635 0, /* tp_subclasses */
636 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200637 0, /* tp_del */
638 0, /* tp_version_tag */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200639 _PyGen_Finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000640};
641
Yury Selivanov5376ba92015-06-22 12:19:30 -0400642static PyObject *
643gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
644 PyObject *name, PyObject *qualname)
Martin v. Löwise440e472004-06-01 15:22:42 +0000645{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400646 PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (gen == NULL) {
648 Py_DECREF(f);
649 return NULL;
650 }
651 gen->gi_frame = f;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200652 f->f_gen = (PyObject *) gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 Py_INCREF(f->f_code);
654 gen->gi_code = (PyObject *)(f->f_code);
655 gen->gi_running = 0;
656 gen->gi_weakreflist = NULL;
Victor Stinner40ee3012014-06-16 15:59:28 +0200657 if (name != NULL)
658 gen->gi_name = name;
659 else
660 gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
661 Py_INCREF(gen->gi_name);
662 if (qualname != NULL)
663 gen->gi_qualname = qualname;
664 else
665 gen->gi_qualname = gen->gi_name;
666 Py_INCREF(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 _PyObject_GC_TRACK(gen);
668 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000669}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670
Victor Stinner40ee3012014-06-16 15:59:28 +0200671PyObject *
Yury Selivanov5376ba92015-06-22 12:19:30 -0400672PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
673{
674 return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
675}
676
677PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200678PyGen_New(PyFrameObject *f)
679{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400680 return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
Victor Stinner40ee3012014-06-16 15:59:28 +0200681}
682
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683int
684PyGen_NeedsFinalizing(PyGenObject *gen)
685{
Antoine Pitrou93963562013-05-14 20:37:52 +0200686 int i;
687 PyFrameObject *f = gen->gi_frame;
688
689 if (f == NULL || f->f_stacktop == NULL)
690 return 0; /* no frame or empty blockstack == no finalization */
691
692 /* Any block type besides a loop requires cleanup. */
693 for (i = 0; i < f->f_iblock; i++)
694 if (f->f_blockstack[i].b_type != SETUP_LOOP)
695 return 1;
696
697 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000699}
Yury Selivanov75445082015-05-11 22:57:16 -0400700
Yury Selivanov5376ba92015-06-22 12:19:30 -0400701/* Coroutine Object */
702
703typedef struct {
704 PyObject_HEAD
705 PyCoroObject *cw_coroutine;
706} PyCoroWrapper;
707
708static int
709gen_is_coroutine(PyObject *o)
710{
711 if (PyGen_CheckExact(o)) {
712 PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
713 if (code->co_flags & CO_ITERABLE_COROUTINE) {
714 return 1;
715 }
716 }
717 return 0;
718}
719
Yury Selivanov75445082015-05-11 22:57:16 -0400720/*
721 * This helper function returns an awaitable for `o`:
722 * - `o` if `o` is a coroutine-object;
723 * - `type(o)->tp_as_async->am_await(o)`
724 *
725 * Raises a TypeError if it's not possible to return
726 * an awaitable and returns NULL.
727 */
728PyObject *
Yury Selivanov5376ba92015-06-22 12:19:30 -0400729_PyCoro_GetAwaitableIter(PyObject *o)
Yury Selivanov75445082015-05-11 22:57:16 -0400730{
Yury Selivanov6ef05902015-05-28 11:21:31 -0400731 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -0400732 PyTypeObject *ot;
733
Yury Selivanov5376ba92015-06-22 12:19:30 -0400734 if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
735 /* 'o' is a coroutine. */
Yury Selivanov75445082015-05-11 22:57:16 -0400736 Py_INCREF(o);
737 return o;
738 }
739
740 ot = Py_TYPE(o);
741 if (ot->tp_as_async != NULL) {
742 getter = ot->tp_as_async->am_await;
743 }
744 if (getter != NULL) {
745 PyObject *res = (*getter)(o);
746 if (res != NULL) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400747 if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
748 /* __await__ must return an *iterator*, not
749 a coroutine or another awaitable (see PEP 492) */
750 PyErr_SetString(PyExc_TypeError,
751 "__await__() returned a coroutine");
752 Py_CLEAR(res);
753 } else if (!PyIter_Check(res)) {
Yury Selivanov75445082015-05-11 22:57:16 -0400754 PyErr_Format(PyExc_TypeError,
755 "__await__() returned non-iterator "
756 "of type '%.100s'",
757 Py_TYPE(res)->tp_name);
758 Py_CLEAR(res);
759 }
Yury Selivanov75445082015-05-11 22:57:16 -0400760 }
761 return res;
762 }
763
764 PyErr_Format(PyExc_TypeError,
765 "object %.100s can't be used in 'await' expression",
766 ot->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -0400767 return NULL;
768}
Yury Selivanov5376ba92015-06-22 12:19:30 -0400769
770static PyObject *
771coro_repr(PyCoroObject *coro)
772{
773 return PyUnicode_FromFormat("<coroutine object %S at %p>",
774 coro->cr_qualname, coro);
775}
776
777static PyObject *
778coro_await(PyCoroObject *coro)
779{
780 PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
781 if (cw == NULL) {
782 return NULL;
783 }
784 Py_INCREF(coro);
785 cw->cw_coroutine = coro;
786 _PyObject_GC_TRACK(cw);
787 return (PyObject *)cw;
788}
789
Yury Selivanove13f8f32015-07-03 00:23:30 -0400790static PyObject *
791coro_get_cr_await(PyCoroObject *coro)
792{
793 PyObject *yf = gen_yf((PyGenObject *) coro);
794 if (yf == NULL)
795 Py_RETURN_NONE;
796 return yf;
797}
798
Yury Selivanov5376ba92015-06-22 12:19:30 -0400799static PyGetSetDef coro_getsetlist[] = {
800 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
801 PyDoc_STR("name of the coroutine")},
802 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
803 PyDoc_STR("qualified name of the coroutine")},
Yury Selivanove13f8f32015-07-03 00:23:30 -0400804 {"cr_await", (getter)coro_get_cr_await, NULL,
805 PyDoc_STR("object being awaited on, or None")},
Yury Selivanov5376ba92015-06-22 12:19:30 -0400806 {NULL} /* Sentinel */
807};
808
809static PyMemberDef coro_memberlist[] = {
810 {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
811 {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
812 {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
813 {NULL} /* Sentinel */
814};
815
816PyDoc_STRVAR(coro_send_doc,
817"send(arg) -> send 'arg' into coroutine,\n\
Yury Selivanov66f88282015-06-24 11:04:15 -0400818return next iterated value or raise StopIteration.");
Yury Selivanov5376ba92015-06-22 12:19:30 -0400819
820PyDoc_STRVAR(coro_throw_doc,
821"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
Yury Selivanov66f88282015-06-24 11:04:15 -0400822return next iterated value or raise StopIteration.");
Yury Selivanov5376ba92015-06-22 12:19:30 -0400823
824PyDoc_STRVAR(coro_close_doc,
825"close() -> raise GeneratorExit inside coroutine.");
826
827static PyMethodDef coro_methods[] = {
828 {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc},
829 {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
830 {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
831 {NULL, NULL} /* Sentinel */
832};
833
834static PyAsyncMethods coro_as_async = {
835 (unaryfunc)coro_await, /* am_await */
836 0, /* am_aiter */
837 0 /* am_anext */
838};
839
840PyTypeObject PyCoro_Type = {
841 PyVarObject_HEAD_INIT(&PyType_Type, 0)
842 "coroutine", /* tp_name */
843 sizeof(PyCoroObject), /* tp_basicsize */
844 0, /* tp_itemsize */
845 /* methods */
846 (destructor)gen_dealloc, /* tp_dealloc */
847 0, /* tp_print */
848 0, /* tp_getattr */
849 0, /* tp_setattr */
850 &coro_as_async, /* tp_as_async */
851 (reprfunc)coro_repr, /* tp_repr */
852 0, /* tp_as_number */
853 0, /* tp_as_sequence */
854 0, /* tp_as_mapping */
855 0, /* tp_hash */
856 0, /* tp_call */
857 0, /* tp_str */
858 PyObject_GenericGetAttr, /* tp_getattro */
859 0, /* tp_setattro */
860 0, /* tp_as_buffer */
861 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
862 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
863 0, /* tp_doc */
864 (traverseproc)gen_traverse, /* tp_traverse */
865 0, /* tp_clear */
866 0, /* tp_richcompare */
867 offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */
868 0, /* tp_iter */
869 0, /* tp_iternext */
870 coro_methods, /* tp_methods */
871 coro_memberlist, /* tp_members */
872 coro_getsetlist, /* tp_getset */
873 0, /* tp_base */
874 0, /* tp_dict */
875 0, /* tp_descr_get */
876 0, /* tp_descr_set */
877 0, /* tp_dictoffset */
878 0, /* tp_init */
879 0, /* tp_alloc */
880 0, /* tp_new */
881 0, /* tp_free */
882 0, /* tp_is_gc */
883 0, /* tp_bases */
884 0, /* tp_mro */
885 0, /* tp_cache */
886 0, /* tp_subclasses */
887 0, /* tp_weaklist */
888 0, /* tp_del */
889 0, /* tp_version_tag */
890 _PyGen_Finalize, /* tp_finalize */
891};
892
893static void
894coro_wrapper_dealloc(PyCoroWrapper *cw)
895{
896 _PyObject_GC_UNTRACK((PyObject *)cw);
897 Py_CLEAR(cw->cw_coroutine);
898 PyObject_GC_Del(cw);
899}
900
901static PyObject *
902coro_wrapper_iternext(PyCoroWrapper *cw)
903{
904 return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0);
905}
906
907static PyObject *
908coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
909{
910 return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0);
911}
912
913static PyObject *
914coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
915{
916 return gen_throw((PyGenObject *)cw->cw_coroutine, args);
917}
918
919static PyObject *
920coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
921{
922 return gen_close((PyGenObject *)cw->cw_coroutine, args);
923}
924
925static int
926coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
927{
928 Py_VISIT((PyObject *)cw->cw_coroutine);
929 return 0;
930}
931
932static PyMethodDef coro_wrapper_methods[] = {
Yury Selivanov66f88282015-06-24 11:04:15 -0400933 {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
934 {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
935 {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
Yury Selivanov5376ba92015-06-22 12:19:30 -0400936 {NULL, NULL} /* Sentinel */
937};
938
939PyTypeObject _PyCoroWrapper_Type = {
940 PyVarObject_HEAD_INIT(&PyType_Type, 0)
941 "coroutine_wrapper",
942 sizeof(PyCoroWrapper), /* tp_basicsize */
943 0, /* tp_itemsize */
944 (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */
945 0, /* tp_print */
946 0, /* tp_getattr */
947 0, /* tp_setattr */
948 0, /* tp_as_async */
949 0, /* tp_repr */
950 0, /* tp_as_number */
951 0, /* tp_as_sequence */
952 0, /* tp_as_mapping */
953 0, /* tp_hash */
954 0, /* tp_call */
955 0, /* tp_str */
956 PyObject_GenericGetAttr, /* tp_getattro */
957 0, /* tp_setattro */
958 0, /* tp_as_buffer */
959 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
960 "A wrapper object implementing __await__ for coroutines.",
961 (traverseproc)coro_wrapper_traverse, /* tp_traverse */
962 0, /* tp_clear */
963 0, /* tp_richcompare */
964 0, /* tp_weaklistoffset */
965 PyObject_SelfIter, /* tp_iter */
966 (iternextfunc)coro_wrapper_iternext, /* tp_iternext */
967 coro_wrapper_methods, /* tp_methods */
968 0, /* tp_members */
969 0, /* tp_getset */
970 0, /* tp_base */
971 0, /* tp_dict */
972 0, /* tp_descr_get */
973 0, /* tp_descr_set */
974 0, /* tp_dictoffset */
975 0, /* tp_init */
976 0, /* tp_alloc */
977 0, /* tp_new */
978 PyObject_Del, /* tp_free */
979};
980
981PyObject *
982PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
983{
984 return gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
985}