blob: 1a8c37abf2337a09f9a05375b00f7d507b142f48 [file] [log] [blame]
Martin v. Löwise440e472004-06-01 15:22:42 +00001/* Generator object implementation */
2
3#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/pystate.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00005#include "frameobject.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00006#include "structmember.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007#include "opcode.h"
Martin v. Löwise440e472004-06-01 15:22:42 +00008
Yury Selivanoveb636452016-09-08 22:01:51 -07009static PyObject *gen_close(PyGenObject *, PyObject *);
10static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
11static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
12
13static char *NON_INIT_CORO_MSG = "can't send non-None value to a "
14 "just-started coroutine";
15
16static char *ASYNC_GEN_IGNORED_EXIT_MSG =
17 "async generator ignored GeneratorExit";
Nick Coghlan1f7ce622012-01-13 21:43:40 +100018
Martin v. Löwise440e472004-06-01 15:22:42 +000019static int
20gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
21{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 Py_VISIT((PyObject *)gen->gi_frame);
23 Py_VISIT(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +020024 Py_VISIT(gen->gi_name);
25 Py_VISIT(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 return 0;
Martin v. Löwise440e472004-06-01 15:22:42 +000027}
28
Antoine Pitrou58720d62013-08-05 23:26:40 +020029void
30_PyGen_Finalize(PyObject *self)
Antoine Pitrou796564c2013-07-30 19:59:21 +020031{
32 PyGenObject *gen = (PyGenObject *)self;
Benjamin Petersonb88db872016-09-07 08:46:59 -070033 PyObject *res = NULL;
Antoine Pitrou796564c2013-07-30 19:59:21 +020034 PyObject *error_type, *error_value, *error_traceback;
35
36 if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
37 /* Generator isn't paused, so no need to close */
38 return;
39
Yury Selivanoveb636452016-09-08 22:01:51 -070040 if (PyAsyncGen_CheckExact(self)) {
41 PyAsyncGenObject *agen = (PyAsyncGenObject*)self;
42 PyObject *finalizer = agen->ag_finalizer;
43 if (finalizer && !agen->ag_closed) {
44 /* Save the current exception, if any. */
45 PyErr_Fetch(&error_type, &error_value, &error_traceback);
46
Victor Stinnerde4ae3d2016-12-04 22:59:09 +010047 res = PyObject_CallFunctionObjArgs(finalizer, self, NULL);
Yury Selivanoveb636452016-09-08 22:01:51 -070048
49 if (res == NULL) {
50 PyErr_WriteUnraisable(self);
51 } else {
52 Py_DECREF(res);
53 }
54 /* Restore the saved exception. */
55 PyErr_Restore(error_type, error_value, error_traceback);
56 return;
57 }
58 }
59
Antoine Pitrou796564c2013-07-30 19:59:21 +020060 /* Save the current exception, if any. */
61 PyErr_Fetch(&error_type, &error_value, &error_traceback);
62
Benjamin Peterson2f40ed42016-09-05 10:14:54 -070063 /* If `gen` is a coroutine, and if it was never awaited on,
64 issue a RuntimeWarning. */
Benjamin Petersonb88db872016-09-07 08:46:59 -070065 if (gen->gi_code != NULL &&
66 ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
67 gen->gi_frame->f_lasti == -1) {
68 if (!error_value) {
69 PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
70 "coroutine '%.50S' was never awaited",
71 gen->gi_qualname);
72 }
Benjamin Peterson2f40ed42016-09-05 10:14:54 -070073 }
74 else {
75 res = gen_close(gen, NULL);
76 }
Antoine Pitrou796564c2013-07-30 19:59:21 +020077
Benjamin Petersonb88db872016-09-07 08:46:59 -070078 if (res == NULL) {
79 if (PyErr_Occurred())
80 PyErr_WriteUnraisable(self);
81 }
82 else {
Antoine Pitrou796564c2013-07-30 19:59:21 +020083 Py_DECREF(res);
Benjamin Petersonb88db872016-09-07 08:46:59 -070084 }
Antoine Pitrou796564c2013-07-30 19:59:21 +020085
86 /* Restore the saved exception. */
87 PyErr_Restore(error_type, error_value, error_traceback);
88}
89
90static void
Martin v. Löwise440e472004-06-01 15:22:42 +000091gen_dealloc(PyGenObject *gen)
92{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 PyObject *self = (PyObject *) gen;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 _PyObject_GC_UNTRACK(gen);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 if (gen->gi_weakreflist != NULL)
98 PyObject_ClearWeakRefs(self);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000099
Antoine Pitrou93963562013-05-14 20:37:52 +0200100 _PyObject_GC_TRACK(self);
101
Antoine Pitrou796564c2013-07-30 19:59:21 +0200102 if (PyObject_CallFinalizerFromDealloc(self))
103 return; /* resurrected. :( */
Antoine Pitrou93963562013-05-14 20:37:52 +0200104
105 _PyObject_GC_UNTRACK(self);
Yury Selivanoveb636452016-09-08 22:01:51 -0700106 if (PyAsyncGen_CheckExact(gen)) {
107 /* We have to handle this case for asynchronous generators
108 right here, because this code has to be between UNTRACK
109 and GC_Del. */
110 Py_CLEAR(((PyAsyncGenObject*)gen)->ag_finalizer);
111 }
Benjamin Petersonbdddb112016-09-05 10:39:57 -0700112 if (gen->gi_frame != NULL) {
113 gen->gi_frame->f_gen = NULL;
114 Py_CLEAR(gen->gi_frame);
115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 Py_CLEAR(gen->gi_code);
Victor Stinner40ee3012014-06-16 15:59:28 +0200117 Py_CLEAR(gen->gi_name);
118 Py_CLEAR(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 PyObject_GC_Del(gen);
Martin v. Löwise440e472004-06-01 15:22:42 +0000120}
121
122static PyObject *
Yury Selivanov77c96812016-02-13 17:59:05 -0500123gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
Martin v. Löwise440e472004-06-01 15:22:42 +0000124{
Antoine Pitrou93963562013-05-14 20:37:52 +0200125 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200127 PyObject *result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000128
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500129 if (gen->gi_running) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400130 char *msg = "generator already executing";
Yury Selivanoveb636452016-09-08 22:01:51 -0700131 if (PyCoro_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400132 msg = "coroutine already executing";
Yury Selivanoveb636452016-09-08 22:01:51 -0700133 }
134 else if (PyAsyncGen_CheckExact(gen)) {
135 msg = "async generator already executing";
136 }
Yury Selivanov5376ba92015-06-22 12:19:30 -0400137 PyErr_SetString(PyExc_ValueError, msg);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500138 return NULL;
139 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200140 if (f == NULL || f->f_stacktop == NULL) {
Yury Selivanov77c96812016-02-13 17:59:05 -0500141 if (PyCoro_CheckExact(gen) && !closing) {
142 /* `gen` is an exhausted coroutine: raise an error,
143 except when called from gen_close(), which should
144 always be a silent method. */
145 PyErr_SetString(
146 PyExc_RuntimeError,
147 "cannot reuse already awaited coroutine");
Yury Selivanoveb636452016-09-08 22:01:51 -0700148 }
149 else if (arg && !exc) {
Yury Selivanov77c96812016-02-13 17:59:05 -0500150 /* `gen` is an exhausted generator:
151 only set exception if called from send(). */
Yury Selivanoveb636452016-09-08 22:01:51 -0700152 if (PyAsyncGen_CheckExact(gen)) {
153 PyErr_SetNone(PyExc_StopAsyncIteration);
154 }
155 else {
156 PyErr_SetNone(PyExc_StopIteration);
157 }
Yury Selivanov77c96812016-02-13 17:59:05 -0500158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 return NULL;
160 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000161
Antoine Pitrou93963562013-05-14 20:37:52 +0200162 if (f->f_lasti == -1) {
163 if (arg && arg != Py_None) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400164 char *msg = "can't send non-None value to a "
165 "just-started generator";
Yury Selivanoveb636452016-09-08 22:01:51 -0700166 if (PyCoro_CheckExact(gen)) {
167 msg = NON_INIT_CORO_MSG;
168 }
169 else if (PyAsyncGen_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400170 msg = "can't send non-None value to a "
Yury Selivanoveb636452016-09-08 22:01:51 -0700171 "just-started async generator";
172 }
Yury Selivanov5376ba92015-06-22 12:19:30 -0400173 PyErr_SetString(PyExc_TypeError, msg);
Antoine Pitrou93963562013-05-14 20:37:52 +0200174 return NULL;
175 }
176 } else {
177 /* Push arg onto the frame's value stack */
178 result = arg ? arg : Py_None;
179 Py_INCREF(result);
180 *(f->f_stacktop++) = result;
181 }
182
183 /* Generators always return to their most recent caller, not
184 * necessarily their creator. */
185 Py_XINCREF(tstate->frame);
186 assert(f->f_back == NULL);
187 f->f_back = tstate->frame;
188
189 gen->gi_running = 1;
Victor Stinner59a73272016-12-09 18:51:13 +0100190 result = PyEval_EvalFrameEx(f, exc);
Antoine Pitrou93963562013-05-14 20:37:52 +0200191 gen->gi_running = 0;
192
193 /* Don't keep the reference to f_back any longer than necessary. It
194 * may keep a chain of frames alive or it could create a reference
195 * cycle. */
196 assert(f->f_back == tstate->frame);
197 Py_CLEAR(f->f_back);
198
199 /* If the generator just returned (as opposed to yielding), signal
200 * that the generator is exhausted. */
201 if (result && f->f_stacktop == NULL) {
202 if (result == Py_None) {
203 /* Delay exception instantiation if we can */
Yury Selivanoveb636452016-09-08 22:01:51 -0700204 if (PyAsyncGen_CheckExact(gen)) {
205 PyErr_SetNone(PyExc_StopAsyncIteration);
206 }
207 else {
208 PyErr_SetNone(PyExc_StopIteration);
209 }
210 }
211 else {
Yury Selivanoveb636452016-09-08 22:01:51 -0700212 /* Async generators cannot return anything but None */
213 assert(!PyAsyncGen_CheckExact(gen));
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200214 _PyGen_SetStopIterationValue(result);
Antoine Pitrou93963562013-05-14 20:37:52 +0200215 }
216 Py_CLEAR(result);
217 }
Yury Selivanov68333392015-05-22 11:16:47 -0400218 else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400219 /* Check for __future__ generator_stop and conditionally turn
220 * a leaking StopIteration into RuntimeError (with its cause
221 * set appropriately). */
Yury Selivanoveb636452016-09-08 22:01:51 -0700222
223 const int check_stop_iter_error_flags = CO_FUTURE_GENERATOR_STOP |
224 CO_COROUTINE |
225 CO_ITERABLE_COROUTINE |
226 CO_ASYNC_GENERATOR;
227
228 if (gen->gi_code != NULL &&
229 ((PyCodeObject *)gen->gi_code)->co_flags &
230 check_stop_iter_error_flags)
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400231 {
Yury Selivanoveb636452016-09-08 22:01:51 -0700232 /* `gen` is either:
233 * a generator with CO_FUTURE_GENERATOR_STOP flag;
234 * a coroutine;
235 * a generator with CO_ITERABLE_COROUTINE flag
236 (decorated with types.coroutine decorator);
237 * an async generator.
238 */
239 const char *msg = "generator raised StopIteration";
240 if (PyCoro_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400241 msg = "coroutine raised StopIteration";
Yury Selivanoveb636452016-09-08 22:01:51 -0700242 }
243 else if PyAsyncGen_CheckExact(gen) {
244 msg = "async generator raised StopIteration";
245 }
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300246 _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400247 }
Yury Selivanov68333392015-05-22 11:16:47 -0400248 else {
Yury Selivanoveb636452016-09-08 22:01:51 -0700249 /* `gen` is an ordinary generator without
250 CO_FUTURE_GENERATOR_STOP flag.
251 */
252
Yury Selivanov68333392015-05-22 11:16:47 -0400253 PyObject *exc, *val, *tb;
254
255 /* Pop the exception before issuing a warning. */
256 PyErr_Fetch(&exc, &val, &tb);
257
Martin Panter7e3a91a2016-02-10 04:40:48 +0000258 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
Yury Selivanov68333392015-05-22 11:16:47 -0400259 "generator '%.50S' raised StopIteration",
260 gen->gi_qualname)) {
261 /* Warning was converted to an error. */
262 Py_XDECREF(exc);
263 Py_XDECREF(val);
264 Py_XDECREF(tb);
265 }
266 else {
267 PyErr_Restore(exc, val, tb);
268 }
269 }
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400270 }
Yury Selivanoveb636452016-09-08 22:01:51 -0700271 else if (PyAsyncGen_CheckExact(gen) && !result &&
272 PyErr_ExceptionMatches(PyExc_StopAsyncIteration))
273 {
274 /* code in `gen` raised a StopAsyncIteration error:
275 raise a RuntimeError.
276 */
277 const char *msg = "async generator raised StopAsyncIteration";
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300278 _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
Yury Selivanoveb636452016-09-08 22:01:51 -0700279 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200280
281 if (!result || f->f_stacktop == NULL) {
282 /* generator can't be rerun, so release the frame */
283 /* first clean reference cycle through stored exception traceback */
284 PyObject *t, *v, *tb;
285 t = f->f_exc_type;
286 v = f->f_exc_value;
287 tb = f->f_exc_traceback;
288 f->f_exc_type = NULL;
289 f->f_exc_value = NULL;
290 f->f_exc_traceback = NULL;
291 Py_XDECREF(t);
292 Py_XDECREF(v);
293 Py_XDECREF(tb);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200294 gen->gi_frame->f_gen = NULL;
Antoine Pitrou93963562013-05-14 20:37:52 +0200295 gen->gi_frame = NULL;
296 Py_DECREF(f);
297 }
298
299 return result;
Martin v. Löwise440e472004-06-01 15:22:42 +0000300}
301
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000302PyDoc_STRVAR(send_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303"send(arg) -> send 'arg' into generator,\n\
304return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000305
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500306PyObject *
307_PyGen_Send(PyGenObject *gen, PyObject *arg)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000308{
Yury Selivanov77c96812016-02-13 17:59:05 -0500309 return gen_send_ex(gen, arg, 0, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000310}
311
312PyDoc_STRVAR(close_doc,
Benjamin Petersonab3da292012-05-03 18:44:09 -0400313"close() -> raise GeneratorExit inside generator.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000314
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000315/*
316 * This helper function is used by gen_close and gen_throw to
317 * close a subiterator being delegated to by yield-from.
318 */
319
Antoine Pitrou93963562013-05-14 20:37:52 +0200320static int
321gen_close_iter(PyObject *yf)
322{
323 PyObject *retval = NULL;
324 _Py_IDENTIFIER(close);
325
Yury Selivanoveb636452016-09-08 22:01:51 -0700326 if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
Antoine Pitrou93963562013-05-14 20:37:52 +0200327 retval = gen_close((PyGenObject *)yf, NULL);
328 if (retval == NULL)
329 return -1;
Yury Selivanoveb636452016-09-08 22:01:51 -0700330 }
331 else {
Antoine Pitrou93963562013-05-14 20:37:52 +0200332 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
333 if (meth == NULL) {
334 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
335 PyErr_WriteUnraisable(yf);
336 PyErr_Clear();
Yury Selivanoveb636452016-09-08 22:01:51 -0700337 }
338 else {
Victor Stinner3466bde2016-09-05 18:16:01 -0700339 retval = _PyObject_CallNoArg(meth);
Antoine Pitrou93963562013-05-14 20:37:52 +0200340 Py_DECREF(meth);
341 if (retval == NULL)
342 return -1;
343 }
344 }
345 Py_XDECREF(retval);
346 return 0;
347}
348
Yury Selivanovc724bae2016-03-02 11:30:46 -0500349PyObject *
350_PyGen_yf(PyGenObject *gen)
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500351{
Antoine Pitrou93963562013-05-14 20:37:52 +0200352 PyObject *yf = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500353 PyFrameObject *f = gen->gi_frame;
Antoine Pitrou93963562013-05-14 20:37:52 +0200354
355 if (f && f->f_stacktop) {
356 PyObject *bytecode = f->f_code->co_code;
357 unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
358
Victor Stinnerf7d199f2016-11-24 22:33:01 +0100359 if (f->f_lasti < 0) {
360 /* Return immediately if the frame didn't start yet. YIELD_FROM
361 always come after LOAD_CONST: a code object should not start
362 with YIELD_FROM */
363 assert(code[0] != YIELD_FROM);
364 return NULL;
365 }
366
Serhiy Storchakaab874002016-09-11 13:48:15 +0300367 if (code[f->f_lasti + sizeof(_Py_CODEUNIT)] != YIELD_FROM)
Antoine Pitrou93963562013-05-14 20:37:52 +0200368 return NULL;
369 yf = f->f_stacktop[-1];
370 Py_INCREF(yf);
371 }
372
373 return yf;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500374}
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000375
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000376static PyObject *
377gen_close(PyGenObject *gen, PyObject *args)
378{
Antoine Pitrou93963562013-05-14 20:37:52 +0200379 PyObject *retval;
Yury Selivanovc724bae2016-03-02 11:30:46 -0500380 PyObject *yf = _PyGen_yf(gen);
Antoine Pitrou93963562013-05-14 20:37:52 +0200381 int err = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000382
Antoine Pitrou93963562013-05-14 20:37:52 +0200383 if (yf) {
384 gen->gi_running = 1;
385 err = gen_close_iter(yf);
386 gen->gi_running = 0;
387 Py_DECREF(yf);
388 }
389 if (err == 0)
390 PyErr_SetNone(PyExc_GeneratorExit);
Yury Selivanov77c96812016-02-13 17:59:05 -0500391 retval = gen_send_ex(gen, Py_None, 1, 1);
Antoine Pitrou93963562013-05-14 20:37:52 +0200392 if (retval) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400393 char *msg = "generator ignored GeneratorExit";
Yury Selivanoveb636452016-09-08 22:01:51 -0700394 if (PyCoro_CheckExact(gen)) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400395 msg = "coroutine ignored GeneratorExit";
Yury Selivanoveb636452016-09-08 22:01:51 -0700396 } else if (PyAsyncGen_CheckExact(gen)) {
397 msg = ASYNC_GEN_IGNORED_EXIT_MSG;
398 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200399 Py_DECREF(retval);
Yury Selivanov5376ba92015-06-22 12:19:30 -0400400 PyErr_SetString(PyExc_RuntimeError, msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return NULL;
402 }
Antoine Pitrou93963562013-05-14 20:37:52 +0200403 if (PyErr_ExceptionMatches(PyExc_StopIteration)
404 || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
405 PyErr_Clear(); /* ignore these errors */
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200406 Py_RETURN_NONE;
Antoine Pitrou93963562013-05-14 20:37:52 +0200407 }
408 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000409}
410
Antoine Pitrou93963562013-05-14 20:37:52 +0200411
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000412PyDoc_STRVAR(throw_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
414return next yielded value or raise StopIteration.");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000415
416static PyObject *
Yury Selivanoveb636452016-09-08 22:01:51 -0700417_gen_throw(PyGenObject *gen, int close_on_genexit,
418 PyObject *typ, PyObject *val, PyObject *tb)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000419{
Yury Selivanovc724bae2016-03-02 11:30:46 -0500420 PyObject *yf = _PyGen_yf(gen);
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000421 _Py_IDENTIFIER(throw);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000422
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000423 if (yf) {
424 PyObject *ret;
425 int err;
Yury Selivanoveb636452016-09-08 22:01:51 -0700426 if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
427 close_on_genexit
428 ) {
429 /* Asynchronous generators *should not* be closed right away.
430 We have to allow some awaits to work it through, hence the
431 `close_on_genexit` parameter here.
432 */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500433 gen->gi_running = 1;
Antoine Pitrou93963562013-05-14 20:37:52 +0200434 err = gen_close_iter(yf);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500435 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000436 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000437 if (err < 0)
Yury Selivanov77c96812016-02-13 17:59:05 -0500438 return gen_send_ex(gen, Py_None, 1, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000439 goto throw_here;
440 }
Yury Selivanoveb636452016-09-08 22:01:51 -0700441 if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
442 /* `yf` is a generator or a coroutine. */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500443 gen->gi_running = 1;
Yury Selivanoveb636452016-09-08 22:01:51 -0700444 /* Close the generator that we are currently iterating with
445 'yield from' or awaiting on with 'await'. */
446 ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
447 typ, val, tb);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500448 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000449 } else {
Yury Selivanoveb636452016-09-08 22:01:51 -0700450 /* `yf` is an iterator or a coroutine-like object. */
Nick Coghlan5b0dac12012-06-17 15:45:11 +1000451 PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000452 if (meth == NULL) {
453 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
454 Py_DECREF(yf);
455 return NULL;
456 }
457 PyErr_Clear();
458 Py_DECREF(yf);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000459 goto throw_here;
460 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500461 gen->gi_running = 1;
Yury Selivanoveb636452016-09-08 22:01:51 -0700462 ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500463 gen->gi_running = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000464 Py_DECREF(meth);
465 }
466 Py_DECREF(yf);
467 if (!ret) {
468 PyObject *val;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500469 /* Pop subiterator from stack */
470 ret = *(--gen->gi_frame->f_stacktop);
471 assert(ret == yf);
472 Py_DECREF(ret);
473 /* Termination repetition of YIELD_FROM */
Victor Stinnerf7d199f2016-11-24 22:33:01 +0100474 assert(gen->gi_frame->f_lasti >= 0);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300475 gen->gi_frame->f_lasti += sizeof(_Py_CODEUNIT);
Nick Coghlanc40bc092012-06-17 15:15:49 +1000476 if (_PyGen_FetchStopIterationValue(&val) == 0) {
Yury Selivanov77c96812016-02-13 17:59:05 -0500477 ret = gen_send_ex(gen, val, 0, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000478 Py_DECREF(val);
479 } else {
Yury Selivanov77c96812016-02-13 17:59:05 -0500480 ret = gen_send_ex(gen, Py_None, 1, 0);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000481 }
482 }
483 return ret;
484 }
485
486throw_here:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 /* First, check the traceback argument, replacing None with
488 NULL. */
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400489 if (tb == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 tb = NULL;
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400491 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 else if (tb != NULL && !PyTraceBack_Check(tb)) {
493 PyErr_SetString(PyExc_TypeError,
494 "throw() third argument must be a traceback object");
495 return NULL;
496 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 Py_INCREF(typ);
499 Py_XINCREF(val);
500 Py_XINCREF(tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000501
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400502 if (PyExceptionClass_Check(typ))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyErr_NormalizeException(&typ, &val, &tb);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 else if (PyExceptionInstance_Check(typ)) {
506 /* Raising an instance. The value should be a dummy. */
507 if (val && val != Py_None) {
508 PyErr_SetString(PyExc_TypeError,
509 "instance exception may not have a separate value");
510 goto failed_throw;
511 }
512 else {
513 /* Normalize to raise <class>, <instance> */
514 Py_XDECREF(val);
515 val = typ;
516 typ = PyExceptionInstance_Class(typ);
517 Py_INCREF(typ);
Antoine Pitrou551ba202011-10-18 16:40:50 +0200518
Benjamin Peterson9d9141f2011-10-19 16:57:40 -0400519 if (tb == NULL)
Antoine Pitrou551ba202011-10-18 16:40:50 +0200520 /* Returns NULL if there's no traceback */
521 tb = PyException_GetTraceback(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 }
523 }
524 else {
525 /* Not something you can raise. throw() fails. */
526 PyErr_Format(PyExc_TypeError,
527 "exceptions must be classes or instances "
528 "deriving from BaseException, not %s",
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000529 Py_TYPE(typ)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 goto failed_throw;
531 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyErr_Restore(typ, val, tb);
Yury Selivanov77c96812016-02-13 17:59:05 -0500534 return gen_send_ex(gen, Py_None, 1, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000535
536failed_throw:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 /* Didn't use our arguments, so restore their original refcounts */
538 Py_DECREF(typ);
539 Py_XDECREF(val);
540 Py_XDECREF(tb);
541 return NULL;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000542}
543
544
545static PyObject *
Yury Selivanoveb636452016-09-08 22:01:51 -0700546gen_throw(PyGenObject *gen, PyObject *args)
547{
548 PyObject *typ;
549 PyObject *tb = NULL;
550 PyObject *val = NULL;
551
552 if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) {
553 return NULL;
554 }
555
556 return _gen_throw(gen, 1, typ, val, tb);
557}
558
559
560static PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000561gen_iternext(PyGenObject *gen)
562{
Yury Selivanov77c96812016-02-13 17:59:05 -0500563 return gen_send_ex(gen, NULL, 0, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000564}
565
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000566/*
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200567 * Set StopIteration with specified value. Value can be arbitrary object
568 * or NULL.
569 *
570 * Returns 0 if StopIteration is set and -1 if any other exception is set.
571 */
572int
573_PyGen_SetStopIterationValue(PyObject *value)
574{
575 PyObject *e;
576
577 if (value == NULL ||
Yury Selivanovb7c91502017-03-12 15:53:07 -0400578 (!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200579 {
580 /* Delay exception instantiation if we can */
581 PyErr_SetObject(PyExc_StopIteration, value);
582 return 0;
583 }
584 /* Construct an exception instance manually with
585 * PyObject_CallFunctionObjArgs and pass it to PyErr_SetObject.
586 *
587 * We do this to handle a situation when "value" is a tuple, in which
588 * case PyErr_SetObject would set the value of StopIteration to
589 * the first element of the tuple.
590 *
591 * (See PyErr_SetObject/_PyErr_CreateException code for details.)
592 */
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100593 e = PyObject_CallFunctionObjArgs(PyExc_StopIteration, value, NULL);
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200594 if (e == NULL) {
595 return -1;
596 }
597 PyErr_SetObject(PyExc_StopIteration, e);
598 Py_DECREF(e);
599 return 0;
600}
601
602/*
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000603 * If StopIteration exception is set, fetches its 'value'
604 * attribute if any, otherwise sets pvalue to None.
605 *
606 * Returns 0 if no exception or StopIteration is set.
607 * If any other exception is set, returns -1 and leaves
608 * pvalue unchanged.
609 */
610
611int
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200612_PyGen_FetchStopIterationValue(PyObject **pvalue)
613{
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000614 PyObject *et, *ev, *tb;
615 PyObject *value = NULL;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500616
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000617 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
618 PyErr_Fetch(&et, &ev, &tb);
Antoine Pitrou7403e912015-04-26 18:46:40 +0200619 if (ev) {
620 /* exception will usually be normalised already */
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300621 if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200622 value = ((PyStopIterationObject *)ev)->value;
623 Py_INCREF(value);
624 Py_DECREF(ev);
Serhiy Storchaka24411f82016-11-06 18:44:42 +0200625 } else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) {
626 /* Avoid normalisation and take ev as value.
627 *
628 * Normalization is required if the value is a tuple, in
629 * that case the value of StopIteration would be set to
630 * the first element of the tuple.
631 *
632 * (See _PyErr_CreateException code for details.)
633 */
Antoine Pitrou7403e912015-04-26 18:46:40 +0200634 value = ev;
635 } else {
636 /* normalisation required */
637 PyErr_NormalizeException(&et, &ev, &tb);
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300638 if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
Antoine Pitrou7403e912015-04-26 18:46:40 +0200639 PyErr_Restore(et, ev, tb);
640 return -1;
641 }
642 value = ((PyStopIterationObject *)ev)->value;
643 Py_INCREF(value);
644 Py_DECREF(ev);
645 }
646 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000647 Py_XDECREF(et);
648 Py_XDECREF(tb);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000649 } else if (PyErr_Occurred()) {
650 return -1;
651 }
652 if (value == NULL) {
653 value = Py_None;
Amaury Forgeot d'Arce557da82012-01-13 21:06:12 +0100654 Py_INCREF(value);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000655 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000656 *pvalue = value;
657 return 0;
658}
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000659
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000660static PyObject *
661gen_repr(PyGenObject *gen)
662{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400663 return PyUnicode_FromFormat("<generator object %S at %p>",
664 gen->gi_qualname, gen);
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000665}
666
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000667static PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200668gen_get_name(PyGenObject *op)
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000669{
Victor Stinner40ee3012014-06-16 15:59:28 +0200670 Py_INCREF(op->gi_name);
671 return op->gi_name;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000672}
673
Victor Stinner40ee3012014-06-16 15:59:28 +0200674static int
675gen_set_name(PyGenObject *op, PyObject *value)
676{
Victor Stinner40ee3012014-06-16 15:59:28 +0200677 /* Not legal to del gen.gi_name or to set it to anything
678 * other than a string object. */
679 if (value == NULL || !PyUnicode_Check(value)) {
680 PyErr_SetString(PyExc_TypeError,
681 "__name__ must be set to a string object");
682 return -1;
683 }
Victor Stinner40ee3012014-06-16 15:59:28 +0200684 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300685 Py_XSETREF(op->gi_name, value);
Victor Stinner40ee3012014-06-16 15:59:28 +0200686 return 0;
687}
688
689static PyObject *
690gen_get_qualname(PyGenObject *op)
691{
692 Py_INCREF(op->gi_qualname);
693 return op->gi_qualname;
694}
695
696static int
697gen_set_qualname(PyGenObject *op, PyObject *value)
698{
Victor Stinner40ee3012014-06-16 15:59:28 +0200699 /* Not legal to del gen.__qualname__ or to set it to anything
700 * other than a string object. */
701 if (value == NULL || !PyUnicode_Check(value)) {
702 PyErr_SetString(PyExc_TypeError,
703 "__qualname__ must be set to a string object");
704 return -1;
705 }
Victor Stinner40ee3012014-06-16 15:59:28 +0200706 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300707 Py_XSETREF(op->gi_qualname, value);
Victor Stinner40ee3012014-06-16 15:59:28 +0200708 return 0;
709}
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000710
Yury Selivanove13f8f32015-07-03 00:23:30 -0400711static PyObject *
712gen_getyieldfrom(PyGenObject *gen)
713{
Yury Selivanovc724bae2016-03-02 11:30:46 -0500714 PyObject *yf = _PyGen_yf(gen);
Yury Selivanove13f8f32015-07-03 00:23:30 -0400715 if (yf == NULL)
716 Py_RETURN_NONE;
717 return yf;
718}
719
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000720static PyGetSetDef gen_getsetlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200721 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
722 PyDoc_STR("name of the generator")},
723 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
724 PyDoc_STR("qualified name of the generator")},
Yury Selivanove13f8f32015-07-03 00:23:30 -0400725 {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
726 PyDoc_STR("object being iterated by yield from, or None")},
Victor Stinner40ee3012014-06-16 15:59:28 +0200727 {NULL} /* Sentinel */
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +0000728};
729
Martin v. Löwise440e472004-06-01 15:22:42 +0000730static PyMemberDef gen_memberlist[] = {
Victor Stinner40ee3012014-06-16 15:59:28 +0200731 {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
732 {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
733 {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 {NULL} /* Sentinel */
Martin v. Löwise440e472004-06-01 15:22:42 +0000735};
736
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000737static PyMethodDef gen_methods[] = {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500738 {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
740 {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
741 {NULL, NULL} /* Sentinel */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000742};
743
Martin v. Löwise440e472004-06-01 15:22:42 +0000744PyTypeObject PyGen_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyVarObject_HEAD_INIT(&PyType_Type, 0)
746 "generator", /* tp_name */
747 sizeof(PyGenObject), /* tp_basicsize */
748 0, /* tp_itemsize */
749 /* methods */
750 (destructor)gen_dealloc, /* tp_dealloc */
751 0, /* tp_print */
752 0, /* tp_getattr */
753 0, /* tp_setattr */
Yury Selivanov75445082015-05-11 22:57:16 -0400754 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 (reprfunc)gen_repr, /* tp_repr */
756 0, /* tp_as_number */
757 0, /* tp_as_sequence */
758 0, /* tp_as_mapping */
759 0, /* tp_hash */
760 0, /* tp_call */
761 0, /* tp_str */
762 PyObject_GenericGetAttr, /* tp_getattro */
763 0, /* tp_setattro */
764 0, /* tp_as_buffer */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200765 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
766 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 0, /* tp_doc */
768 (traverseproc)gen_traverse, /* tp_traverse */
769 0, /* tp_clear */
770 0, /* tp_richcompare */
771 offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
Yury Selivanov5376ba92015-06-22 12:19:30 -0400772 PyObject_SelfIter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 (iternextfunc)gen_iternext, /* tp_iternext */
774 gen_methods, /* tp_methods */
775 gen_memberlist, /* tp_members */
776 gen_getsetlist, /* tp_getset */
777 0, /* tp_base */
778 0, /* tp_dict */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 0, /* tp_descr_get */
781 0, /* tp_descr_set */
782 0, /* tp_dictoffset */
783 0, /* tp_init */
784 0, /* tp_alloc */
785 0, /* tp_new */
786 0, /* tp_free */
787 0, /* tp_is_gc */
788 0, /* tp_bases */
789 0, /* tp_mro */
790 0, /* tp_cache */
791 0, /* tp_subclasses */
792 0, /* tp_weaklist */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200793 0, /* tp_del */
794 0, /* tp_version_tag */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200795 _PyGen_Finalize, /* tp_finalize */
Martin v. Löwise440e472004-06-01 15:22:42 +0000796};
797
Yury Selivanov5376ba92015-06-22 12:19:30 -0400798static PyObject *
799gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
800 PyObject *name, PyObject *qualname)
Martin v. Löwise440e472004-06-01 15:22:42 +0000801{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400802 PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 if (gen == NULL) {
804 Py_DECREF(f);
805 return NULL;
806 }
807 gen->gi_frame = f;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200808 f->f_gen = (PyObject *) gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 Py_INCREF(f->f_code);
810 gen->gi_code = (PyObject *)(f->f_code);
811 gen->gi_running = 0;
812 gen->gi_weakreflist = NULL;
Victor Stinner40ee3012014-06-16 15:59:28 +0200813 if (name != NULL)
814 gen->gi_name = name;
815 else
816 gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
817 Py_INCREF(gen->gi_name);
818 if (qualname != NULL)
819 gen->gi_qualname = qualname;
820 else
821 gen->gi_qualname = gen->gi_name;
822 Py_INCREF(gen->gi_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 _PyObject_GC_TRACK(gen);
824 return (PyObject *)gen;
Martin v. Löwise440e472004-06-01 15:22:42 +0000825}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826
Victor Stinner40ee3012014-06-16 15:59:28 +0200827PyObject *
Yury Selivanov5376ba92015-06-22 12:19:30 -0400828PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
829{
830 return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
831}
832
833PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +0200834PyGen_New(PyFrameObject *f)
835{
Yury Selivanov5376ba92015-06-22 12:19:30 -0400836 return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
Victor Stinner40ee3012014-06-16 15:59:28 +0200837}
838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839int
840PyGen_NeedsFinalizing(PyGenObject *gen)
841{
Antoine Pitrou93963562013-05-14 20:37:52 +0200842 int i;
843 PyFrameObject *f = gen->gi_frame;
844
845 if (f == NULL || f->f_stacktop == NULL)
846 return 0; /* no frame or empty blockstack == no finalization */
847
848 /* Any block type besides a loop requires cleanup. */
849 for (i = 0; i < f->f_iblock; i++)
850 if (f->f_blockstack[i].b_type != SETUP_LOOP)
851 return 1;
852
853 /* No blocks except loops, it's safe to skip finalization. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855}
Yury Selivanov75445082015-05-11 22:57:16 -0400856
Yury Selivanov5376ba92015-06-22 12:19:30 -0400857/* Coroutine Object */
858
859typedef struct {
860 PyObject_HEAD
861 PyCoroObject *cw_coroutine;
862} PyCoroWrapper;
863
864static int
865gen_is_coroutine(PyObject *o)
866{
867 if (PyGen_CheckExact(o)) {
868 PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
869 if (code->co_flags & CO_ITERABLE_COROUTINE) {
870 return 1;
871 }
872 }
873 return 0;
874}
875
Yury Selivanov75445082015-05-11 22:57:16 -0400876/*
877 * This helper function returns an awaitable for `o`:
878 * - `o` if `o` is a coroutine-object;
879 * - `type(o)->tp_as_async->am_await(o)`
880 *
881 * Raises a TypeError if it's not possible to return
882 * an awaitable and returns NULL.
883 */
884PyObject *
Yury Selivanov5376ba92015-06-22 12:19:30 -0400885_PyCoro_GetAwaitableIter(PyObject *o)
Yury Selivanov75445082015-05-11 22:57:16 -0400886{
Yury Selivanov6ef05902015-05-28 11:21:31 -0400887 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -0400888 PyTypeObject *ot;
889
Yury Selivanov5376ba92015-06-22 12:19:30 -0400890 if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
891 /* 'o' is a coroutine. */
Yury Selivanov75445082015-05-11 22:57:16 -0400892 Py_INCREF(o);
893 return o;
894 }
895
896 ot = Py_TYPE(o);
897 if (ot->tp_as_async != NULL) {
898 getter = ot->tp_as_async->am_await;
899 }
900 if (getter != NULL) {
901 PyObject *res = (*getter)(o);
902 if (res != NULL) {
Yury Selivanov5376ba92015-06-22 12:19:30 -0400903 if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
904 /* __await__ must return an *iterator*, not
905 a coroutine or another awaitable (see PEP 492) */
906 PyErr_SetString(PyExc_TypeError,
907 "__await__() returned a coroutine");
908 Py_CLEAR(res);
909 } else if (!PyIter_Check(res)) {
Yury Selivanov75445082015-05-11 22:57:16 -0400910 PyErr_Format(PyExc_TypeError,
911 "__await__() returned non-iterator "
912 "of type '%.100s'",
913 Py_TYPE(res)->tp_name);
914 Py_CLEAR(res);
915 }
Yury Selivanov75445082015-05-11 22:57:16 -0400916 }
917 return res;
918 }
919
920 PyErr_Format(PyExc_TypeError,
921 "object %.100s can't be used in 'await' expression",
922 ot->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -0400923 return NULL;
924}
Yury Selivanov5376ba92015-06-22 12:19:30 -0400925
926static PyObject *
927coro_repr(PyCoroObject *coro)
928{
929 return PyUnicode_FromFormat("<coroutine object %S at %p>",
930 coro->cr_qualname, coro);
931}
932
933static PyObject *
934coro_await(PyCoroObject *coro)
935{
936 PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
937 if (cw == NULL) {
938 return NULL;
939 }
940 Py_INCREF(coro);
941 cw->cw_coroutine = coro;
942 _PyObject_GC_TRACK(cw);
943 return (PyObject *)cw;
944}
945
Yury Selivanove13f8f32015-07-03 00:23:30 -0400946static PyObject *
947coro_get_cr_await(PyCoroObject *coro)
948{
Yury Selivanovc724bae2016-03-02 11:30:46 -0500949 PyObject *yf = _PyGen_yf((PyGenObject *) coro);
Yury Selivanove13f8f32015-07-03 00:23:30 -0400950 if (yf == NULL)
951 Py_RETURN_NONE;
952 return yf;
953}
954
Yury Selivanov5376ba92015-06-22 12:19:30 -0400955static PyGetSetDef coro_getsetlist[] = {
956 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
957 PyDoc_STR("name of the coroutine")},
958 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
959 PyDoc_STR("qualified name of the coroutine")},
Yury Selivanove13f8f32015-07-03 00:23:30 -0400960 {"cr_await", (getter)coro_get_cr_await, NULL,
961 PyDoc_STR("object being awaited on, or None")},
Yury Selivanov5376ba92015-06-22 12:19:30 -0400962 {NULL} /* Sentinel */
963};
964
965static PyMemberDef coro_memberlist[] = {
966 {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
967 {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
968 {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
969 {NULL} /* Sentinel */
970};
971
972PyDoc_STRVAR(coro_send_doc,
973"send(arg) -> send 'arg' into coroutine,\n\
Yury Selivanov66f88282015-06-24 11:04:15 -0400974return next iterated value or raise StopIteration.");
Yury Selivanov5376ba92015-06-22 12:19:30 -0400975
976PyDoc_STRVAR(coro_throw_doc,
977"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
Yury Selivanov66f88282015-06-24 11:04:15 -0400978return next iterated value or raise StopIteration.");
Yury Selivanov5376ba92015-06-22 12:19:30 -0400979
980PyDoc_STRVAR(coro_close_doc,
981"close() -> raise GeneratorExit inside coroutine.");
982
983static PyMethodDef coro_methods[] = {
984 {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc},
985 {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
986 {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
987 {NULL, NULL} /* Sentinel */
988};
989
990static PyAsyncMethods coro_as_async = {
991 (unaryfunc)coro_await, /* am_await */
992 0, /* am_aiter */
993 0 /* am_anext */
994};
995
996PyTypeObject PyCoro_Type = {
997 PyVarObject_HEAD_INIT(&PyType_Type, 0)
998 "coroutine", /* tp_name */
999 sizeof(PyCoroObject), /* tp_basicsize */
1000 0, /* tp_itemsize */
1001 /* methods */
1002 (destructor)gen_dealloc, /* tp_dealloc */
1003 0, /* tp_print */
1004 0, /* tp_getattr */
1005 0, /* tp_setattr */
1006 &coro_as_async, /* tp_as_async */
1007 (reprfunc)coro_repr, /* tp_repr */
1008 0, /* tp_as_number */
1009 0, /* tp_as_sequence */
1010 0, /* tp_as_mapping */
1011 0, /* tp_hash */
1012 0, /* tp_call */
1013 0, /* tp_str */
1014 PyObject_GenericGetAttr, /* tp_getattro */
1015 0, /* tp_setattro */
1016 0, /* tp_as_buffer */
1017 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1018 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
1019 0, /* tp_doc */
1020 (traverseproc)gen_traverse, /* tp_traverse */
1021 0, /* tp_clear */
1022 0, /* tp_richcompare */
1023 offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */
1024 0, /* tp_iter */
1025 0, /* tp_iternext */
1026 coro_methods, /* tp_methods */
1027 coro_memberlist, /* tp_members */
1028 coro_getsetlist, /* tp_getset */
1029 0, /* tp_base */
1030 0, /* tp_dict */
1031 0, /* tp_descr_get */
1032 0, /* tp_descr_set */
1033 0, /* tp_dictoffset */
1034 0, /* tp_init */
1035 0, /* tp_alloc */
1036 0, /* tp_new */
1037 0, /* tp_free */
1038 0, /* tp_is_gc */
1039 0, /* tp_bases */
1040 0, /* tp_mro */
1041 0, /* tp_cache */
1042 0, /* tp_subclasses */
1043 0, /* tp_weaklist */
1044 0, /* tp_del */
1045 0, /* tp_version_tag */
1046 _PyGen_Finalize, /* tp_finalize */
1047};
1048
1049static void
1050coro_wrapper_dealloc(PyCoroWrapper *cw)
1051{
1052 _PyObject_GC_UNTRACK((PyObject *)cw);
1053 Py_CLEAR(cw->cw_coroutine);
1054 PyObject_GC_Del(cw);
1055}
1056
1057static PyObject *
1058coro_wrapper_iternext(PyCoroWrapper *cw)
1059{
Yury Selivanov77c96812016-02-13 17:59:05 -05001060 return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0);
Yury Selivanov5376ba92015-06-22 12:19:30 -04001061}
1062
1063static PyObject *
1064coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
1065{
Yury Selivanov77c96812016-02-13 17:59:05 -05001066 return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0);
Yury Selivanov5376ba92015-06-22 12:19:30 -04001067}
1068
1069static PyObject *
1070coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
1071{
1072 return gen_throw((PyGenObject *)cw->cw_coroutine, args);
1073}
1074
1075static PyObject *
1076coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
1077{
1078 return gen_close((PyGenObject *)cw->cw_coroutine, args);
1079}
1080
1081static int
1082coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
1083{
1084 Py_VISIT((PyObject *)cw->cw_coroutine);
1085 return 0;
1086}
1087
1088static PyMethodDef coro_wrapper_methods[] = {
Yury Selivanov66f88282015-06-24 11:04:15 -04001089 {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
1090 {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
1091 {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
Yury Selivanov5376ba92015-06-22 12:19:30 -04001092 {NULL, NULL} /* Sentinel */
1093};
1094
1095PyTypeObject _PyCoroWrapper_Type = {
1096 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1097 "coroutine_wrapper",
1098 sizeof(PyCoroWrapper), /* tp_basicsize */
1099 0, /* tp_itemsize */
1100 (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */
1101 0, /* tp_print */
1102 0, /* tp_getattr */
1103 0, /* tp_setattr */
1104 0, /* tp_as_async */
1105 0, /* tp_repr */
1106 0, /* tp_as_number */
1107 0, /* tp_as_sequence */
1108 0, /* tp_as_mapping */
1109 0, /* tp_hash */
1110 0, /* tp_call */
1111 0, /* tp_str */
1112 PyObject_GenericGetAttr, /* tp_getattro */
1113 0, /* tp_setattro */
1114 0, /* tp_as_buffer */
1115 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1116 "A wrapper object implementing __await__ for coroutines.",
1117 (traverseproc)coro_wrapper_traverse, /* tp_traverse */
1118 0, /* tp_clear */
1119 0, /* tp_richcompare */
1120 0, /* tp_weaklistoffset */
1121 PyObject_SelfIter, /* tp_iter */
1122 (iternextfunc)coro_wrapper_iternext, /* tp_iternext */
1123 coro_wrapper_methods, /* tp_methods */
1124 0, /* tp_members */
1125 0, /* tp_getset */
1126 0, /* tp_base */
1127 0, /* tp_dict */
1128 0, /* tp_descr_get */
1129 0, /* tp_descr_set */
1130 0, /* tp_dictoffset */
1131 0, /* tp_init */
1132 0, /* tp_alloc */
1133 0, /* tp_new */
Yury Selivanov33499b72016-11-08 19:19:28 -05001134 0, /* tp_free */
Yury Selivanov5376ba92015-06-22 12:19:30 -04001135};
1136
1137PyObject *
1138PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1139{
1140 return gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
1141}
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001142
1143
1144/* __aiter__ wrapper; see http://bugs.python.org/issue27243 for details. */
1145
1146typedef struct {
1147 PyObject_HEAD
Yury Selivanoveb636452016-09-08 22:01:51 -07001148 PyObject *ags_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001149} PyAIterWrapper;
1150
1151
1152static PyObject *
1153aiter_wrapper_iternext(PyAIterWrapper *aw)
1154{
Serhiy Storchaka60e49aa2016-11-06 18:47:03 +02001155 _PyGen_SetStopIterationValue(aw->ags_aiter);
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001156 return NULL;
1157}
1158
1159static int
1160aiter_wrapper_traverse(PyAIterWrapper *aw, visitproc visit, void *arg)
1161{
Yury Selivanoveb636452016-09-08 22:01:51 -07001162 Py_VISIT((PyObject *)aw->ags_aiter);
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001163 return 0;
1164}
1165
1166static void
1167aiter_wrapper_dealloc(PyAIterWrapper *aw)
1168{
1169 _PyObject_GC_UNTRACK((PyObject *)aw);
Yury Selivanoveb636452016-09-08 22:01:51 -07001170 Py_CLEAR(aw->ags_aiter);
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001171 PyObject_GC_Del(aw);
1172}
1173
1174static PyAsyncMethods aiter_wrapper_as_async = {
1175 PyObject_SelfIter, /* am_await */
1176 0, /* am_aiter */
1177 0 /* am_anext */
1178};
1179
1180PyTypeObject _PyAIterWrapper_Type = {
1181 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1182 "aiter_wrapper",
1183 sizeof(PyAIterWrapper), /* tp_basicsize */
1184 0, /* tp_itemsize */
1185 (destructor)aiter_wrapper_dealloc, /* destructor tp_dealloc */
1186 0, /* tp_print */
1187 0, /* tp_getattr */
1188 0, /* tp_setattr */
1189 &aiter_wrapper_as_async, /* tp_as_async */
1190 0, /* tp_repr */
1191 0, /* tp_as_number */
1192 0, /* tp_as_sequence */
1193 0, /* tp_as_mapping */
1194 0, /* tp_hash */
1195 0, /* tp_call */
1196 0, /* tp_str */
1197 PyObject_GenericGetAttr, /* tp_getattro */
1198 0, /* tp_setattro */
1199 0, /* tp_as_buffer */
1200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1201 "A wrapper object for __aiter__ bakwards compatibility.",
1202 (traverseproc)aiter_wrapper_traverse, /* tp_traverse */
1203 0, /* tp_clear */
1204 0, /* tp_richcompare */
1205 0, /* tp_weaklistoffset */
1206 PyObject_SelfIter, /* tp_iter */
1207 (iternextfunc)aiter_wrapper_iternext, /* tp_iternext */
1208 0, /* tp_methods */
1209 0, /* tp_members */
1210 0, /* tp_getset */
1211 0, /* tp_base */
1212 0, /* tp_dict */
1213 0, /* tp_descr_get */
1214 0, /* tp_descr_set */
1215 0, /* tp_dictoffset */
1216 0, /* tp_init */
1217 0, /* tp_alloc */
1218 0, /* tp_new */
Yury Selivanov33499b72016-11-08 19:19:28 -05001219 0, /* tp_free */
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001220};
1221
1222
1223PyObject *
1224_PyAIterWrapper_New(PyObject *aiter)
1225{
1226 PyAIterWrapper *aw = PyObject_GC_New(PyAIterWrapper,
1227 &_PyAIterWrapper_Type);
1228 if (aw == NULL) {
1229 return NULL;
1230 }
1231 Py_INCREF(aiter);
Yury Selivanoveb636452016-09-08 22:01:51 -07001232 aw->ags_aiter = aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001233 _PyObject_GC_TRACK(aw);
1234 return (PyObject *)aw;
1235}
Yury Selivanoveb636452016-09-08 22:01:51 -07001236
1237
1238/* ========= Asynchronous Generators ========= */
1239
1240
1241typedef enum {
1242 AWAITABLE_STATE_INIT, /* new awaitable, has not yet been iterated */
1243 AWAITABLE_STATE_ITER, /* being iterated */
1244 AWAITABLE_STATE_CLOSED, /* closed */
1245} AwaitableState;
1246
1247
1248typedef struct {
1249 PyObject_HEAD
1250 PyAsyncGenObject *ags_gen;
1251
1252 /* Can be NULL, when in the __anext__() mode
1253 (equivalent of "asend(None)") */
1254 PyObject *ags_sendval;
1255
1256 AwaitableState ags_state;
1257} PyAsyncGenASend;
1258
1259
1260typedef struct {
1261 PyObject_HEAD
1262 PyAsyncGenObject *agt_gen;
1263
1264 /* Can be NULL, when in the "aclose()" mode
1265 (equivalent of "athrow(GeneratorExit)") */
1266 PyObject *agt_args;
1267
1268 AwaitableState agt_state;
1269} PyAsyncGenAThrow;
1270
1271
1272typedef struct {
1273 PyObject_HEAD
1274 PyObject *agw_val;
1275} _PyAsyncGenWrappedValue;
1276
1277
1278#ifndef _PyAsyncGen_MAXFREELIST
1279#define _PyAsyncGen_MAXFREELIST 80
1280#endif
1281
1282/* Freelists boost performance 6-10%; they also reduce memory
1283 fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend
1284 are short-living objects that are instantiated for every
1285 __anext__ call.
1286*/
1287
1288static _PyAsyncGenWrappedValue *ag_value_freelist[_PyAsyncGen_MAXFREELIST];
1289static int ag_value_freelist_free = 0;
1290
1291static PyAsyncGenASend *ag_asend_freelist[_PyAsyncGen_MAXFREELIST];
1292static int ag_asend_freelist_free = 0;
1293
1294#define _PyAsyncGenWrappedValue_CheckExact(o) \
1295 (Py_TYPE(o) == &_PyAsyncGenWrappedValue_Type)
1296
1297#define PyAsyncGenASend_CheckExact(o) \
1298 (Py_TYPE(o) == &_PyAsyncGenASend_Type)
1299
1300
1301static int
1302async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg)
1303{
1304 Py_VISIT(gen->ag_finalizer);
1305 return gen_traverse((PyGenObject*)gen, visit, arg);
1306}
1307
1308
1309static PyObject *
1310async_gen_repr(PyAsyncGenObject *o)
1311{
1312 return PyUnicode_FromFormat("<async_generator object %S at %p>",
1313 o->ag_qualname, o);
1314}
1315
1316
1317static int
1318async_gen_init_hooks(PyAsyncGenObject *o)
1319{
1320 PyThreadState *tstate;
1321 PyObject *finalizer;
1322 PyObject *firstiter;
1323
1324 if (o->ag_hooks_inited) {
1325 return 0;
1326 }
1327
1328 o->ag_hooks_inited = 1;
1329
1330 tstate = PyThreadState_GET();
1331
1332 finalizer = tstate->async_gen_finalizer;
1333 if (finalizer) {
1334 Py_INCREF(finalizer);
1335 o->ag_finalizer = finalizer;
1336 }
1337
1338 firstiter = tstate->async_gen_firstiter;
1339 if (firstiter) {
1340 PyObject *res;
1341
1342 Py_INCREF(firstiter);
Victor Stinner7bfb42d2016-12-05 17:04:32 +01001343 res = PyObject_CallFunctionObjArgs(firstiter, o, NULL);
Yury Selivanoveb636452016-09-08 22:01:51 -07001344 Py_DECREF(firstiter);
1345 if (res == NULL) {
1346 return 1;
1347 }
1348 Py_DECREF(res);
1349 }
1350
1351 return 0;
1352}
1353
1354
1355static PyObject *
1356async_gen_anext(PyAsyncGenObject *o)
1357{
1358 if (async_gen_init_hooks(o)) {
1359 return NULL;
1360 }
1361 return async_gen_asend_new(o, NULL);
1362}
1363
1364
1365static PyObject *
1366async_gen_asend(PyAsyncGenObject *o, PyObject *arg)
1367{
1368 if (async_gen_init_hooks(o)) {
1369 return NULL;
1370 }
1371 return async_gen_asend_new(o, arg);
1372}
1373
1374
1375static PyObject *
1376async_gen_aclose(PyAsyncGenObject *o, PyObject *arg)
1377{
1378 if (async_gen_init_hooks(o)) {
1379 return NULL;
1380 }
1381 return async_gen_athrow_new(o, NULL);
1382}
1383
1384static PyObject *
1385async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
1386{
1387 if (async_gen_init_hooks(o)) {
1388 return NULL;
1389 }
1390 return async_gen_athrow_new(o, args);
1391}
1392
1393
1394static PyGetSetDef async_gen_getsetlist[] = {
1395 {"__name__", (getter)gen_get_name, (setter)gen_set_name,
1396 PyDoc_STR("name of the async generator")},
1397 {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
1398 PyDoc_STR("qualified name of the async generator")},
1399 {"ag_await", (getter)coro_get_cr_await, NULL,
1400 PyDoc_STR("object being awaited on, or None")},
1401 {NULL} /* Sentinel */
1402};
1403
1404static PyMemberDef async_gen_memberlist[] = {
1405 {"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY},
1406 {"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running), READONLY},
1407 {"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY},
1408 {NULL} /* Sentinel */
1409};
1410
1411PyDoc_STRVAR(async_aclose_doc,
1412"aclose() -> raise GeneratorExit inside generator.");
1413
1414PyDoc_STRVAR(async_asend_doc,
1415"asend(v) -> send 'v' in generator.");
1416
1417PyDoc_STRVAR(async_athrow_doc,
1418"athrow(typ[,val[,tb]]) -> raise exception in generator.");
1419
1420static PyMethodDef async_gen_methods[] = {
1421 {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
1422 {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
1423 {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
1424 {NULL, NULL} /* Sentinel */
1425};
1426
1427
1428static PyAsyncMethods async_gen_as_async = {
1429 0, /* am_await */
1430 PyObject_SelfIter, /* am_aiter */
1431 (unaryfunc)async_gen_anext /* am_anext */
1432};
1433
1434
1435PyTypeObject PyAsyncGen_Type = {
1436 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1437 "async_generator", /* tp_name */
1438 sizeof(PyAsyncGenObject), /* tp_basicsize */
1439 0, /* tp_itemsize */
1440 /* methods */
1441 (destructor)gen_dealloc, /* tp_dealloc */
1442 0, /* tp_print */
1443 0, /* tp_getattr */
1444 0, /* tp_setattr */
1445 &async_gen_as_async, /* tp_as_async */
1446 (reprfunc)async_gen_repr, /* tp_repr */
1447 0, /* tp_as_number */
1448 0, /* tp_as_sequence */
1449 0, /* tp_as_mapping */
1450 0, /* tp_hash */
1451 0, /* tp_call */
1452 0, /* tp_str */
1453 PyObject_GenericGetAttr, /* tp_getattro */
1454 0, /* tp_setattro */
1455 0, /* tp_as_buffer */
1456 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1457 Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
1458 0, /* tp_doc */
1459 (traverseproc)async_gen_traverse, /* tp_traverse */
1460 0, /* tp_clear */
1461 0, /* tp_richcompare */
1462 offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */
1463 0, /* tp_iter */
1464 0, /* tp_iternext */
1465 async_gen_methods, /* tp_methods */
1466 async_gen_memberlist, /* tp_members */
1467 async_gen_getsetlist, /* tp_getset */
1468 0, /* tp_base */
1469 0, /* tp_dict */
1470 0, /* tp_descr_get */
1471 0, /* tp_descr_set */
1472 0, /* tp_dictoffset */
1473 0, /* tp_init */
1474 0, /* tp_alloc */
1475 0, /* tp_new */
1476 0, /* tp_free */
1477 0, /* tp_is_gc */
1478 0, /* tp_bases */
1479 0, /* tp_mro */
1480 0, /* tp_cache */
1481 0, /* tp_subclasses */
1482 0, /* tp_weaklist */
1483 0, /* tp_del */
1484 0, /* tp_version_tag */
1485 _PyGen_Finalize, /* tp_finalize */
1486};
1487
1488
1489PyObject *
1490PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1491{
1492 PyAsyncGenObject *o;
1493 o = (PyAsyncGenObject *)gen_new_with_qualname(
1494 &PyAsyncGen_Type, f, name, qualname);
1495 if (o == NULL) {
1496 return NULL;
1497 }
1498 o->ag_finalizer = NULL;
1499 o->ag_closed = 0;
1500 o->ag_hooks_inited = 0;
1501 return (PyObject*)o;
1502}
1503
1504
1505int
1506PyAsyncGen_ClearFreeLists(void)
1507{
1508 int ret = ag_value_freelist_free + ag_asend_freelist_free;
1509
1510 while (ag_value_freelist_free) {
1511 _PyAsyncGenWrappedValue *o;
1512 o = ag_value_freelist[--ag_value_freelist_free];
1513 assert(_PyAsyncGenWrappedValue_CheckExact(o));
Yury Selivanov29310c42016-11-08 19:46:22 -05001514 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001515 }
1516
1517 while (ag_asend_freelist_free) {
1518 PyAsyncGenASend *o;
1519 o = ag_asend_freelist[--ag_asend_freelist_free];
1520 assert(Py_TYPE(o) == &_PyAsyncGenASend_Type);
Yury Selivanov29310c42016-11-08 19:46:22 -05001521 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001522 }
1523
1524 return ret;
1525}
1526
1527void
1528PyAsyncGen_Fini(void)
1529{
1530 PyAsyncGen_ClearFreeLists();
1531}
1532
1533
1534static PyObject *
1535async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
1536{
1537 if (result == NULL) {
1538 if (!PyErr_Occurred()) {
1539 PyErr_SetNone(PyExc_StopAsyncIteration);
1540 }
1541
1542 if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)
1543 || PyErr_ExceptionMatches(PyExc_GeneratorExit)
1544 ) {
1545 gen->ag_closed = 1;
1546 }
1547
1548 return NULL;
1549 }
1550
1551 if (_PyAsyncGenWrappedValue_CheckExact(result)) {
1552 /* async yield */
Serhiy Storchaka60e49aa2016-11-06 18:47:03 +02001553 _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val);
Yury Selivanoveb636452016-09-08 22:01:51 -07001554 Py_DECREF(result);
Yury Selivanoveb636452016-09-08 22:01:51 -07001555 return NULL;
1556 }
1557
1558 return result;
1559}
1560
1561
1562/* ---------- Async Generator ASend Awaitable ------------ */
1563
1564
1565static void
1566async_gen_asend_dealloc(PyAsyncGenASend *o)
1567{
Yury Selivanov29310c42016-11-08 19:46:22 -05001568 _PyObject_GC_UNTRACK((PyObject *)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001569 Py_CLEAR(o->ags_gen);
1570 Py_CLEAR(o->ags_sendval);
1571 if (ag_asend_freelist_free < _PyAsyncGen_MAXFREELIST) {
1572 assert(PyAsyncGenASend_CheckExact(o));
1573 ag_asend_freelist[ag_asend_freelist_free++] = o;
1574 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001575 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001576 }
1577}
1578
Yury Selivanov29310c42016-11-08 19:46:22 -05001579static int
1580async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg)
1581{
1582 Py_VISIT(o->ags_gen);
1583 Py_VISIT(o->ags_sendval);
1584 return 0;
1585}
1586
Yury Selivanoveb636452016-09-08 22:01:51 -07001587
1588static PyObject *
1589async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg)
1590{
1591 PyObject *result;
1592
1593 if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1594 PyErr_SetNone(PyExc_StopIteration);
1595 return NULL;
1596 }
1597
1598 if (o->ags_state == AWAITABLE_STATE_INIT) {
1599 if (arg == NULL || arg == Py_None) {
1600 arg = o->ags_sendval;
1601 }
1602 o->ags_state = AWAITABLE_STATE_ITER;
1603 }
1604
1605 result = gen_send_ex((PyGenObject*)o->ags_gen, arg, 0, 0);
1606 result = async_gen_unwrap_value(o->ags_gen, result);
1607
1608 if (result == NULL) {
1609 o->ags_state = AWAITABLE_STATE_CLOSED;
1610 }
1611
1612 return result;
1613}
1614
1615
1616static PyObject *
1617async_gen_asend_iternext(PyAsyncGenASend *o)
1618{
1619 return async_gen_asend_send(o, NULL);
1620}
1621
1622
1623static PyObject *
1624async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
1625{
1626 PyObject *result;
1627
1628 if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1629 PyErr_SetNone(PyExc_StopIteration);
1630 return NULL;
1631 }
1632
1633 result = gen_throw((PyGenObject*)o->ags_gen, args);
1634 result = async_gen_unwrap_value(o->ags_gen, result);
1635
1636 if (result == NULL) {
1637 o->ags_state = AWAITABLE_STATE_CLOSED;
1638 }
1639
1640 return result;
1641}
1642
1643
1644static PyObject *
1645async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
1646{
1647 o->ags_state = AWAITABLE_STATE_CLOSED;
1648 Py_RETURN_NONE;
1649}
1650
1651
1652static PyMethodDef async_gen_asend_methods[] = {
1653 {"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
1654 {"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc},
1655 {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
1656 {NULL, NULL} /* Sentinel */
1657};
1658
1659
1660static PyAsyncMethods async_gen_asend_as_async = {
1661 PyObject_SelfIter, /* am_await */
1662 0, /* am_aiter */
1663 0 /* am_anext */
1664};
1665
1666
1667PyTypeObject _PyAsyncGenASend_Type = {
1668 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1669 "async_generator_asend", /* tp_name */
1670 sizeof(PyAsyncGenASend), /* tp_basicsize */
1671 0, /* tp_itemsize */
1672 /* methods */
1673 (destructor)async_gen_asend_dealloc, /* tp_dealloc */
1674 0, /* tp_print */
1675 0, /* tp_getattr */
1676 0, /* tp_setattr */
1677 &async_gen_asend_as_async, /* tp_as_async */
1678 0, /* tp_repr */
1679 0, /* tp_as_number */
1680 0, /* tp_as_sequence */
1681 0, /* tp_as_mapping */
1682 0, /* tp_hash */
1683 0, /* tp_call */
1684 0, /* tp_str */
1685 PyObject_GenericGetAttr, /* tp_getattro */
1686 0, /* tp_setattro */
1687 0, /* tp_as_buffer */
Yury Selivanov29310c42016-11-08 19:46:22 -05001688 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yury Selivanoveb636452016-09-08 22:01:51 -07001689 0, /* tp_doc */
Yury Selivanov29310c42016-11-08 19:46:22 -05001690 (traverseproc)async_gen_asend_traverse, /* tp_traverse */
Yury Selivanoveb636452016-09-08 22:01:51 -07001691 0, /* tp_clear */
1692 0, /* tp_richcompare */
1693 0, /* tp_weaklistoffset */
1694 PyObject_SelfIter, /* tp_iter */
1695 (iternextfunc)async_gen_asend_iternext, /* tp_iternext */
1696 async_gen_asend_methods, /* tp_methods */
1697 0, /* tp_members */
1698 0, /* tp_getset */
1699 0, /* tp_base */
1700 0, /* tp_dict */
1701 0, /* tp_descr_get */
1702 0, /* tp_descr_set */
1703 0, /* tp_dictoffset */
1704 0, /* tp_init */
1705 0, /* tp_alloc */
1706 0, /* tp_new */
1707};
1708
1709
1710static PyObject *
1711async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
1712{
1713 PyAsyncGenASend *o;
1714 if (ag_asend_freelist_free) {
1715 ag_asend_freelist_free--;
1716 o = ag_asend_freelist[ag_asend_freelist_free];
1717 _Py_NewReference((PyObject *)o);
1718 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001719 o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type);
Yury Selivanoveb636452016-09-08 22:01:51 -07001720 if (o == NULL) {
1721 return NULL;
1722 }
1723 }
1724
1725 Py_INCREF(gen);
1726 o->ags_gen = gen;
1727
1728 Py_XINCREF(sendval);
1729 o->ags_sendval = sendval;
1730
1731 o->ags_state = AWAITABLE_STATE_INIT;
Yury Selivanov29310c42016-11-08 19:46:22 -05001732
1733 _PyObject_GC_TRACK((PyObject*)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001734 return (PyObject*)o;
1735}
1736
1737
1738/* ---------- Async Generator Value Wrapper ------------ */
1739
1740
1741static void
1742async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
1743{
Yury Selivanov29310c42016-11-08 19:46:22 -05001744 _PyObject_GC_UNTRACK((PyObject *)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001745 Py_CLEAR(o->agw_val);
1746 if (ag_value_freelist_free < _PyAsyncGen_MAXFREELIST) {
1747 assert(_PyAsyncGenWrappedValue_CheckExact(o));
1748 ag_value_freelist[ag_value_freelist_free++] = o;
1749 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001750 PyObject_GC_Del(o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001751 }
1752}
1753
1754
Yury Selivanov29310c42016-11-08 19:46:22 -05001755static int
1756async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o,
1757 visitproc visit, void *arg)
1758{
1759 Py_VISIT(o->agw_val);
1760 return 0;
1761}
1762
1763
Yury Selivanoveb636452016-09-08 22:01:51 -07001764PyTypeObject _PyAsyncGenWrappedValue_Type = {
1765 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1766 "async_generator_wrapped_value", /* tp_name */
1767 sizeof(_PyAsyncGenWrappedValue), /* tp_basicsize */
1768 0, /* tp_itemsize */
1769 /* methods */
1770 (destructor)async_gen_wrapped_val_dealloc, /* tp_dealloc */
1771 0, /* tp_print */
1772 0, /* tp_getattr */
1773 0, /* tp_setattr */
1774 0, /* tp_as_async */
1775 0, /* tp_repr */
1776 0, /* tp_as_number */
1777 0, /* tp_as_sequence */
1778 0, /* tp_as_mapping */
1779 0, /* tp_hash */
1780 0, /* tp_call */
1781 0, /* tp_str */
1782 PyObject_GenericGetAttr, /* tp_getattro */
1783 0, /* tp_setattro */
1784 0, /* tp_as_buffer */
Yury Selivanov29310c42016-11-08 19:46:22 -05001785 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yury Selivanoveb636452016-09-08 22:01:51 -07001786 0, /* tp_doc */
Yury Selivanov29310c42016-11-08 19:46:22 -05001787 (traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */
Yury Selivanoveb636452016-09-08 22:01:51 -07001788 0, /* tp_clear */
1789 0, /* tp_richcompare */
1790 0, /* tp_weaklistoffset */
1791 0, /* tp_iter */
1792 0, /* tp_iternext */
1793 0, /* tp_methods */
1794 0, /* tp_members */
1795 0, /* tp_getset */
1796 0, /* tp_base */
1797 0, /* tp_dict */
1798 0, /* tp_descr_get */
1799 0, /* tp_descr_set */
1800 0, /* tp_dictoffset */
1801 0, /* tp_init */
1802 0, /* tp_alloc */
1803 0, /* tp_new */
1804};
1805
1806
1807PyObject *
1808_PyAsyncGenValueWrapperNew(PyObject *val)
1809{
1810 _PyAsyncGenWrappedValue *o;
1811 assert(val);
1812
1813 if (ag_value_freelist_free) {
1814 ag_value_freelist_free--;
1815 o = ag_value_freelist[ag_value_freelist_free];
1816 assert(_PyAsyncGenWrappedValue_CheckExact(o));
1817 _Py_NewReference((PyObject*)o);
1818 } else {
Yury Selivanov29310c42016-11-08 19:46:22 -05001819 o = PyObject_GC_New(_PyAsyncGenWrappedValue,
1820 &_PyAsyncGenWrappedValue_Type);
Yury Selivanoveb636452016-09-08 22:01:51 -07001821 if (o == NULL) {
1822 return NULL;
1823 }
1824 }
1825 o->agw_val = val;
1826 Py_INCREF(val);
Yury Selivanov29310c42016-11-08 19:46:22 -05001827 _PyObject_GC_TRACK((PyObject*)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001828 return (PyObject*)o;
1829}
1830
1831
1832/* ---------- Async Generator AThrow awaitable ------------ */
1833
1834
1835static void
1836async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
1837{
Yury Selivanov29310c42016-11-08 19:46:22 -05001838 _PyObject_GC_UNTRACK((PyObject *)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07001839 Py_CLEAR(o->agt_gen);
1840 Py_CLEAR(o->agt_args);
Yury Selivanov29310c42016-11-08 19:46:22 -05001841 PyObject_GC_Del(o);
1842}
1843
1844
1845static int
1846async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg)
1847{
1848 Py_VISIT(o->agt_gen);
1849 Py_VISIT(o->agt_args);
1850 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07001851}
1852
1853
1854static PyObject *
1855async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
1856{
1857 PyGenObject *gen = (PyGenObject*)o->agt_gen;
1858 PyFrameObject *f = gen->gi_frame;
1859 PyObject *retval;
1860
1861 if (f == NULL || f->f_stacktop == NULL ||
1862 o->agt_state == AWAITABLE_STATE_CLOSED) {
1863 PyErr_SetNone(PyExc_StopIteration);
1864 return NULL;
1865 }
1866
1867 if (o->agt_state == AWAITABLE_STATE_INIT) {
1868 if (o->agt_gen->ag_closed) {
1869 PyErr_SetNone(PyExc_StopIteration);
1870 return NULL;
1871 }
1872
1873 if (arg != Py_None) {
1874 PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
1875 return NULL;
1876 }
1877
1878 o->agt_state = AWAITABLE_STATE_ITER;
1879
1880 if (o->agt_args == NULL) {
1881 /* aclose() mode */
1882 o->agt_gen->ag_closed = 1;
1883
1884 retval = _gen_throw((PyGenObject *)gen,
1885 0, /* Do not close generator when
1886 PyExc_GeneratorExit is passed */
1887 PyExc_GeneratorExit, NULL, NULL);
1888
1889 if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
1890 Py_DECREF(retval);
1891 goto yield_close;
1892 }
1893 } else {
1894 PyObject *typ;
1895 PyObject *tb = NULL;
1896 PyObject *val = NULL;
1897
1898 if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3,
1899 &typ, &val, &tb)) {
1900 return NULL;
1901 }
1902
1903 retval = _gen_throw((PyGenObject *)gen,
1904 0, /* Do not close generator when
1905 PyExc_GeneratorExit is passed */
1906 typ, val, tb);
1907 retval = async_gen_unwrap_value(o->agt_gen, retval);
1908 }
1909 if (retval == NULL) {
1910 goto check_error;
1911 }
1912 return retval;
1913 }
1914
1915 assert(o->agt_state == AWAITABLE_STATE_ITER);
1916
1917 retval = gen_send_ex((PyGenObject *)gen, arg, 0, 0);
1918 if (o->agt_args) {
1919 return async_gen_unwrap_value(o->agt_gen, retval);
1920 } else {
1921 /* aclose() mode */
1922 if (retval) {
1923 if (_PyAsyncGenWrappedValue_CheckExact(retval)) {
1924 Py_DECREF(retval);
1925 goto yield_close;
1926 }
1927 else {
1928 return retval;
1929 }
1930 }
1931 else {
1932 goto check_error;
1933 }
1934 }
1935
1936yield_close:
1937 PyErr_SetString(
1938 PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
1939 return NULL;
1940
1941check_error:
Yury Selivanov41782e42016-11-16 18:16:17 -05001942 if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
1943 o->agt_state = AWAITABLE_STATE_CLOSED;
1944 if (o->agt_args == NULL) {
1945 /* when aclose() is called we don't want to propagate
1946 StopAsyncIteration; just raise StopIteration, signalling
1947 that 'aclose()' is done. */
1948 PyErr_Clear();
1949 PyErr_SetNone(PyExc_StopIteration);
1950 }
1951 }
1952 else if (PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
Yury Selivanoveb636452016-09-08 22:01:51 -07001953 o->agt_state = AWAITABLE_STATE_CLOSED;
1954 PyErr_Clear(); /* ignore these errors */
1955 PyErr_SetNone(PyExc_StopIteration);
1956 }
1957 return NULL;
1958}
1959
1960
1961static PyObject *
1962async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
1963{
1964 PyObject *retval;
1965
1966 if (o->agt_state == AWAITABLE_STATE_INIT) {
1967 PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
1968 return NULL;
1969 }
1970
1971 if (o->agt_state == AWAITABLE_STATE_CLOSED) {
1972 PyErr_SetNone(PyExc_StopIteration);
1973 return NULL;
1974 }
1975
1976 retval = gen_throw((PyGenObject*)o->agt_gen, args);
1977 if (o->agt_args) {
1978 return async_gen_unwrap_value(o->agt_gen, retval);
1979 } else {
1980 /* aclose() mode */
1981 if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
1982 Py_DECREF(retval);
1983 PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
1984 return NULL;
1985 }
1986 return retval;
1987 }
1988}
1989
1990
1991static PyObject *
1992async_gen_athrow_iternext(PyAsyncGenAThrow *o)
1993{
1994 return async_gen_athrow_send(o, Py_None);
1995}
1996
1997
1998static PyObject *
1999async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
2000{
2001 o->agt_state = AWAITABLE_STATE_CLOSED;
2002 Py_RETURN_NONE;
2003}
2004
2005
2006static PyMethodDef async_gen_athrow_methods[] = {
2007 {"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
2008 {"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc},
2009 {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
2010 {NULL, NULL} /* Sentinel */
2011};
2012
2013
2014static PyAsyncMethods async_gen_athrow_as_async = {
2015 PyObject_SelfIter, /* am_await */
2016 0, /* am_aiter */
2017 0 /* am_anext */
2018};
2019
2020
2021PyTypeObject _PyAsyncGenAThrow_Type = {
2022 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2023 "async_generator_athrow", /* tp_name */
2024 sizeof(PyAsyncGenAThrow), /* tp_basicsize */
2025 0, /* tp_itemsize */
2026 /* methods */
2027 (destructor)async_gen_athrow_dealloc, /* tp_dealloc */
2028 0, /* tp_print */
2029 0, /* tp_getattr */
2030 0, /* tp_setattr */
2031 &async_gen_athrow_as_async, /* tp_as_async */
2032 0, /* tp_repr */
2033 0, /* tp_as_number */
2034 0, /* tp_as_sequence */
2035 0, /* tp_as_mapping */
2036 0, /* tp_hash */
2037 0, /* tp_call */
2038 0, /* tp_str */
2039 PyObject_GenericGetAttr, /* tp_getattro */
2040 0, /* tp_setattro */
2041 0, /* tp_as_buffer */
Yury Selivanov29310c42016-11-08 19:46:22 -05002042 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Yury Selivanoveb636452016-09-08 22:01:51 -07002043 0, /* tp_doc */
Yury Selivanov29310c42016-11-08 19:46:22 -05002044 (traverseproc)async_gen_athrow_traverse, /* tp_traverse */
Yury Selivanoveb636452016-09-08 22:01:51 -07002045 0, /* tp_clear */
2046 0, /* tp_richcompare */
2047 0, /* tp_weaklistoffset */
2048 PyObject_SelfIter, /* tp_iter */
2049 (iternextfunc)async_gen_athrow_iternext, /* tp_iternext */
2050 async_gen_athrow_methods, /* tp_methods */
2051 0, /* tp_members */
2052 0, /* tp_getset */
2053 0, /* tp_base */
2054 0, /* tp_dict */
2055 0, /* tp_descr_get */
2056 0, /* tp_descr_set */
2057 0, /* tp_dictoffset */
2058 0, /* tp_init */
2059 0, /* tp_alloc */
2060 0, /* tp_new */
2061};
2062
2063
2064static PyObject *
2065async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
2066{
2067 PyAsyncGenAThrow *o;
Yury Selivanov29310c42016-11-08 19:46:22 -05002068 o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type);
Yury Selivanoveb636452016-09-08 22:01:51 -07002069 if (o == NULL) {
2070 return NULL;
2071 }
2072 o->agt_gen = gen;
2073 o->agt_args = args;
2074 o->agt_state = AWAITABLE_STATE_INIT;
2075 Py_INCREF(gen);
2076 Py_XINCREF(args);
Yury Selivanov29310c42016-11-08 19:46:22 -05002077 _PyObject_GC_TRACK((PyObject*)o);
Yury Selivanoveb636452016-09-08 22:01:51 -07002078 return (PyObject*)o;
2079}