blob: 8c2213e5bf229aac5a993c1df11ad85ba6900b93 [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
Yury Selivanoveb636452016-09-08 22:01:51 -07008static PyObject *gen_close(PyGenObject *, PyObject *);
9static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
10static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
11
12static char *NON_INIT_CORO_MSG = "can't send non-None value to a "
13 "just-started coroutine";
14
15static char *ASYNC_GEN_IGNORED_EXIT_MSG =
16 "async generator ignored GeneratorExit";
Nick Coghlan1f7ce622012-01-13 21:43:40 +100017
Martin v. Löwise440e472004-06-01 15:22:42 +000018static int
19gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
20{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 Py_VISIT((PyObject *)gen->gi_frame);
22 Py_VISIT(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +020023 Py_VISIT(gen->gi_name);
24 Py_VISIT(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 return 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000026}
27
Antoine Pitrou58720d62013-08-05 23:26:40 +020028void
29_PyGen_Finalize(PyObject *self)
Antoine Pitrou796564c2013-07-30 19:59:21 +020030{
31 PyGenObject *gen = (PyGenObject *)self;
Benjamin Petersonb88db872016-09-07 08:46:59 -070032 PyObject *res = NULL;
Antoine Pitrou796564c2013-07-30 19:59:21 +020033 PyObject *error_type, *error_value, *error_traceback;
34
35 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
36 /* Generator isn't paused, so no need to close */
37 return;
38
Yury Selivanoveb636452016-09-08 22:01:51 -070039 if (PyAsyncGen_CheckExact(self)) {
40 PyAsyncGenObject *agen = (PyAsyncGenObject*)self;
41 PyObject *finalizer = agen->ag_finalizer;
42 if (finalizer && !agen->ag_closed) {
43 /* Save the current exception, if any. */
44 PyErr_Fetch(&error_type, &error_value, &error_traceback);
45
Victor Stinnerde4ae3d2016-12-04 22:59:09 +010046 res = PyObject_CallFunctionObjArgs(finalizer, self, NULL);
Yury Selivanoveb636452016-09-08 22:01:51 -070047
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 return;
56 }
57 }
58
Antoine Pitrou796564c2013-07-30 19:59:21 +020059 /* Save the current exception, if any. */
60 PyErr_Fetch(&error_type, &error_value, &error_traceback);
61
Benjamin Peterson2f40ed42016-09-05 10:14:54 -070062 /* If `gen` is a coroutine, and if it was never awaited on,
63 issue a RuntimeWarning. */
Benjamin Petersonb88db872016-09-07 08:46:59 -070064 if (gen->gi_code != NULL &&
65 ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
66 gen->gi_frame->f_lasti == -1) {
67 if (!error_value) {
68 PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
69 "coroutine '%.50S' was never awaited",
70 gen->gi_qualname);
71 }
Benjamin Peterson2f40ed42016-09-05 10:14:54 -070072 }
73 else {
74 res = gen_close(gen, NULL);
75 }
Antoine Pitrou796564c2013-07-30 19:59:21 +020076
Benjamin Petersonb88db872016-09-07 08:46:59 -070077 if (res == NULL) {
78 if (PyErr_Occurred())
79 PyErr_WriteUnraisable(self);
80 }
81 else {
Antoine Pitrou796564c2013-07-30 19:59:21 +020082 Py_DECREF(res);
Benjamin Petersonb88db872016-09-07 08:46:59 -070083 }
Antoine Pitrou796564c2013-07-30 19:59:21 +020084
85 /* Restore the saved exception. */
86 PyErr_Restore(error_type, error_value, error_traceback);
87}
88
89static void
Martin v. Löwise440e472004-06-01 15:22:42 +000090gen_dealloc(PyGenObject *gen)
91{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyObject *self = (PyObject *) gen;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 _PyObject_GC_UNTRACK(gen);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (gen->gi_weakreflist != NULL)
97 PyObject_ClearWeakRefs(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000098
Antoine Pitrou93963562013-05-14 20:37:52 +020099 _PyObject_GC_TRACK(self);
100
Antoine Pitrou796564c2013-07-30 19:59:21 +0200101 if (PyObject_CallFinalizerFromDealloc(self))
102 return; /* resurrected. :( */
Antoine Pitrou93963562013-05-14 20:37:52 +0200103
104 _PyObject_GC_UNTRACK(self);
Yury Selivanoveb636452016-09-08 22:01:51 -0700105 if (PyAsyncGen_CheckExact(gen)) {
106 /* We have to handle this case for asynchronous generators
107 right here, because this code has to be between UNTRACK
108 and GC_Del. */
109 Py_CLEAR(((PyAsyncGenObject*)gen)->ag_finalizer);
110 }
Benjamin Petersonbdddb112016-09-05 10:39:57 -0700111 if (gen->gi_frame != NULL) {
112 gen->gi_frame->f_gen = NULL;
113 Py_CLEAR(gen->gi_frame);
114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 Py_CLEAR(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +0200116 Py_CLEAR(gen->gi_name);
117 Py_CLEAR(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyObject_GC_Del(gen);
Martin v. Löwise440e472004-06-01 15:22:42 +0000119}
120
121static PyObject *
Yury Selivanov77c96812016-02-13 17:59:05 -0500122gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
Martin v. Löwise440e472004-06-01 15:22:42 +0000123{
Antoine Pitrou93963562013-05-14 20:37:52 +0200124 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200126 PyObject *result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000127
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500128 if (gen->gi_running) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400129 char *msg = "generator already executing";
Yury Selivanoveb636452016-09-08 22:01:51 -0700130 if (PyCoro_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400131 msg = "coroutine already executing";
Yury Selivanoveb636452016-09-08 22:01:51 -0700132 }
133 else if (PyAsyncGen_CheckExact(gen)) {
134 msg = "async generator already executing";
135 }
Yury Selivanov5376ba92015-06-22 12:19:30 -0400136 PyErr_SetString(PyExc_ValueError, msg);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500137 return NULL;
138 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200139 if (f == NULL || f->f_stacktop == NULL) {
Yury Selivanov77c96812016-02-13 17:59:05 -0500140 if (PyCoro_CheckExact(gen) && !closing) {
141 /* `gen` is an exhausted coroutine: raise an error,
142 except when called from gen_close(), which should
143 always be a silent method. */
144 PyErr_SetString(
145 PyExc_RuntimeError,
146 "cannot reuse already awaited coroutine");
Yury Selivanoveb636452016-09-08 22:01:51 -0700147 }
148 else if (arg && !exc) {
Yury Selivanov77c96812016-02-13 17:59:05 -0500149 /* `gen` is an exhausted generator:
150 only set exception if called from send(). */
Yury Selivanoveb636452016-09-08 22:01:51 -0700151 if (PyAsyncGen_CheckExact(gen)) {
152 PyErr_SetNone(PyExc_StopAsyncIteration);
153 }
154 else {
155 PyErr_SetNone(PyExc_StopIteration);
156 }
Yury Selivanov77c96812016-02-13 17:59:05 -0500157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 return NULL;
159 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000160
Antoine Pitrou93963562013-05-14 20:37:52 +0200161 if (f->f_lasti == -1) {
162 if (arg && arg != Py_None) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400163 char *msg = "can't send non-None value to a "
164 "just-started generator";
Yury Selivanoveb636452016-09-08 22:01:51 -0700165 if (PyCoro_CheckExact(gen)) {
166 msg = NON_INIT_CORO_MSG;
167 }
168 else if (PyAsyncGen_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400169 msg = "can't send non-None value to a "
Yury Selivanoveb636452016-09-08 22:01:51 -0700170 "just-started async generator";
171 }
Yury Selivanov5376ba92015-06-22 12:19:30 -0400172 PyErr_SetString(PyExc_TypeError, msg);
Antoine Pitrou93963562013-05-14 20:37:52 +0200173 return NULL;
174 }
175 } else {
176 /* Push arg onto the frame's value stack */
177 result = arg ? arg : Py_None;
178 Py_INCREF(result);
179 *(f->f_stacktop++) = result;
180 }
181
182 /* Generators always return to their most recent caller, not
183 * necessarily their creator. */
184 Py_XINCREF(tstate->frame);
185 assert(f->f_back == NULL);
186 f->f_back = tstate->frame;
187
188 gen->gi_running = 1;
Victor Stinner59a73272016-12-09 18:51:13 +0100189 result = PyEval_EvalFrameEx(f, exc);
Antoine Pitrou93963562013-05-14 20:37:52 +0200190 gen->gi_running = 0;
191
192 /* Don't keep the reference to f_back any longer than necessary. It
193 * may keep a chain of frames alive or it could create a reference
194 * cycle. */
195 assert(f->f_back == tstate->frame);
196 Py_CLEAR(f->f_back);
197
198 /* If the generator just returned (as opposed to yielding), signal
199 * that the generator is exhausted. */
200 if (result && f->f_stacktop == NULL) {
201 if (result == Py_None) {
202 /* Delay exception instantiation if we can */
Yury Selivanoveb636452016-09-08 22:01:51 -0700203 if (PyAsyncGen_CheckExact(gen)) {
204 PyErr_SetNone(PyExc_StopAsyncIteration);
205 }
206 else {
207 PyErr_SetNone(PyExc_StopIteration);
208 }
209 }
210 else {
Yury Selivanoveb636452016-09-08 22:01:51 -0700211 /* Async generators cannot return anything but None */
212 assert(!PyAsyncGen_CheckExact(gen));
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200213 _PyGen_SetStopIterationValue(result);
Antoine Pitrou93963562013-05-14 20:37:52 +0200214 }
215 Py_CLEAR(result);
216 }
Yury Selivanov68333392015-05-22 11:16:47 -0400217 else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400218 /* Check for __future__ generator_stop and conditionally turn
219 * a leaking StopIteration into RuntimeError (with its cause
220 * set appropriately). */
Yury Selivanoveb636452016-09-08 22:01:51 -0700221
222 const int check_stop_iter_error_flags = CO_FUTURE_GENERATOR_STOP |
223 CO_COROUTINE |
224 CO_ITERABLE_COROUTINE |
225 CO_ASYNC_GENERATOR;
226
227 if (gen->gi_code != NULL &&
228 ((PyCodeObject *)gen->gi_code)->co_flags &
229 check_stop_iter_error_flags)
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400230 {
Yury Selivanoveb636452016-09-08 22:01:51 -0700231 /* `gen` is either:
232 * a generator with CO_FUTURE_GENERATOR_STOP flag;
233 * a coroutine;
234 * a generator with CO_ITERABLE_COROUTINE flag
235 (decorated with types.coroutine decorator);
236 * an async generator.
237 */
238 const char *msg = "generator raised StopIteration";
239 if (PyCoro_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400240 msg = "coroutine raised StopIteration";
Yury Selivanoveb636452016-09-08 22:01:51 -0700241 }
242 else if PyAsyncGen_CheckExact(gen) {
243 msg = "async generator raised StopIteration";
244 }
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300245 _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400246 }
Yury Selivanov68333392015-05-22 11:16:47 -0400247 else {
Yury Selivanoveb636452016-09-08 22:01:51 -0700248 /* `gen` is an ordinary generator without
249 CO_FUTURE_GENERATOR_STOP flag.
250 */
251
Yury Selivanov68333392015-05-22 11:16:47 -0400252 PyObject *exc, *val, *tb;
253
254 /* Pop the exception before issuing a warning. */
255 PyErr_Fetch(&exc, &val, &tb);
256
Martin Panter7e3a91a2016-02-10 04:40:48 +0000257 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
Yury Selivanov68333392015-05-22 11:16:47 -0400258 "generator '%.50S' raised StopIteration",
259 gen->gi_qualname)) {
260 /* Warning was converted to an error. */
261 Py_XDECREF(exc);
262 Py_XDECREF(val);
263 Py_XDECREF(tb);
264 }
265 else {
266 PyErr_Restore(exc, val, tb);
267 }
268 }
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400269 }
Yury Selivanoveb636452016-09-08 22:01:51 -0700270 else if (PyAsyncGen_CheckExact(gen) && !result &&
271 PyErr_ExceptionMatches(PyExc_StopAsyncIteration))
272 {
273 /* code in `gen` raised a StopAsyncIteration error:
274 raise a RuntimeError.
275 */
276 const char *msg = "async generator raised StopAsyncIteration";
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300277 _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
Yury Selivanoveb636452016-09-08 22:01:51 -0700278 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200279
280 if (!result || f->f_stacktop == NULL) {
281 /* generator can't be rerun, so release the frame */
282 /* first clean reference cycle through stored exception traceback */
283 PyObject *t, *v, *tb;
284 t = f->f_exc_type;
285 v = f->f_exc_value;
286 tb = f->f_exc_traceback;
287 f->f_exc_type = NULL;
288 f->f_exc_value = NULL;
289 f->f_exc_traceback = NULL;
290 Py_XDECREF(t);
291 Py_XDECREF(v);
292 Py_XDECREF(tb);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200293 gen->gi_frame->f_gen = NULL;
Antoine Pitrou93963562013-05-14 20:37:52 +0200294 gen->gi_frame = NULL;
295 Py_DECREF(f);
296 }
297
298 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000299}
300
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000301PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302"send(arg) -> send 'arg' into generator,\n\
303return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000304
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500305PyObject *
306_PyGen_Send(PyGenObject *gen, PyObject *arg)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000307{
Yury Selivanov77c96812016-02-13 17:59:05 -0500308 return gen_send_ex(gen, arg, 0, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000309}
310
311PyDoc_STRVAR(close_doc,
Benjamin Petersonab3da292012-05-03 18:44:09 -0400312"close() -> raise GeneratorExit inside generator.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000313
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000314/*
315 * This helper function is used by gen_close and gen_throw to
316 * close a subiterator being delegated to by yield-from.
317 */
318
Antoine Pitrou93963562013-05-14 20:37:52 +0200319static int
320gen_close_iter(PyObject *yf)
321{
322 PyObject *retval = NULL;
323 _Py_IDENTIFIER(close);
324
Yury Selivanoveb636452016-09-08 22:01:51 -0700325 if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
Antoine Pitrou93963562013-05-14 20:37:52 +0200326 retval = gen_close((PyGenObject *)yf, NULL);
327 if (retval == NULL)
328 return -1;
Yury Selivanoveb636452016-09-08 22:01:51 -0700329 }
330 else {
Antoine Pitrou93963562013-05-14 20:37:52 +0200331 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
332 if (meth == NULL) {
333 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
334 PyErr_WriteUnraisable(yf);
335 PyErr_Clear();
Yury Selivanoveb636452016-09-08 22:01:51 -0700336 }
337 else {
Victor Stinner3466bde2016-09-05 18:16:01 -0700338 retval = _PyObject_CallNoArg(meth);
Antoine Pitrou93963562013-05-14 20:37:52 +0200339 Py_DECREF(meth);
340 if (retval == NULL)
341 return -1;
342 }
343 }
344 Py_XDECREF(retval);
345 return 0;
346}
347
Yury Selivanovc724bae2016-03-02 11:30:46 -0500348PyObject *
349_PyGen_yf(PyGenObject *gen)
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500350{
Antoine Pitrou93963562013-05-14 20:37:52 +0200351 PyObject *yf = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500352 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200353
354 if (f && f->f_stacktop) {
355 PyObject *bytecode = f->f_code->co_code;
356 unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
357
Victor Stinnerf7d199f2016-11-24 22:33:01 +0100358 if (f->f_lasti < 0) {
359 /* Return immediately if the frame didn't start yet. YIELD_FROM
360 always come after LOAD_CONST: a code object should not start
361 with YIELD_FROM */
362 assert(code[0] != YIELD_FROM);
363 return NULL;
364 }
365
Serhiy Storchakaab874002016-09-11 13:48:15 +0300366 if (code[f->f_lasti + sizeof(_Py_CODEUNIT)] != YIELD_FROM)
Antoine Pitrou93963562013-05-14 20:37:52 +0200367 return NULL;
368 yf = f->f_stacktop[-1];
369 Py_INCREF(yf);
370 }
371
372 return yf;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500373}
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000374
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000375static PyObject *
376gen_close(PyGenObject *gen, PyObject *args)
377{
Antoine Pitrou93963562013-05-14 20:37:52 +0200378 PyObject *retval;
Yury Selivanovc724bae2016-03-02 11:30:46 -0500379 PyObject *yf = _PyGen_yf(gen);
Antoine Pitrou93963562013-05-14 20:37:52 +0200380 int err = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000381
Antoine Pitrou93963562013-05-14 20:37:52 +0200382 if (yf) {
383 gen->gi_running = 1;
384 err = gen_close_iter(yf);
385 gen->gi_running = 0;
386 Py_DECREF(yf);
387 }
388 if (err == 0)
389 PyErr_SetNone(PyExc_GeneratorExit);
Yury Selivanov77c96812016-02-13 17:59:05 -0500390 retval = gen_send_ex(gen, Py_None, 1, 1);
Antoine Pitrou93963562013-05-14 20:37:52 +0200391 if (retval) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400392 char *msg = "generator ignored GeneratorExit";
Yury Selivanoveb636452016-09-08 22:01:51 -0700393 if (PyCoro_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400394 msg = "coroutine ignored GeneratorExit";
Yury Selivanoveb636452016-09-08 22:01:51 -0700395 } else if (PyAsyncGen_CheckExact(gen)) {
396 msg = ASYNC_GEN_IGNORED_EXIT_MSG;
397 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200398 Py_DECREF(retval);
Yury Selivanov5376ba92015-06-22 12:19:30 -0400399 PyErr_SetString(PyExc_RuntimeError, msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 return NULL;
401 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200402 if (PyErr_ExceptionMatches(PyExc_StopIteration)
403 || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
404 PyErr_Clear(); /* ignore these errors */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200405 Py_RETURN_NONE;
Antoine Pitrou93963562013-05-14 20:37:52 +0200406 }
407 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000408}
409
Antoine Pitrou93963562013-05-14 20:37:52 +0200410
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000411PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
413return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000414
415static PyObject *
Yury Selivanoveb636452016-09-08 22:01:51 -0700416_gen_throw(PyGenObject *gen, int close_on_genexit,
417 PyObject *typ, PyObject *val, PyObject *tb)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000418{
Yury Selivanovc724bae2016-03-02 11:30:46 -0500419 PyObject *yf = _PyGen_yf(gen);
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000420 _Py_IDENTIFIER(throw);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000421
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000422 if (yf) {
423 PyObject *ret;
424 int err;
Yury Selivanoveb636452016-09-08 22:01:51 -0700425 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
426 close_on_genexit
427 ) {
428 /* Asynchronous generators *should not* be closed right away.
429 We have to allow some awaits to work it through, hence the
430 `close_on_genexit` parameter here.
431 */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500432 gen->gi_running = 1;
Antoine Pitrou93963562013-05-14 20:37:52 +0200433 err = gen_close_iter(yf);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500434 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000435 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000436 if (err < 0)
Yury Selivanov77c96812016-02-13 17:59:05 -0500437 return gen_send_ex(gen, Py_None, 1, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000438 goto throw_here;
439 }
Yury Selivanoveb636452016-09-08 22:01:51 -0700440 if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
441 /* `yf` is a generator or a coroutine. */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500442 gen->gi_running = 1;
Yury Selivanoveb636452016-09-08 22:01:51 -0700443 /* Close the generator that we are currently iterating with
444 'yield from' or awaiting on with 'await'. */
445 ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
446 typ, val, tb);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500447 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000448 } else {
Yury Selivanoveb636452016-09-08 22:01:51 -0700449 /* `yf` is an iterator or a coroutine-like object. */
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000450 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000451 if (meth == NULL) {
452 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
453 Py_DECREF(yf);
454 return NULL;
455 }
456 PyErr_Clear();
457 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000458 goto throw_here;
459 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500460 gen->gi_running = 1;
Yury Selivanoveb636452016-09-08 22:01:51 -0700461 ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500462 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000463 Py_DECREF(meth);
464 }
465 Py_DECREF(yf);
466 if (!ret) {
467 PyObject *val;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500468 /* Pop subiterator from stack */
469 ret = *(--gen->gi_frame->f_stacktop);
470 assert(ret == yf);
471 Py_DECREF(ret);
472 /* Termination repetition of YIELD_FROM */
Victor Stinnerf7d199f2016-11-24 22:33:01 +0100473 assert(gen->gi_frame->f_lasti >= 0);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300474 gen->gi_frame->f_lasti += sizeof(_Py_CODEUNIT);
Nick Coghlanc40bc092012-06-17 15:15:49 +1000475 if (_PyGen_FetchStopIterationValue(&val) == 0) {
Yury Selivanov77c96812016-02-13 17:59:05 -0500476 ret = gen_send_ex(gen, val, 0, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000477 Py_DECREF(val);
478 } else {
Yury Selivanov77c96812016-02-13 17:59:05 -0500479 ret = gen_send_ex(gen, Py_None, 1, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000480 }
481 }
482 return ret;
483 }
484
485throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 /* First, check the traceback argument, replacing None with
487 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400488 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 else if (tb != NULL && !PyTraceBack_Check(tb)) {
492 PyErr_SetString(PyExc_TypeError,
493 "throw() third argument must be a traceback object");
494 return NULL;
495 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_INCREF(typ);
498 Py_XINCREF(val);
499 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000500
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400501 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 else if (PyExceptionInstance_Check(typ)) {
505 /* Raising an instance. The value should be a dummy. */
506 if (val && val != Py_None) {
507 PyErr_SetString(PyExc_TypeError,
508 "instance exception may not have a separate value");
509 goto failed_throw;
510 }
511 else {
512 /* Normalize to raise <class>, <instance> */
513 Py_XDECREF(val);
514 val = typ;
515 typ = PyExceptionInstance_Class(typ);
516 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200517
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400518 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200519 /* Returns NULL if there's no traceback */
520 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 }
522 }
523 else {
524 /* Not something you can raise. throw() fails. */
525 PyErr_Format(PyExc_TypeError,
526 "exceptions must be classes or instances "
527 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000528 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 goto failed_throw;
530 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyErr_Restore(typ, val, tb);
Yury Selivanov77c96812016-02-13 17:59:05 -0500533 return gen_send_ex(gen, Py_None, 1, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000534
535failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* Didn't use our arguments, so restore their original refcounts */
537 Py_DECREF(typ);
538 Py_XDECREF(val);
539 Py_XDECREF(tb);
540 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000541}
542
543
544static PyObject *
Yury Selivanoveb636452016-09-08 22:01:51 -0700545gen_throw(PyGenObject *gen, PyObject *args)
546{
547 PyObject *typ;
548 PyObject *tb = NULL;
549 PyObject *val = NULL;
550
551 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) {
552 return NULL;
553 }
554
555 return _gen_throw(gen, 1, typ, val, tb);
556}
557
558
559static PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000560gen_iternext(PyGenObject *gen)
561{
Yury Selivanov77c96812016-02-13 17:59:05 -0500562 return gen_send_ex(gen, NULL, 0, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000563}
564
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000565/*
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200566 * Set StopIteration with specified value. Value can be arbitrary object
567 * or NULL.
568 *
569 * Returns 0 if StopIteration is set and -1 if any other exception is set.
570 */
571int
572_PyGen_SetStopIterationValue(PyObject *value)
573{
574 PyObject *e;
575
576 if (value == NULL ||
Yury Selivanovb7c91502017-03-12 15:53:07 -0400577 (!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200578 {
579 /* Delay exception instantiation if we can */
580 PyErr_SetObject(PyExc_StopIteration, value);
581 return 0;
582 }
583 /* Construct an exception instance manually with
584 * PyObject_CallFunctionObjArgs and pass it to PyErr_SetObject.
585 *
586 * We do this to handle a situation when "value" is a tuple, in which
587 * case PyErr_SetObject would set the value of StopIteration to
588 * the first element of the tuple.
589 *
590 * (See PyErr_SetObject/_PyErr_CreateException code for details.)
591 */
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100592 e = PyObject_CallFunctionObjArgs(PyExc_StopIteration, value, NULL);
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200593 if (e == NULL) {
594 return -1;
595 }
596 PyErr_SetObject(PyExc_StopIteration, e);
597 Py_DECREF(e);
598 return 0;
599}
600
601/*
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000602 * If StopIteration exception is set, fetches its 'value'
603 * attribute if any, otherwise sets pvalue to None.
604 *
605 * Returns 0 if no exception or StopIteration is set.
606 * If any other exception is set, returns -1 and leaves
607 * pvalue unchanged.
608 */
609
610int
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200611_PyGen_FetchStopIterationValue(PyObject **pvalue)
612{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000613 PyObject *et, *ev, *tb;
614 PyObject *value = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500615
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000616 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
617 PyErr_Fetch(&et, &ev, &tb);
Antoine Pitrou7403e912015-04-26 18:46:40 +0200618 if (ev) {
619 /* exception will usually be normalised already */
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300620 if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200621 value = ((PyStopIterationObject *)ev)->value;
622 Py_INCREF(value);
623 Py_DECREF(ev);
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200624 } else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) {
625 /* Avoid normalisation and take ev as value.
626 *
627 * Normalization is required if the value is a tuple, in
628 * that case the value of StopIteration would be set to
629 * the first element of the tuple.
630 *
631 * (See _PyErr_CreateException code for details.)
632 */
Antoine Pitrou7403e912015-04-26 18:46:40 +0200633 value = ev;
634 } else {
635 /* normalisation required */
636 PyErr_NormalizeException(&et, &ev, &tb);
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300637 if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200638 PyErr_Restore(et, ev, tb);
639 return -1;
640 }
641 value = ((PyStopIterationObject *)ev)->value;
642 Py_INCREF(value);
643 Py_DECREF(ev);
644 }
645 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000646 Py_XDECREF(et);
647 Py_XDECREF(tb);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000648 } else if (PyErr_Occurred()) {
649 return -1;
650 }
651 if (value == NULL) {
652 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100653 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000654 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000655 *pvalue = value;
656 return 0;
657}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000658
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000659static PyObject *
660gen_repr(PyGenObject *gen)
661{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400662 return PyUnicode_FromFormat("<generator object %S at %p>",
663 gen->gi_qualname, gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000664}
665
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000666static PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200667gen_get_name(PyGenObject *op)
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000668{
Victor Stinner40ee3012014-06-16 15:59:28 +0200669 Py_INCREF(op->gi_name);
670 return op->gi_name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000671}
672
Victor Stinner40ee3012014-06-16 15:59:28 +0200673static int
674gen_set_name(PyGenObject *op, PyObject *value)
675{
Victor Stinner40ee3012014-06-16 15:59:28 +0200676 /* Not legal to del gen.gi_name or to set it to anything
677 * other than a string object. */
678 if (value == NULL || !PyUnicode_Check(value)) {
679 PyErr_SetString(PyExc_TypeError,
680 "__name__ must be set to a string object");
681 return -1;
682 }
Victor Stinner40ee3012014-06-16 15:59:28 +0200683 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300684 Py_XSETREF(op->gi_name, value);
Victor Stinner40ee3012014-06-16 15:59:28 +0200685 return 0;
686}
687
688static PyObject *
689gen_get_qualname(PyGenObject *op)
690{
691 Py_INCREF(op->gi_qualname);
692 return op->gi_qualname;
693}
694
695static int
696gen_set_qualname(PyGenObject *op, PyObject *value)
697{
Victor Stinner40ee3012014-06-16 15:59:28 +0200698 /* Not legal to del gen.__qualname__ or to set it to anything
699 * other than a string object. */
700 if (value == NULL || !PyUnicode_Check(value)) {
701 PyErr_SetString(PyExc_TypeError,
702 "__qualname__ must be set to a string object");
703 return -1;
704 }
Victor Stinner40ee3012014-06-16 15:59:28 +0200705 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300706 Py_XSETREF(op->gi_qualname, value);
Victor Stinner40ee3012014-06-16 15:59:28 +0200707 return 0;
708}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000709
Yury Selivanove13f8f32015-07-03 00:23:30 -0400710static PyObject *
711gen_getyieldfrom(PyGenObject *gen)
712{
Yury Selivanovc724bae2016-03-02 11:30:46 -0500713 PyObject *yf = _PyGen_yf(gen);
Yury Selivanove13f8f32015-07-03 00:23:30 -0400714 if (yf == NULL)
715 Py_RETURN_NONE;
716 return yf;
717}
718
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000719static PyGetSetDef gen_getsetlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200720 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
721 PyDoc_STR("name of the generator")},
722 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
723 PyDoc_STR("qualified name of the generator")},
Yury Selivanove13f8f32015-07-03 00:23:30 -0400724 {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
725 PyDoc_STR("object being iterated by yield from, or None")},
Victor Stinner40ee3012014-06-16 15:59:28 +0200726 {NULL} /* Sentinel */
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000727};
728
Martin v. Löwise440e472004-06-01 15:22:42 +0000729static PyMemberDef gen_memberlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200730 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
731 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
732 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000734};
735
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000736static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500737 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
739 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
740 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000741};
742
Martin v. Löwise440e472004-06-01 15:22:42 +0000743PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 PyVarObject_HEAD_INIT(&PyType_Type, 0)
745 "generator", /* tp_name */
746 sizeof(PyGenObject), /* tp_basicsize */
747 0, /* tp_itemsize */
748 /* methods */
749 (destructor)gen_dealloc, /* tp_dealloc */
750 0, /* tp_print */
751 0, /* tp_getattr */
752 0, /* tp_setattr */
Yury Selivanov75445082015-05-11 22:57:16 -0400753 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 (reprfunc)gen_repr, /* tp_repr */
755 0, /* tp_as_number */
756 0, /* tp_as_sequence */
757 0, /* tp_as_mapping */
758 0, /* tp_hash */
759 0, /* tp_call */
760 0, /* tp_str */
761 PyObject_GenericGetAttr, /* tp_getattro */
762 0, /* tp_setattro */
763 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200764 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
765 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 0, /* tp_doc */
767 (traverseproc)gen_traverse, /* tp_traverse */
768 0, /* tp_clear */
769 0, /* tp_richcompare */
770 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
Yury Selivanov5376ba92015-06-22 12:19:30 -0400771 PyObject_SelfIter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 (iternextfunc)gen_iternext, /* tp_iternext */
773 gen_methods, /* tp_methods */
774 gen_memberlist, /* tp_members */
775 gen_getsetlist, /* tp_getset */
776 0, /* tp_base */
777 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 0, /* tp_descr_get */
780 0, /* tp_descr_set */
781 0, /* tp_dictoffset */
782 0, /* tp_init */
783 0, /* tp_alloc */
784 0, /* tp_new */
785 0, /* tp_free */
786 0, /* tp_is_gc */
787 0, /* tp_bases */
788 0, /* tp_mro */
789 0, /* tp_cache */
790 0, /* tp_subclasses */
791 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200792 0, /* tp_del */
793 0, /* tp_version_tag */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200794 _PyGen_Finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000795};
796
Yury Selivanov5376ba92015-06-22 12:19:30 -0400797static PyObject *
798gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
799 PyObject *name, PyObject *qualname)
Martin v. Löwise440e472004-06-01 15:22:42 +0000800{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400801 PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (gen == NULL) {
803 Py_DECREF(f);
804 return NULL;
805 }
806 gen->gi_frame = f;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200807 f->f_gen = (PyObject *) gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 Py_INCREF(f->f_code);
809 gen->gi_code = (PyObject *)(f->f_code);
810 gen->gi_running = 0;
811 gen->gi_weakreflist = NULL;
Victor Stinner40ee3012014-06-16 15:59:28 +0200812 if (name != NULL)
813 gen->gi_name = name;
814 else
815 gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
816 Py_INCREF(gen->gi_name);
817 if (qualname != NULL)
818 gen->gi_qualname = qualname;
819 else
820 gen->gi_qualname = gen->gi_name;
821 Py_INCREF(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 _PyObject_GC_TRACK(gen);
823 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000824}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825
Victor Stinner40ee3012014-06-16 15:59:28 +0200826PyObject *
Yury Selivanov5376ba92015-06-22 12:19:30 -0400827PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
828{
829 return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
830}
831
832PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200833PyGen_New(PyFrameObject *f)
834{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400835 return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
Victor Stinner40ee3012014-06-16 15:59:28 +0200836}
837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838int
839PyGen_NeedsFinalizing(PyGenObject *gen)
840{
Antoine Pitrou93963562013-05-14 20:37:52 +0200841 int i;
842 PyFrameObject *f = gen->gi_frame;
843
844 if (f == NULL || f->f_stacktop == NULL)
845 return 0; /* no frame or empty blockstack == no finalization */
846
847 /* Any block type besides a loop requires cleanup. */
848 for (i = 0; i < f->f_iblock; i++)
849 if (f->f_blockstack[i].b_type != SETUP_LOOP)
850 return 1;
851
852 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854}
Yury Selivanov75445082015-05-11 22:57:16 -0400855
Yury Selivanov5376ba92015-06-22 12:19:30 -0400856/* Coroutine Object */
857
858typedef struct {
859 PyObject_HEAD
860 PyCoroObject *cw_coroutine;
861} PyCoroWrapper;
862
863static int
864gen_is_coroutine(PyObject *o)
865{
866 if (PyGen_CheckExact(o)) {
867 PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
868 if (code->co_flags & CO_ITERABLE_COROUTINE) {
869 return 1;
870 }
871 }
872 return 0;
873}
874
Yury Selivanov75445082015-05-11 22:57:16 -0400875/*
876 * This helper function returns an awaitable for `o`:
877 * - `o` if `o` is a coroutine-object;
878 * - `type(o)->tp_as_async->am_await(o)`
879 *
880 * Raises a TypeError if it's not possible to return
881 * an awaitable and returns NULL.
882 */
883PyObject *
Yury Selivanov5376ba92015-06-22 12:19:30 -0400884_PyCoro_GetAwaitableIter(PyObject *o)
Yury Selivanov75445082015-05-11 22:57:16 -0400885{
Yury Selivanov6ef05902015-05-28 11:21:31 -0400886 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -0400887 PyTypeObject *ot;
888
Yury Selivanov5376ba92015-06-22 12:19:30 -0400889 if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
890 /* 'o' is a coroutine. */
Yury Selivanov75445082015-05-11 22:57:16 -0400891 Py_INCREF(o);
892 return o;
893 }
894
895 ot = Py_TYPE(o);
896 if (ot->tp_as_async != NULL) {
897 getter = ot->tp_as_async->am_await;
898 }
899 if (getter != NULL) {
900 PyObject *res = (*getter)(o);
901 if (res != NULL) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400902 if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
903 /* __await__ must return an *iterator*, not
904 a coroutine or another awaitable (see PEP 492) */
905 PyErr_SetString(PyExc_TypeError,
906 "__await__() returned a coroutine");
907 Py_CLEAR(res);
908 } else if (!PyIter_Check(res)) {
Yury Selivanov75445082015-05-11 22:57:16 -0400909 PyErr_Format(PyExc_TypeError,
910 "__await__() returned non-iterator "
911 "of type '%.100s'",
912 Py_TYPE(res)->tp_name);
913 Py_CLEAR(res);
914 }
Yury Selivanov75445082015-05-11 22:57:16 -0400915 }
916 return res;
917 }
918
919 PyErr_Format(PyExc_TypeError,
920 "object %.100s can't be used in 'await' expression",
921 ot->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -0400922 return NULL;
923}
Yury Selivanov5376ba92015-06-22 12:19:30 -0400924
925static PyObject *
926coro_repr(PyCoroObject *coro)
927{
928 return PyUnicode_FromFormat("<coroutine object %S at %p>",
929 coro->cr_qualname, coro);
930}
931
932static PyObject *
933coro_await(PyCoroObject *coro)
934{
935 PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
936 if (cw == NULL) {
937 return NULL;
938 }
939 Py_INCREF(coro);
940 cw->cw_coroutine = coro;
941 _PyObject_GC_TRACK(cw);
942 return (PyObject *)cw;
943}
944
Yury Selivanove13f8f32015-07-03 00:23:30 -0400945static PyObject *
946coro_get_cr_await(PyCoroObject *coro)
947{
Yury Selivanovc724bae2016-03-02 11:30:46 -0500948 PyObject *yf = _PyGen_yf((PyGenObject *) coro);
Yury Selivanove13f8f32015-07-03 00:23:30 -0400949 if (yf == NULL)
950 Py_RETURN_NONE;
951 return yf;
952}
953
Yury Selivanov5376ba92015-06-22 12:19:30 -0400954static PyGetSetDef coro_getsetlist[] = {
955 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
956 PyDoc_STR("name of the coroutine")},
957 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
958 PyDoc_STR("qualified name of the coroutine")},
Yury Selivanove13f8f32015-07-03 00:23:30 -0400959 {"cr_await", (getter)coro_get_cr_await, NULL,
960 PyDoc_STR("object being awaited on, or None")},
Yury Selivanov5376ba92015-06-22 12:19:30 -0400961 {NULL} /* Sentinel */
962};
963
964static PyMemberDef coro_memberlist[] = {
965 {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
966 {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
967 {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
968 {NULL} /* Sentinel */
969};
970
971PyDoc_STRVAR(coro_send_doc,
972"send(arg) -> send 'arg' into coroutine,\n\
Yury Selivanov66f88282015-06-24 11:04:15 -0400973return next iterated value or raise StopIteration.");
Yury Selivanov5376ba92015-06-22 12:19:30 -0400974
975PyDoc_STRVAR(coro_throw_doc,
976"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
Yury Selivanov66f88282015-06-24 11:04:15 -0400977return next iterated value or raise StopIteration.");
Yury Selivanov5376ba92015-06-22 12:19:30 -0400978
979PyDoc_STRVAR(coro_close_doc,
980"close() -> raise GeneratorExit inside coroutine.");
981
982static PyMethodDef coro_methods[] = {
983 {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc},
984 {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
985 {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
986 {NULL, NULL} /* Sentinel */
987};
988
989static PyAsyncMethods coro_as_async = {
990 (unaryfunc)coro_await, /* am_await */
991 0, /* am_aiter */
992 0 /* am_anext */
993};
994
995PyTypeObject PyCoro_Type = {
996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
997 "coroutine", /* tp_name */
998 sizeof(PyCoroObject), /* tp_basicsize */
999 0, /* tp_itemsize */
1000 /* methods */
1001 (destructor)gen_dealloc, /* tp_dealloc */
1002 0, /* tp_print */
1003 0, /* tp_getattr */
1004 0, /* tp_setattr */
1005 &coro_as_async, /* tp_as_async */
1006 (reprfunc)coro_repr, /* tp_repr */
1007 0, /* tp_as_number */
1008 0, /* tp_as_sequence */
1009 0, /* tp_as_mapping */
1010 0, /* tp_hash */
1011 0, /* tp_call */
1012 0, /* tp_str */
1013 PyObject_GenericGetAttr, /* tp_getattro */
1014 0, /* tp_setattro */
1015 0, /* tp_as_buffer */
1016 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1017 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
1018 0, /* tp_doc */
1019 (traverseproc)gen_traverse, /* tp_traverse */
1020 0, /* tp_clear */
1021 0, /* tp_richcompare */
1022 offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */
1023 0, /* tp_iter */
1024 0, /* tp_iternext */
1025 coro_methods, /* tp_methods */
1026 coro_memberlist, /* tp_members */
1027 coro_getsetlist, /* tp_getset */
1028 0, /* tp_base */
1029 0, /* tp_dict */
1030 0, /* tp_descr_get */
1031 0, /* tp_descr_set */
1032 0, /* tp_dictoffset */
1033 0, /* tp_init */
1034 0, /* tp_alloc */
1035 0, /* tp_new */
1036 0, /* tp_free */
1037 0, /* tp_is_gc */
1038 0, /* tp_bases */
1039 0, /* tp_mro */
1040 0, /* tp_cache */
1041 0, /* tp_subclasses */
1042 0, /* tp_weaklist */
1043 0, /* tp_del */
1044 0, /* tp_version_tag */
1045 _PyGen_Finalize, /* tp_finalize */
1046};
1047
1048static void
1049coro_wrapper_dealloc(PyCoroWrapper *cw)
1050{
1051 _PyObject_GC_UNTRACK((PyObject *)cw);
1052 Py_CLEAR(cw->cw_coroutine);
1053 PyObject_GC_Del(cw);
1054}
1055
1056static PyObject *
1057coro_wrapper_iternext(PyCoroWrapper *cw)
1058{
Yury Selivanov77c96812016-02-13 17:59:05 -05001059 return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0);
Yury Selivanov5376ba92015-06-22 12:19:30 -04001060}
1061
1062static PyObject *
1063coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
1064{
Yury Selivanov77c96812016-02-13 17:59:05 -05001065 return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0);
Yury Selivanov5376ba92015-06-22 12:19:30 -04001066}
1067
1068static PyObject *
1069coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
1070{
1071 return gen_throw((PyGenObject *)cw->cw_coroutine, args);
1072}
1073
1074static PyObject *
1075coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
1076{
1077 return gen_close((PyGenObject *)cw->cw_coroutine, args);
1078}
1079
1080static int
1081coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
1082{
1083 Py_VISIT((PyObject *)cw->cw_coroutine);
1084 return 0;
1085}
1086
1087static PyMethodDef coro_wrapper_methods[] = {
Yury Selivanov66f88282015-06-24 11:04:15 -04001088 {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
1089 {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
1090 {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
Yury Selivanov5376ba92015-06-22 12:19:30 -04001091 {NULL, NULL} /* Sentinel */
1092};
1093
1094PyTypeObject _PyCoroWrapper_Type = {
1095 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1096 "coroutine_wrapper",
1097 sizeof(PyCoroWrapper), /* tp_basicsize */
1098 0, /* tp_itemsize */
1099 (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */
1100 0, /* tp_print */
1101 0, /* tp_getattr */
1102 0, /* tp_setattr */
1103 0, /* tp_as_async */
1104 0, /* tp_repr */
1105 0, /* tp_as_number */
1106 0, /* tp_as_sequence */
1107 0, /* tp_as_mapping */
1108 0, /* tp_hash */
1109 0, /* tp_call */
1110 0, /* tp_str */
1111 PyObject_GenericGetAttr, /* tp_getattro */
1112 0, /* tp_setattro */
1113 0, /* tp_as_buffer */
1114 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1115 "A wrapper object implementing __await__ for coroutines.",
1116 (traverseproc)coro_wrapper_traverse, /* tp_traverse */
1117 0, /* tp_clear */
1118 0, /* tp_richcompare */
1119 0, /* tp_weaklistoffset */
1120 PyObject_SelfIter, /* tp_iter */
1121 (iternextfunc)coro_wrapper_iternext, /* tp_iternext */
1122 coro_wrapper_methods, /* tp_methods */
1123 0, /* tp_members */
1124 0, /* tp_getset */
1125 0, /* tp_base */
1126 0, /* tp_dict */
1127 0, /* tp_descr_get */
1128 0, /* tp_descr_set */
1129 0, /* tp_dictoffset */
1130 0, /* tp_init */
1131 0, /* tp_alloc */
1132 0, /* tp_new */
Yury Selivanov33499b72016-11-08 19:19:28 -05001133 0, /* tp_free */
Yury Selivanov5376ba92015-06-22 12:19:30 -04001134};
1135
1136PyObject *
1137PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1138{
1139 return gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
1140}
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001141
1142
1143/* __aiter__ wrapper; see http://bugs.python.org/issue27243 for details. */
1144
1145typedef struct {
1146 PyObject_HEAD
Yury Selivanoveb636452016-09-08 22:01:51 -07001147 PyObject *ags_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001148} PyAIterWrapper;
1149
1150
1151static PyObject *
1152aiter_wrapper_iternext(PyAIterWrapper *aw)
1153{
Serhiy Storchaka60e49aa2016-11-06 18:47:03 +02001154 _PyGen_SetStopIterationValue(aw->ags_aiter);
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001155 return NULL;
1156}
1157
1158static int
1159aiter_wrapper_traverse(PyAIterWrapper *aw, visitproc visit, void *arg)
1160{
Yury Selivanoveb636452016-09-08 22:01:51 -07001161 Py_VISIT((PyObject *)aw->ags_aiter);
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001162 return 0;
1163}
1164
1165static void
1166aiter_wrapper_dealloc(PyAIterWrapper *aw)
1167{
1168 _PyObject_GC_UNTRACK((PyObject *)aw);
Yury Selivanoveb636452016-09-08 22:01:51 -07001169 Py_CLEAR(aw->ags_aiter);
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001170 PyObject_GC_Del(aw);
1171}
1172
1173static PyAsyncMethods aiter_wrapper_as_async = {
1174 PyObject_SelfIter, /* am_await */
1175 0, /* am_aiter */
1176 0 /* am_anext */
1177};
1178
1179PyTypeObject _PyAIterWrapper_Type = {
1180 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1181 "aiter_wrapper",
1182 sizeof(PyAIterWrapper), /* tp_basicsize */
1183 0, /* tp_itemsize */
1184 (destructor)aiter_wrapper_dealloc, /* destructor tp_dealloc */
1185 0, /* tp_print */
1186 0, /* tp_getattr */
1187 0, /* tp_setattr */
1188 &aiter_wrapper_as_async, /* tp_as_async */
1189 0, /* tp_repr */
1190 0, /* tp_as_number */
1191 0, /* tp_as_sequence */
1192 0, /* tp_as_mapping */
1193 0, /* tp_hash */
1194 0, /* tp_call */
1195 0, /* tp_str */
1196 PyObject_GenericGetAttr, /* tp_getattro */
1197 0, /* tp_setattro */
1198 0, /* tp_as_buffer */
1199 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1200 "A wrapper object for __aiter__ bakwards compatibility.",
1201 (traverseproc)aiter_wrapper_traverse, /* tp_traverse */
1202 0, /* tp_clear */
1203 0, /* tp_richcompare */
1204 0, /* tp_weaklistoffset */
1205 PyObject_SelfIter, /* tp_iter */
1206 (iternextfunc)aiter_wrapper_iternext, /* tp_iternext */
1207 0, /* tp_methods */
1208 0, /* tp_members */
1209 0, /* tp_getset */
1210 0, /* tp_base */
1211 0, /* tp_dict */
1212 0, /* tp_descr_get */
1213 0, /* tp_descr_set */
1214 0, /* tp_dictoffset */
1215 0, /* tp_init */
1216 0, /* tp_alloc */
1217 0, /* tp_new */
Yury Selivanov33499b72016-11-08 19:19:28 -05001218 0, /* tp_free */
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001219};
1220
1221
1222PyObject *
1223_PyAIterWrapper_New(PyObject *aiter)
1224{
1225 PyAIterWrapper *aw = PyObject_GC_New(PyAIterWrapper,
1226 &_PyAIterWrapper_Type);
1227 if (aw == NULL) {
1228 return NULL;
1229 }
1230 Py_INCREF(aiter);
Yury Selivanoveb636452016-09-08 22:01:51 -07001231 aw->ags_aiter = aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001232 _PyObject_GC_TRACK(aw);
1233 return (PyObject *)aw;
1234}
Yury Selivanoveb636452016-09-08 22:01:51 -07001235
1236
1237/* ========= Asynchronous Generators ========= */
1238
1239
1240typedef enum {
1241 AWAITABLE_STATE_INIT, /* new awaitable, has not yet been iterated */
1242 AWAITABLE_STATE_ITER, /* being iterated */
1243 AWAITABLE_STATE_CLOSED, /* closed */
1244} AwaitableState;
1245
1246
1247typedef struct {
1248 PyObject_HEAD
1249 PyAsyncGenObject *ags_gen;
1250
1251 /* Can be NULL, when in the __anext__() mode
1252 (equivalent of "asend(None)") */
1253 PyObject *ags_sendval;
1254
1255 AwaitableState ags_state;
1256} PyAsyncGenASend;
1257
1258
1259typedef struct {
1260 PyObject_HEAD
1261 PyAsyncGenObject *agt_gen;
1262
1263 /* Can be NULL, when in the "aclose()" mode
1264 (equivalent of "athrow(GeneratorExit)") */
1265 PyObject *agt_args;
1266
1267 AwaitableState agt_state;
1268} PyAsyncGenAThrow;
1269
1270
1271typedef struct {
1272 PyObject_HEAD
1273 PyObject *agw_val;
1274} _PyAsyncGenWrappedValue;
1275
1276
1277#ifndef _PyAsyncGen_MAXFREELIST
1278#define _PyAsyncGen_MAXFREELIST 80
1279#endif
1280
1281/* Freelists boost performance 6-10%; they also reduce memory
1282 fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend
1283 are short-living objects that are instantiated for every
1284 __anext__ call.
1285*/
1286
1287static _PyAsyncGenWrappedValue *ag_value_freelist[_PyAsyncGen_MAXFREELIST];
1288static int ag_value_freelist_free = 0;
1289
1290static PyAsyncGenASend *ag_asend_freelist[_PyAsyncGen_MAXFREELIST];
1291static int ag_asend_freelist_free = 0;
1292
1293#define _PyAsyncGenWrappedValue_CheckExact(o) \
1294 (Py_TYPE(o) == &_PyAsyncGenWrappedValue_Type)
1295
1296#define PyAsyncGenASend_CheckExact(o) \
1297 (Py_TYPE(o) == &_PyAsyncGenASend_Type)
1298
1299
1300static int
1301async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg)
1302{
1303 Py_VISIT(gen->ag_finalizer);
1304 return gen_traverse((PyGenObject*)gen, visit, arg);
1305}
1306
1307
1308static PyObject *
1309async_gen_repr(PyAsyncGenObject *o)
1310{
1311 return PyUnicode_FromFormat("<async_generator object %S at %p>",
1312 o->ag_qualname, o);
1313}
1314
1315
1316static int
1317async_gen_init_hooks(PyAsyncGenObject *o)
1318{
1319 PyThreadState *tstate;
1320 PyObject *finalizer;
1321 PyObject *firstiter;
1322
1323 if (o->ag_hooks_inited) {
1324 return 0;
1325 }
1326
1327 o->ag_hooks_inited = 1;
1328
1329 tstate = PyThreadState_GET();
1330
1331 finalizer = tstate->async_gen_finalizer;
1332 if (finalizer) {
1333 Py_INCREF(finalizer);
1334 o->ag_finalizer = finalizer;
1335 }
1336
1337 firstiter = tstate->async_gen_firstiter;
1338 if (firstiter) {
1339 PyObject *res;
1340
1341 Py_INCREF(firstiter);
Victor Stinner7bfb42d2016-12-05 17:04:32 +01001342 res = PyObject_CallFunctionObjArgs(firstiter, o, NULL);
Yury Selivanoveb636452016-09-08 22:01:51 -07001343 Py_DECREF(firstiter);
1344 if (res == NULL) {
1345 return 1;
1346 }
1347 Py_DECREF(res);
1348 }
1349
1350 return 0;
1351}
1352
1353
1354static PyObject *
1355async_gen_anext(PyAsyncGenObject *o)
1356{
1357 if (async_gen_init_hooks(o)) {
1358 return NULL;
1359 }
1360 return async_gen_asend_new(o, NULL);
1361}
1362
1363
1364static PyObject *
1365async_gen_asend(PyAsyncGenObject *o, PyObject *arg)
1366{
1367 if (async_gen_init_hooks(o)) {
1368 return NULL;
1369 }
1370 return async_gen_asend_new(o, arg);
1371}
1372
1373
1374static PyObject *
1375async_gen_aclose(PyAsyncGenObject *o, PyObject *arg)
1376{
1377 if (async_gen_init_hooks(o)) {
1378 return NULL;
1379 }
1380 return async_gen_athrow_new(o, NULL);
1381}
1382
1383static PyObject *
1384async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
1385{
1386 if (async_gen_init_hooks(o)) {
1387 return NULL;
1388 }
1389 return async_gen_athrow_new(o, args);
1390}
1391
1392
1393static PyGetSetDef async_gen_getsetlist[] = {
1394 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
1395 PyDoc_STR("name of the async generator")},
1396 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
1397 PyDoc_STR("qualified name of the async generator")},
1398 {"ag_await", (getter)coro_get_cr_await, NULL,
1399 PyDoc_STR("object being awaited on, or None")},
1400 {NULL} /* Sentinel */
1401};
1402
1403static PyMemberDef async_gen_memberlist[] = {
1404 {"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY},
1405 {"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running), READONLY},
1406 {"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY},
1407 {NULL} /* Sentinel */
1408};
1409
1410PyDoc_STRVAR(async_aclose_doc,
1411"aclose() -> raise GeneratorExit inside generator.");
1412
1413PyDoc_STRVAR(async_asend_doc,
1414"asend(v) -> send 'v' in generator.");
1415
1416PyDoc_STRVAR(async_athrow_doc,
1417"athrow(typ[,val[,tb]]) -> raise exception in generator.");
1418
1419static PyMethodDef async_gen_methods[] = {
1420 {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
1421 {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
1422 {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
1423 {NULL, NULL} /* Sentinel */
1424};
1425
1426
1427static PyAsyncMethods async_gen_as_async = {
1428 0, /* am_await */
1429 PyObject_SelfIter, /* am_aiter */
1430 (unaryfunc)async_gen_anext /* am_anext */
1431};
1432
1433
1434PyTypeObject PyAsyncGen_Type = {
1435 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1436 "async_generator", /* tp_name */
1437 sizeof(PyAsyncGenObject), /* tp_basicsize */
1438 0, /* tp_itemsize */
1439 /* methods */
1440 (destructor)gen_dealloc, /* tp_dealloc */
1441 0, /* tp_print */
1442 0, /* tp_getattr */
1443 0, /* tp_setattr */
1444 &async_gen_as_async, /* tp_as_async */
1445 (reprfunc)async_gen_repr, /* tp_repr */
1446 0, /* tp_as_number */
1447 0, /* tp_as_sequence */
1448 0, /* tp_as_mapping */
1449 0, /* tp_hash */
1450 0, /* tp_call */
1451 0, /* tp_str */
1452 PyObject_GenericGetAttr, /* tp_getattro */
1453 0, /* tp_setattro */
1454 0, /* tp_as_buffer */
1455 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1456 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
1457 0, /* tp_doc */
1458 (traverseproc)async_gen_traverse, /* tp_traverse */
1459 0, /* tp_clear */
1460 0, /* tp_richcompare */
1461 offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */
1462 0, /* tp_iter */
1463 0, /* tp_iternext */
1464 async_gen_methods, /* tp_methods */
1465 async_gen_memberlist, /* tp_members */
1466 async_gen_getsetlist, /* tp_getset */
1467 0, /* tp_base */
1468 0, /* tp_dict */
1469 0, /* tp_descr_get */
1470 0, /* tp_descr_set */
1471 0, /* tp_dictoffset */
1472 0, /* tp_init */
1473 0, /* tp_alloc */
1474 0, /* tp_new */
1475 0, /* tp_free */
1476 0, /* tp_is_gc */
1477 0, /* tp_bases */
1478 0, /* tp_mro */
1479 0, /* tp_cache */
1480 0, /* tp_subclasses */
1481 0, /* tp_weaklist */
1482 0, /* tp_del */
1483 0, /* tp_version_tag */
1484 _PyGen_Finalize, /* tp_finalize */
1485};
1486
1487
1488PyObject *
1489PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1490{
1491 PyAsyncGenObject *o;
1492 o = (PyAsyncGenObject *)gen_new_with_qualname(
1493 &PyAsyncGen_Type, f, name, qualname);
1494 if (o == NULL) {
1495 return NULL;
1496 }
1497 o->ag_finalizer = NULL;
1498 o->ag_closed = 0;
1499 o->ag_hooks_inited = 0;
1500 return (PyObject*)o;
1501}
1502
1503
1504int
1505PyAsyncGen_ClearFreeLists(void)
1506{
1507 int ret = ag_value_freelist_free + ag_asend_freelist_free;
1508
1509 while (ag_value_freelist_free) {
1510 _PyAsyncGenWrappedValue *o;
1511 o = ag_value_freelist[--ag_value_freelist_free];
1512 assert(_PyAsyncGenWrappedValue_CheckExact(o));
Yury Selivanov29310c42016-11-08 19:46:22 -05001513 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001514 }
1515
1516 while (ag_asend_freelist_free) {
1517 PyAsyncGenASend *o;
1518 o = ag_asend_freelist[--ag_asend_freelist_free];
1519 assert(Py_TYPE(o) == &_PyAsyncGenASend_Type);
Yury Selivanov29310c42016-11-08 19:46:22 -05001520 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001521 }
1522
1523 return ret;
1524}
1525
1526void
1527PyAsyncGen_Fini(void)
1528{
1529 PyAsyncGen_ClearFreeLists();
1530}
1531
1532
1533static PyObject *
1534async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
1535{
1536 if (result == NULL) {
1537 if (!PyErr_Occurred()) {
1538 PyErr_SetNone(PyExc_StopAsyncIteration);
1539 }
1540
1541 if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)
1542 || PyErr_ExceptionMatches(PyExc_GeneratorExit)
1543 ) {
1544 gen->ag_closed = 1;
1545 }
1546
1547 return NULL;
1548 }
1549
1550 if (_PyAsyncGenWrappedValue_CheckExact(result)) {
1551 /* async yield */
Serhiy Storchaka60e49aa2016-11-06 18:47:03 +02001552 _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val);
Yury Selivanoveb636452016-09-08 22:01:51 -07001553 Py_DECREF(result);
Yury Selivanoveb636452016-09-08 22:01:51 -07001554 return NULL;
1555 }
1556
1557 return result;
1558}
1559
1560
1561/* ---------- Async Generator ASend Awaitable ------------ */
1562
1563
1564static void
1565async_gen_asend_dealloc(PyAsyncGenASend *o)
1566{
Yury Selivanov29310c42016-11-08 19:46:22 -05001567 _PyObject_GC_UNTRACK((PyObject *)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001568 Py_CLEAR(o->ags_gen);
1569 Py_CLEAR(o->ags_sendval);
1570 if (ag_asend_freelist_free < _PyAsyncGen_MAXFREELIST) {
1571 assert(PyAsyncGenASend_CheckExact(o));
1572 ag_asend_freelist[ag_asend_freelist_free++] = o;
1573 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001574 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001575 }
1576}
1577
Yury Selivanov29310c42016-11-08 19:46:22 -05001578static int
1579async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg)
1580{
1581 Py_VISIT(o->ags_gen);
1582 Py_VISIT(o->ags_sendval);
1583 return 0;
1584}
1585
Yury Selivanoveb636452016-09-08 22:01:51 -07001586
1587static PyObject *
1588async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg)
1589{
1590 PyObject *result;
1591
1592 if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1593 PyErr_SetNone(PyExc_StopIteration);
1594 return NULL;
1595 }
1596
1597 if (o->ags_state == AWAITABLE_STATE_INIT) {
1598 if (arg == NULL || arg == Py_None) {
1599 arg = o->ags_sendval;
1600 }
1601 o->ags_state = AWAITABLE_STATE_ITER;
1602 }
1603
1604 result = gen_send_ex((PyGenObject*)o->ags_gen, arg, 0, 0);
1605 result = async_gen_unwrap_value(o->ags_gen, result);
1606
1607 if (result == NULL) {
1608 o->ags_state = AWAITABLE_STATE_CLOSED;
1609 }
1610
1611 return result;
1612}
1613
1614
1615static PyObject *
1616async_gen_asend_iternext(PyAsyncGenASend *o)
1617{
1618 return async_gen_asend_send(o, NULL);
1619}
1620
1621
1622static PyObject *
1623async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
1624{
1625 PyObject *result;
1626
1627 if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1628 PyErr_SetNone(PyExc_StopIteration);
1629 return NULL;
1630 }
1631
1632 result = gen_throw((PyGenObject*)o->ags_gen, args);
1633 result = async_gen_unwrap_value(o->ags_gen, result);
1634
1635 if (result == NULL) {
1636 o->ags_state = AWAITABLE_STATE_CLOSED;
1637 }
1638
1639 return result;
1640}
1641
1642
1643static PyObject *
1644async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
1645{
1646 o->ags_state = AWAITABLE_STATE_CLOSED;
1647 Py_RETURN_NONE;
1648}
1649
1650
1651static PyMethodDef async_gen_asend_methods[] = {
1652 {"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
1653 {"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc},
1654 {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
1655 {NULL, NULL} /* Sentinel */
1656};
1657
1658
1659static PyAsyncMethods async_gen_asend_as_async = {
1660 PyObject_SelfIter, /* am_await */
1661 0, /* am_aiter */
1662 0 /* am_anext */
1663};
1664
1665
1666PyTypeObject _PyAsyncGenASend_Type = {
1667 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1668 "async_generator_asend", /* tp_name */
1669 sizeof(PyAsyncGenASend), /* tp_basicsize */
1670 0, /* tp_itemsize */
1671 /* methods */
1672 (destructor)async_gen_asend_dealloc, /* tp_dealloc */
1673 0, /* tp_print */
1674 0, /* tp_getattr */
1675 0, /* tp_setattr */
1676 &async_gen_asend_as_async, /* tp_as_async */
1677 0, /* tp_repr */
1678 0, /* tp_as_number */
1679 0, /* tp_as_sequence */
1680 0, /* tp_as_mapping */
1681 0, /* tp_hash */
1682 0, /* tp_call */
1683 0, /* tp_str */
1684 PyObject_GenericGetAttr, /* tp_getattro */
1685 0, /* tp_setattro */
1686 0, /* tp_as_buffer */
Yury Selivanov29310c42016-11-08 19:46:22 -05001687 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yury Selivanoveb636452016-09-08 22:01:51 -07001688 0, /* tp_doc */
Yury Selivanov29310c42016-11-08 19:46:22 -05001689 (traverseproc)async_gen_asend_traverse, /* tp_traverse */
Yury Selivanoveb636452016-09-08 22:01:51 -07001690 0, /* tp_clear */
1691 0, /* tp_richcompare */
1692 0, /* tp_weaklistoffset */
1693 PyObject_SelfIter, /* tp_iter */
1694 (iternextfunc)async_gen_asend_iternext, /* tp_iternext */
1695 async_gen_asend_methods, /* tp_methods */
1696 0, /* tp_members */
1697 0, /* tp_getset */
1698 0, /* tp_base */
1699 0, /* tp_dict */
1700 0, /* tp_descr_get */
1701 0, /* tp_descr_set */
1702 0, /* tp_dictoffset */
1703 0, /* tp_init */
1704 0, /* tp_alloc */
1705 0, /* tp_new */
1706};
1707
1708
1709static PyObject *
1710async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
1711{
1712 PyAsyncGenASend *o;
1713 if (ag_asend_freelist_free) {
1714 ag_asend_freelist_free--;
1715 o = ag_asend_freelist[ag_asend_freelist_free];
1716 _Py_NewReference((PyObject *)o);
1717 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001718 o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type);
Yury Selivanoveb636452016-09-08 22:01:51 -07001719 if (o == NULL) {
1720 return NULL;
1721 }
1722 }
1723
1724 Py_INCREF(gen);
1725 o->ags_gen = gen;
1726
1727 Py_XINCREF(sendval);
1728 o->ags_sendval = sendval;
1729
1730 o->ags_state = AWAITABLE_STATE_INIT;
Yury Selivanov29310c42016-11-08 19:46:22 -05001731
1732 _PyObject_GC_TRACK((PyObject*)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001733 return (PyObject*)o;
1734}
1735
1736
1737/* ---------- Async Generator Value Wrapper ------------ */
1738
1739
1740static void
1741async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
1742{
Yury Selivanov29310c42016-11-08 19:46:22 -05001743 _PyObject_GC_UNTRACK((PyObject *)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001744 Py_CLEAR(o->agw_val);
1745 if (ag_value_freelist_free < _PyAsyncGen_MAXFREELIST) {
1746 assert(_PyAsyncGenWrappedValue_CheckExact(o));
1747 ag_value_freelist[ag_value_freelist_free++] = o;
1748 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001749 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001750 }
1751}
1752
1753
Yury Selivanov29310c42016-11-08 19:46:22 -05001754static int
1755async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o,
1756 visitproc visit, void *arg)
1757{
1758 Py_VISIT(o->agw_val);
1759 return 0;
1760}
1761
1762
Yury Selivanoveb636452016-09-08 22:01:51 -07001763PyTypeObject _PyAsyncGenWrappedValue_Type = {
1764 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1765 "async_generator_wrapped_value", /* tp_name */
1766 sizeof(_PyAsyncGenWrappedValue), /* tp_basicsize */
1767 0, /* tp_itemsize */
1768 /* methods */
1769 (destructor)async_gen_wrapped_val_dealloc, /* tp_dealloc */
1770 0, /* tp_print */
1771 0, /* tp_getattr */
1772 0, /* tp_setattr */
1773 0, /* tp_as_async */
1774 0, /* tp_repr */
1775 0, /* tp_as_number */
1776 0, /* tp_as_sequence */
1777 0, /* tp_as_mapping */
1778 0, /* tp_hash */
1779 0, /* tp_call */
1780 0, /* tp_str */
1781 PyObject_GenericGetAttr, /* tp_getattro */
1782 0, /* tp_setattro */
1783 0, /* tp_as_buffer */
Yury Selivanov29310c42016-11-08 19:46:22 -05001784 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yury Selivanoveb636452016-09-08 22:01:51 -07001785 0, /* tp_doc */
Yury Selivanov29310c42016-11-08 19:46:22 -05001786 (traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */
Yury Selivanoveb636452016-09-08 22:01:51 -07001787 0, /* tp_clear */
1788 0, /* tp_richcompare */
1789 0, /* tp_weaklistoffset */
1790 0, /* tp_iter */
1791 0, /* tp_iternext */
1792 0, /* tp_methods */
1793 0, /* tp_members */
1794 0, /* tp_getset */
1795 0, /* tp_base */
1796 0, /* tp_dict */
1797 0, /* tp_descr_get */
1798 0, /* tp_descr_set */
1799 0, /* tp_dictoffset */
1800 0, /* tp_init */
1801 0, /* tp_alloc */
1802 0, /* tp_new */
1803};
1804
1805
1806PyObject *
1807_PyAsyncGenValueWrapperNew(PyObject *val)
1808{
1809 _PyAsyncGenWrappedValue *o;
1810 assert(val);
1811
1812 if (ag_value_freelist_free) {
1813 ag_value_freelist_free--;
1814 o = ag_value_freelist[ag_value_freelist_free];
1815 assert(_PyAsyncGenWrappedValue_CheckExact(o));
1816 _Py_NewReference((PyObject*)o);
1817 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001818 o = PyObject_GC_New(_PyAsyncGenWrappedValue,
1819 &_PyAsyncGenWrappedValue_Type);
Yury Selivanoveb636452016-09-08 22:01:51 -07001820 if (o == NULL) {
1821 return NULL;
1822 }
1823 }
1824 o->agw_val = val;
1825 Py_INCREF(val);
Yury Selivanov29310c42016-11-08 19:46:22 -05001826 _PyObject_GC_TRACK((PyObject*)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001827 return (PyObject*)o;
1828}
1829
1830
1831/* ---------- Async Generator AThrow awaitable ------------ */
1832
1833
1834static void
1835async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
1836{
Yury Selivanov29310c42016-11-08 19:46:22 -05001837 _PyObject_GC_UNTRACK((PyObject *)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001838 Py_CLEAR(o->agt_gen);
1839 Py_CLEAR(o->agt_args);
Yury Selivanov29310c42016-11-08 19:46:22 -05001840 PyObject_GC_Del(o);
1841}
1842
1843
1844static int
1845async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg)
1846{
1847 Py_VISIT(o->agt_gen);
1848 Py_VISIT(o->agt_args);
1849 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07001850}
1851
1852
1853static PyObject *
1854async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
1855{
1856 PyGenObject *gen = (PyGenObject*)o->agt_gen;
1857 PyFrameObject *f = gen->gi_frame;
1858 PyObject *retval;
1859
1860 if (f == NULL || f->f_stacktop == NULL ||
1861 o->agt_state == AWAITABLE_STATE_CLOSED) {
1862 PyErr_SetNone(PyExc_StopIteration);
1863 return NULL;
1864 }
1865
1866 if (o->agt_state == AWAITABLE_STATE_INIT) {
1867 if (o->agt_gen->ag_closed) {
1868 PyErr_SetNone(PyExc_StopIteration);
1869 return NULL;
1870 }
1871
1872 if (arg != Py_None) {
1873 PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
1874 return NULL;
1875 }
1876
1877 o->agt_state = AWAITABLE_STATE_ITER;
1878
1879 if (o->agt_args == NULL) {
1880 /* aclose() mode */
1881 o->agt_gen->ag_closed = 1;
1882
1883 retval = _gen_throw((PyGenObject *)gen,
1884 0, /* Do not close generator when
1885 PyExc_GeneratorExit is passed */
1886 PyExc_GeneratorExit, NULL, NULL);
1887
1888 if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
1889 Py_DECREF(retval);
1890 goto yield_close;
1891 }
1892 } else {
1893 PyObject *typ;
1894 PyObject *tb = NULL;
1895 PyObject *val = NULL;
1896
1897 if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3,
1898 &typ, &val, &tb)) {
1899 return NULL;
1900 }
1901
1902 retval = _gen_throw((PyGenObject *)gen,
1903 0, /* Do not close generator when
1904 PyExc_GeneratorExit is passed */
1905 typ, val, tb);
1906 retval = async_gen_unwrap_value(o->agt_gen, retval);
1907 }
1908 if (retval == NULL) {
1909 goto check_error;
1910 }
1911 return retval;
1912 }
1913
1914 assert(o->agt_state == AWAITABLE_STATE_ITER);
1915
1916 retval = gen_send_ex((PyGenObject *)gen, arg, 0, 0);
1917 if (o->agt_args) {
1918 return async_gen_unwrap_value(o->agt_gen, retval);
1919 } else {
1920 /* aclose() mode */
1921 if (retval) {
1922 if (_PyAsyncGenWrappedValue_CheckExact(retval)) {
1923 Py_DECREF(retval);
1924 goto yield_close;
1925 }
1926 else {
1927 return retval;
1928 }
1929 }
1930 else {
1931 goto check_error;
1932 }
1933 }
1934
1935yield_close:
1936 PyErr_SetString(
1937 PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
1938 return NULL;
1939
1940check_error:
Yury Selivanov41782e42016-11-16 18:16:17 -05001941 if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
1942 o->agt_state = AWAITABLE_STATE_CLOSED;
1943 if (o->agt_args == NULL) {
1944 /* when aclose() is called we don't want to propagate
1945 StopAsyncIteration; just raise StopIteration, signalling
1946 that 'aclose()' is done. */
1947 PyErr_Clear();
1948 PyErr_SetNone(PyExc_StopIteration);
1949 }
1950 }
1951 else if (PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
Yury Selivanoveb636452016-09-08 22:01:51 -07001952 o->agt_state = AWAITABLE_STATE_CLOSED;
1953 PyErr_Clear(); /* ignore these errors */
1954 PyErr_SetNone(PyExc_StopIteration);
1955 }
1956 return NULL;
1957}
1958
1959
1960static PyObject *
1961async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
1962{
1963 PyObject *retval;
1964
1965 if (o->agt_state == AWAITABLE_STATE_INIT) {
1966 PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
1967 return NULL;
1968 }
1969
1970 if (o->agt_state == AWAITABLE_STATE_CLOSED) {
1971 PyErr_SetNone(PyExc_StopIteration);
1972 return NULL;
1973 }
1974
1975 retval = gen_throw((PyGenObject*)o->agt_gen, args);
1976 if (o->agt_args) {
1977 return async_gen_unwrap_value(o->agt_gen, retval);
1978 } else {
1979 /* aclose() mode */
1980 if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
1981 Py_DECREF(retval);
1982 PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
1983 return NULL;
1984 }
1985 return retval;
1986 }
1987}
1988
1989
1990static PyObject *
1991async_gen_athrow_iternext(PyAsyncGenAThrow *o)
1992{
1993 return async_gen_athrow_send(o, Py_None);
1994}
1995
1996
1997static PyObject *
1998async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
1999{
2000 o->agt_state = AWAITABLE_STATE_CLOSED;
2001 Py_RETURN_NONE;
2002}
2003
2004
2005static PyMethodDef async_gen_athrow_methods[] = {
2006 {"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
2007 {"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc},
2008 {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
2009 {NULL, NULL} /* Sentinel */
2010};
2011
2012
2013static PyAsyncMethods async_gen_athrow_as_async = {
2014 PyObject_SelfIter, /* am_await */
2015 0, /* am_aiter */
2016 0 /* am_anext */
2017};
2018
2019
2020PyTypeObject _PyAsyncGenAThrow_Type = {
2021 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2022 "async_generator_athrow", /* tp_name */
2023 sizeof(PyAsyncGenAThrow), /* tp_basicsize */
2024 0, /* tp_itemsize */
2025 /* methods */
2026 (destructor)async_gen_athrow_dealloc, /* tp_dealloc */
2027 0, /* tp_print */
2028 0, /* tp_getattr */
2029 0, /* tp_setattr */
2030 &async_gen_athrow_as_async, /* tp_as_async */
2031 0, /* tp_repr */
2032 0, /* tp_as_number */
2033 0, /* tp_as_sequence */
2034 0, /* tp_as_mapping */
2035 0, /* tp_hash */
2036 0, /* tp_call */
2037 0, /* tp_str */
2038 PyObject_GenericGetAttr, /* tp_getattro */
2039 0, /* tp_setattro */
2040 0, /* tp_as_buffer */
Yury Selivanov29310c42016-11-08 19:46:22 -05002041 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yury Selivanoveb636452016-09-08 22:01:51 -07002042 0, /* tp_doc */
Yury Selivanov29310c42016-11-08 19:46:22 -05002043 (traverseproc)async_gen_athrow_traverse, /* tp_traverse */
Yury Selivanoveb636452016-09-08 22:01:51 -07002044 0, /* tp_clear */
2045 0, /* tp_richcompare */
2046 0, /* tp_weaklistoffset */
2047 PyObject_SelfIter, /* tp_iter */
2048 (iternextfunc)async_gen_athrow_iternext, /* tp_iternext */
2049 async_gen_athrow_methods, /* tp_methods */
2050 0, /* tp_members */
2051 0, /* tp_getset */
2052 0, /* tp_base */
2053 0, /* tp_dict */
2054 0, /* tp_descr_get */
2055 0, /* tp_descr_set */
2056 0, /* tp_dictoffset */
2057 0, /* tp_init */
2058 0, /* tp_alloc */
2059 0, /* tp_new */
2060};
2061
2062
2063static PyObject *
2064async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
2065{
2066 PyAsyncGenAThrow *o;
Yury Selivanov29310c42016-11-08 19:46:22 -05002067 o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type);
Yury Selivanoveb636452016-09-08 22:01:51 -07002068 if (o == NULL) {
2069 return NULL;
2070 }
2071 o->agt_gen = gen;
2072 o->agt_args = args;
2073 o->agt_state = AWAITABLE_STATE_INIT;
2074 Py_INCREF(gen);
2075 Py_XINCREF(args);
Yury Selivanov29310c42016-11-08 19:46:22 -05002076 _PyObject_GC_TRACK((PyObject*)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07002077 return (PyObject*)o;
2078}