• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Generator object implementation */
2 
3 #include "Python.h"
4 #include "pycore_object.h"
5 #include "pycore_pystate.h"
6 #include "frameobject.h"
7 #include "structmember.h"
8 #include "opcode.h"
9 
10 static PyObject *gen_close(PyGenObject *, PyObject *);
11 static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
12 static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
13 
14 static char *NON_INIT_CORO_MSG = "can't send non-None value to a "
15                                  "just-started coroutine";
16 
17 static char *ASYNC_GEN_IGNORED_EXIT_MSG =
18                                  "async generator ignored GeneratorExit";
19 
20 static inline int
exc_state_traverse(_PyErr_StackItem * exc_state,visitproc visit,void * arg)21 exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg)
22 {
23     Py_VISIT(exc_state->exc_type);
24     Py_VISIT(exc_state->exc_value);
25     Py_VISIT(exc_state->exc_traceback);
26     return 0;
27 }
28 
29 static int
gen_traverse(PyGenObject * gen,visitproc visit,void * arg)30 gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
31 {
32     Py_VISIT((PyObject *)gen->gi_frame);
33     Py_VISIT(gen->gi_code);
34     Py_VISIT(gen->gi_name);
35     Py_VISIT(gen->gi_qualname);
36     /* No need to visit cr_origin, because it's just tuples/str/int, so can't
37        participate in a reference cycle. */
38     return exc_state_traverse(&gen->gi_exc_state, visit, arg);
39 }
40 
41 void
_PyGen_Finalize(PyObject * self)42 _PyGen_Finalize(PyObject *self)
43 {
44     PyGenObject *gen = (PyGenObject *)self;
45     PyObject *res = NULL;
46     PyObject *error_type, *error_value, *error_traceback;
47 
48     if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) {
49         /* Generator isn't paused, so no need to close */
50         return;
51     }
52 
53     if (PyAsyncGen_CheckExact(self)) {
54         PyAsyncGenObject *agen = (PyAsyncGenObject*)self;
55         PyObject *finalizer = agen->ag_finalizer;
56         if (finalizer && !agen->ag_closed) {
57             /* Save the current exception, if any. */
58             PyErr_Fetch(&error_type, &error_value, &error_traceback);
59 
60             res = PyObject_CallFunctionObjArgs(finalizer, self, NULL);
61 
62             if (res == NULL) {
63                 PyErr_WriteUnraisable(self);
64             } else {
65                 Py_DECREF(res);
66             }
67             /* Restore the saved exception. */
68             PyErr_Restore(error_type, error_value, error_traceback);
69             return;
70         }
71     }
72 
73     /* Save the current exception, if any. */
74     PyErr_Fetch(&error_type, &error_value, &error_traceback);
75 
76     /* If `gen` is a coroutine, and if it was never awaited on,
77        issue a RuntimeWarning. */
78     if (gen->gi_code != NULL &&
79         ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
80         gen->gi_frame->f_lasti == -1)
81     {
82         _PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
83     }
84     else {
85         res = gen_close(gen, NULL);
86     }
87 
88     if (res == NULL) {
89         if (PyErr_Occurred()) {
90             PyErr_WriteUnraisable(self);
91         }
92     }
93     else {
94         Py_DECREF(res);
95     }
96 
97     /* Restore the saved exception. */
98     PyErr_Restore(error_type, error_value, error_traceback);
99 }
100 
101 static inline void
exc_state_clear(_PyErr_StackItem * exc_state)102 exc_state_clear(_PyErr_StackItem *exc_state)
103 {
104     PyObject *t, *v, *tb;
105     t = exc_state->exc_type;
106     v = exc_state->exc_value;
107     tb = exc_state->exc_traceback;
108     exc_state->exc_type = NULL;
109     exc_state->exc_value = NULL;
110     exc_state->exc_traceback = NULL;
111     Py_XDECREF(t);
112     Py_XDECREF(v);
113     Py_XDECREF(tb);
114 }
115 
116 static void
gen_dealloc(PyGenObject * gen)117 gen_dealloc(PyGenObject *gen)
118 {
119     PyObject *self = (PyObject *) gen;
120 
121     _PyObject_GC_UNTRACK(gen);
122 
123     if (gen->gi_weakreflist != NULL)
124         PyObject_ClearWeakRefs(self);
125 
126     _PyObject_GC_TRACK(self);
127 
128     if (PyObject_CallFinalizerFromDealloc(self))
129         return;                     /* resurrected.  :( */
130 
131     _PyObject_GC_UNTRACK(self);
132     if (PyAsyncGen_CheckExact(gen)) {
133         /* We have to handle this case for asynchronous generators
134            right here, because this code has to be between UNTRACK
135            and GC_Del. */
136         Py_CLEAR(((PyAsyncGenObject*)gen)->ag_finalizer);
137     }
138     if (gen->gi_frame != NULL) {
139         gen->gi_frame->f_gen = NULL;
140         Py_CLEAR(gen->gi_frame);
141     }
142     if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) {
143         Py_CLEAR(((PyCoroObject *)gen)->cr_origin);
144     }
145     Py_CLEAR(gen->gi_code);
146     Py_CLEAR(gen->gi_name);
147     Py_CLEAR(gen->gi_qualname);
148     exc_state_clear(&gen->gi_exc_state);
149     PyObject_GC_Del(gen);
150 }
151 
152 static PyObject *
gen_send_ex(PyGenObject * gen,PyObject * arg,int exc,int closing)153 gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
154 {
155     PyThreadState *tstate = _PyThreadState_GET();
156     PyFrameObject *f = gen->gi_frame;
157     PyObject *result;
158 
159     if (gen->gi_running) {
160         const char *msg = "generator already executing";
161         if (PyCoro_CheckExact(gen)) {
162             msg = "coroutine already executing";
163         }
164         else if (PyAsyncGen_CheckExact(gen)) {
165             msg = "async generator already executing";
166         }
167         PyErr_SetString(PyExc_ValueError, msg);
168         return NULL;
169     }
170     if (f == NULL || f->f_stacktop == NULL) {
171         if (PyCoro_CheckExact(gen) && !closing) {
172             /* `gen` is an exhausted coroutine: raise an error,
173                except when called from gen_close(), which should
174                always be a silent method. */
175             PyErr_SetString(
176                 PyExc_RuntimeError,
177                 "cannot reuse already awaited coroutine");
178         }
179         else if (arg && !exc) {
180             /* `gen` is an exhausted generator:
181                only set exception if called from send(). */
182             if (PyAsyncGen_CheckExact(gen)) {
183                 PyErr_SetNone(PyExc_StopAsyncIteration);
184             }
185             else {
186                 PyErr_SetNone(PyExc_StopIteration);
187             }
188         }
189         return NULL;
190     }
191 
192     if (f->f_lasti == -1) {
193         if (arg && arg != Py_None) {
194             const char *msg = "can't send non-None value to a "
195                               "just-started generator";
196             if (PyCoro_CheckExact(gen)) {
197                 msg = NON_INIT_CORO_MSG;
198             }
199             else if (PyAsyncGen_CheckExact(gen)) {
200                 msg = "can't send non-None value to a "
201                       "just-started async generator";
202             }
203             PyErr_SetString(PyExc_TypeError, msg);
204             return NULL;
205         }
206     } else {
207         /* Push arg onto the frame's value stack */
208         result = arg ? arg : Py_None;
209         Py_INCREF(result);
210         *(f->f_stacktop++) = result;
211     }
212 
213     /* Generators always return to their most recent caller, not
214      * necessarily their creator. */
215     Py_XINCREF(tstate->frame);
216     assert(f->f_back == NULL);
217     f->f_back = tstate->frame;
218 
219     gen->gi_running = 1;
220     gen->gi_exc_state.previous_item = tstate->exc_info;
221     tstate->exc_info = &gen->gi_exc_state;
222     result = PyEval_EvalFrameEx(f, exc);
223     tstate->exc_info = gen->gi_exc_state.previous_item;
224     gen->gi_exc_state.previous_item = NULL;
225     gen->gi_running = 0;
226 
227     /* Don't keep the reference to f_back any longer than necessary.  It
228      * may keep a chain of frames alive or it could create a reference
229      * cycle. */
230     assert(f->f_back == tstate->frame);
231     Py_CLEAR(f->f_back);
232 
233     /* If the generator just returned (as opposed to yielding), signal
234      * that the generator is exhausted. */
235     if (result && f->f_stacktop == NULL) {
236         if (result == Py_None) {
237             /* Delay exception instantiation if we can */
238             if (PyAsyncGen_CheckExact(gen)) {
239                 PyErr_SetNone(PyExc_StopAsyncIteration);
240             }
241             else {
242                 PyErr_SetNone(PyExc_StopIteration);
243             }
244         }
245         else {
246             /* Async generators cannot return anything but None */
247             assert(!PyAsyncGen_CheckExact(gen));
248             _PyGen_SetStopIterationValue(result);
249         }
250         Py_CLEAR(result);
251     }
252     else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
253         const char *msg = "generator raised StopIteration";
254         if (PyCoro_CheckExact(gen)) {
255             msg = "coroutine raised StopIteration";
256         }
257         else if PyAsyncGen_CheckExact(gen) {
258             msg = "async generator raised StopIteration";
259         }
260         _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
261 
262     }
263     else if (!result && PyAsyncGen_CheckExact(gen) &&
264              PyErr_ExceptionMatches(PyExc_StopAsyncIteration))
265     {
266         /* code in `gen` raised a StopAsyncIteration error:
267            raise a RuntimeError.
268         */
269         const char *msg = "async generator raised StopAsyncIteration";
270         _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
271     }
272 
273     if (!result || f->f_stacktop == NULL) {
274         /* generator can't be rerun, so release the frame */
275         /* first clean reference cycle through stored exception traceback */
276         exc_state_clear(&gen->gi_exc_state);
277         gen->gi_frame->f_gen = NULL;
278         gen->gi_frame = NULL;
279         Py_DECREF(f);
280     }
281 
282     return result;
283 }
284 
285 PyDoc_STRVAR(send_doc,
286 "send(arg) -> send 'arg' into generator,\n\
287 return next yielded value or raise StopIteration.");
288 
289 PyObject *
_PyGen_Send(PyGenObject * gen,PyObject * arg)290 _PyGen_Send(PyGenObject *gen, PyObject *arg)
291 {
292     return gen_send_ex(gen, arg, 0, 0);
293 }
294 
295 PyDoc_STRVAR(close_doc,
296 "close() -> raise GeneratorExit inside generator.");
297 
298 /*
299  *   This helper function is used by gen_close and gen_throw to
300  *   close a subiterator being delegated to by yield-from.
301  */
302 
303 static int
gen_close_iter(PyObject * yf)304 gen_close_iter(PyObject *yf)
305 {
306     PyObject *retval = NULL;
307     _Py_IDENTIFIER(close);
308 
309     if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
310         retval = gen_close((PyGenObject *)yf, NULL);
311         if (retval == NULL)
312             return -1;
313     }
314     else {
315         PyObject *meth;
316         if (_PyObject_LookupAttrId(yf, &PyId_close, &meth) < 0) {
317             PyErr_WriteUnraisable(yf);
318         }
319         if (meth) {
320             retval = _PyObject_CallNoArg(meth);
321             Py_DECREF(meth);
322             if (retval == NULL)
323                 return -1;
324         }
325     }
326     Py_XDECREF(retval);
327     return 0;
328 }
329 
330 PyObject *
_PyGen_yf(PyGenObject * gen)331 _PyGen_yf(PyGenObject *gen)
332 {
333     PyObject *yf = NULL;
334     PyFrameObject *f = gen->gi_frame;
335 
336     if (f && f->f_stacktop) {
337         PyObject *bytecode = f->f_code->co_code;
338         unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
339 
340         if (f->f_lasti < 0) {
341             /* Return immediately if the frame didn't start yet. YIELD_FROM
342                always come after LOAD_CONST: a code object should not start
343                with YIELD_FROM */
344             assert(code[0] != YIELD_FROM);
345             return NULL;
346         }
347 
348         if (code[f->f_lasti + sizeof(_Py_CODEUNIT)] != YIELD_FROM)
349             return NULL;
350         yf = f->f_stacktop[-1];
351         Py_INCREF(yf);
352     }
353 
354     return yf;
355 }
356 
357 static PyObject *
gen_close(PyGenObject * gen,PyObject * args)358 gen_close(PyGenObject *gen, PyObject *args)
359 {
360     PyObject *retval;
361     PyObject *yf = _PyGen_yf(gen);
362     int err = 0;
363 
364     if (yf) {
365         gen->gi_running = 1;
366         err = gen_close_iter(yf);
367         gen->gi_running = 0;
368         Py_DECREF(yf);
369     }
370     if (err == 0)
371         PyErr_SetNone(PyExc_GeneratorExit);
372     retval = gen_send_ex(gen, Py_None, 1, 1);
373     if (retval) {
374         const char *msg = "generator ignored GeneratorExit";
375         if (PyCoro_CheckExact(gen)) {
376             msg = "coroutine ignored GeneratorExit";
377         } else if (PyAsyncGen_CheckExact(gen)) {
378             msg = ASYNC_GEN_IGNORED_EXIT_MSG;
379         }
380         Py_DECREF(retval);
381         PyErr_SetString(PyExc_RuntimeError, msg);
382         return NULL;
383     }
384     if (PyErr_ExceptionMatches(PyExc_StopIteration)
385         || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
386         PyErr_Clear();          /* ignore these errors */
387         Py_RETURN_NONE;
388     }
389     return NULL;
390 }
391 
392 
393 PyDoc_STRVAR(throw_doc,
394 "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
395 return next yielded value or raise StopIteration.");
396 
397 static PyObject *
_gen_throw(PyGenObject * gen,int close_on_genexit,PyObject * typ,PyObject * val,PyObject * tb)398 _gen_throw(PyGenObject *gen, int close_on_genexit,
399            PyObject *typ, PyObject *val, PyObject *tb)
400 {
401     PyObject *yf = _PyGen_yf(gen);
402     _Py_IDENTIFIER(throw);
403 
404     if (yf) {
405         PyObject *ret;
406         int err;
407         if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
408             close_on_genexit
409         ) {
410             /* Asynchronous generators *should not* be closed right away.
411                We have to allow some awaits to work it through, hence the
412                `close_on_genexit` parameter here.
413             */
414             gen->gi_running = 1;
415             err = gen_close_iter(yf);
416             gen->gi_running = 0;
417             Py_DECREF(yf);
418             if (err < 0)
419                 return gen_send_ex(gen, Py_None, 1, 0);
420             goto throw_here;
421         }
422         if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
423             /* `yf` is a generator or a coroutine. */
424             gen->gi_running = 1;
425             /* Close the generator that we are currently iterating with
426                'yield from' or awaiting on with 'await'. */
427             ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
428                              typ, val, tb);
429             gen->gi_running = 0;
430         } else {
431             /* `yf` is an iterator or a coroutine-like object. */
432             PyObject *meth;
433             if (_PyObject_LookupAttrId(yf, &PyId_throw, &meth) < 0) {
434                 Py_DECREF(yf);
435                 return NULL;
436             }
437             if (meth == NULL) {
438                 Py_DECREF(yf);
439                 goto throw_here;
440             }
441             gen->gi_running = 1;
442             ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
443             gen->gi_running = 0;
444             Py_DECREF(meth);
445         }
446         Py_DECREF(yf);
447         if (!ret) {
448             PyObject *val;
449             /* Pop subiterator from stack */
450             ret = *(--gen->gi_frame->f_stacktop);
451             assert(ret == yf);
452             Py_DECREF(ret);
453             /* Termination repetition of YIELD_FROM */
454             assert(gen->gi_frame->f_lasti >= 0);
455             gen->gi_frame->f_lasti += sizeof(_Py_CODEUNIT);
456             if (_PyGen_FetchStopIterationValue(&val) == 0) {
457                 ret = gen_send_ex(gen, val, 0, 0);
458                 Py_DECREF(val);
459             } else {
460                 ret = gen_send_ex(gen, Py_None, 1, 0);
461             }
462         }
463         return ret;
464     }
465 
466 throw_here:
467     /* First, check the traceback argument, replacing None with
468        NULL. */
469     if (tb == Py_None) {
470         tb = NULL;
471     }
472     else if (tb != NULL && !PyTraceBack_Check(tb)) {
473         PyErr_SetString(PyExc_TypeError,
474             "throw() third argument must be a traceback object");
475         return NULL;
476     }
477 
478     Py_INCREF(typ);
479     Py_XINCREF(val);
480     Py_XINCREF(tb);
481 
482     if (PyExceptionClass_Check(typ))
483         PyErr_NormalizeException(&typ, &val, &tb);
484 
485     else if (PyExceptionInstance_Check(typ)) {
486         /* Raising an instance.  The value should be a dummy. */
487         if (val && val != Py_None) {
488             PyErr_SetString(PyExc_TypeError,
489               "instance exception may not have a separate value");
490             goto failed_throw;
491         }
492         else {
493             /* Normalize to raise <class>, <instance> */
494             Py_XDECREF(val);
495             val = typ;
496             typ = PyExceptionInstance_Class(typ);
497             Py_INCREF(typ);
498 
499             if (tb == NULL)
500                 /* Returns NULL if there's no traceback */
501                 tb = PyException_GetTraceback(val);
502         }
503     }
504     else {
505         /* Not something you can raise.  throw() fails. */
506         PyErr_Format(PyExc_TypeError,
507                      "exceptions must be classes or instances "
508                      "deriving from BaseException, not %s",
509                      Py_TYPE(typ)->tp_name);
510             goto failed_throw;
511     }
512 
513     PyErr_Restore(typ, val, tb);
514     return gen_send_ex(gen, Py_None, 1, 0);
515 
516 failed_throw:
517     /* Didn't use our arguments, so restore their original refcounts */
518     Py_DECREF(typ);
519     Py_XDECREF(val);
520     Py_XDECREF(tb);
521     return NULL;
522 }
523 
524 
525 static PyObject *
gen_throw(PyGenObject * gen,PyObject * args)526 gen_throw(PyGenObject *gen, PyObject *args)
527 {
528     PyObject *typ;
529     PyObject *tb = NULL;
530     PyObject *val = NULL;
531 
532     if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) {
533         return NULL;
534     }
535 
536     return _gen_throw(gen, 1, typ, val, tb);
537 }
538 
539 
540 static PyObject *
gen_iternext(PyGenObject * gen)541 gen_iternext(PyGenObject *gen)
542 {
543     return gen_send_ex(gen, NULL, 0, 0);
544 }
545 
546 /*
547  * Set StopIteration with specified value.  Value can be arbitrary object
548  * or NULL.
549  *
550  * Returns 0 if StopIteration is set and -1 if any other exception is set.
551  */
552 int
_PyGen_SetStopIterationValue(PyObject * value)553 _PyGen_SetStopIterationValue(PyObject *value)
554 {
555     PyObject *e;
556 
557     if (value == NULL ||
558         (!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
559     {
560         /* Delay exception instantiation if we can */
561         PyErr_SetObject(PyExc_StopIteration, value);
562         return 0;
563     }
564     /* Construct an exception instance manually with
565      * PyObject_CallFunctionObjArgs and pass it to PyErr_SetObject.
566      *
567      * We do this to handle a situation when "value" is a tuple, in which
568      * case PyErr_SetObject would set the value of StopIteration to
569      * the first element of the tuple.
570      *
571      * (See PyErr_SetObject/_PyErr_CreateException code for details.)
572      */
573     e = PyObject_CallFunctionObjArgs(PyExc_StopIteration, value, NULL);
574     if (e == NULL) {
575         return -1;
576     }
577     PyErr_SetObject(PyExc_StopIteration, e);
578     Py_DECREF(e);
579     return 0;
580 }
581 
582 /*
583  *   If StopIteration exception is set, fetches its 'value'
584  *   attribute if any, otherwise sets pvalue to None.
585  *
586  *   Returns 0 if no exception or StopIteration is set.
587  *   If any other exception is set, returns -1 and leaves
588  *   pvalue unchanged.
589  */
590 
591 int
_PyGen_FetchStopIterationValue(PyObject ** pvalue)592 _PyGen_FetchStopIterationValue(PyObject **pvalue)
593 {
594     PyObject *et, *ev, *tb;
595     PyObject *value = NULL;
596 
597     if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
598         PyErr_Fetch(&et, &ev, &tb);
599         if (ev) {
600             /* exception will usually be normalised already */
601             if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
602                 value = ((PyStopIterationObject *)ev)->value;
603                 Py_INCREF(value);
604                 Py_DECREF(ev);
605             } else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) {
606                 /* Avoid normalisation and take ev as value.
607                  *
608                  * Normalization is required if the value is a tuple, in
609                  * that case the value of StopIteration would be set to
610                  * the first element of the tuple.
611                  *
612                  * (See _PyErr_CreateException code for details.)
613                  */
614                 value = ev;
615             } else {
616                 /* normalisation required */
617                 PyErr_NormalizeException(&et, &ev, &tb);
618                 if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
619                     PyErr_Restore(et, ev, tb);
620                     return -1;
621                 }
622                 value = ((PyStopIterationObject *)ev)->value;
623                 Py_INCREF(value);
624                 Py_DECREF(ev);
625             }
626         }
627         Py_XDECREF(et);
628         Py_XDECREF(tb);
629     } else if (PyErr_Occurred()) {
630         return -1;
631     }
632     if (value == NULL) {
633         value = Py_None;
634         Py_INCREF(value);
635     }
636     *pvalue = value;
637     return 0;
638 }
639 
640 static PyObject *
gen_repr(PyGenObject * gen)641 gen_repr(PyGenObject *gen)
642 {
643     return PyUnicode_FromFormat("<generator object %S at %p>",
644                                 gen->gi_qualname, gen);
645 }
646 
647 static PyObject *
gen_get_name(PyGenObject * op,void * Py_UNUSED (ignored))648 gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored))
649 {
650     Py_INCREF(op->gi_name);
651     return op->gi_name;
652 }
653 
654 static int
gen_set_name(PyGenObject * op,PyObject * value,void * Py_UNUSED (ignored))655 gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
656 {
657     /* Not legal to del gen.gi_name or to set it to anything
658      * other than a string object. */
659     if (value == NULL || !PyUnicode_Check(value)) {
660         PyErr_SetString(PyExc_TypeError,
661                         "__name__ must be set to a string object");
662         return -1;
663     }
664     Py_INCREF(value);
665     Py_XSETREF(op->gi_name, value);
666     return 0;
667 }
668 
669 static PyObject *
gen_get_qualname(PyGenObject * op,void * Py_UNUSED (ignored))670 gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored))
671 {
672     Py_INCREF(op->gi_qualname);
673     return op->gi_qualname;
674 }
675 
676 static int
gen_set_qualname(PyGenObject * op,PyObject * value,void * Py_UNUSED (ignored))677 gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
678 {
679     /* Not legal to del gen.__qualname__ or to set it to anything
680      * other than a string object. */
681     if (value == NULL || !PyUnicode_Check(value)) {
682         PyErr_SetString(PyExc_TypeError,
683                         "__qualname__ must be set to a string object");
684         return -1;
685     }
686     Py_INCREF(value);
687     Py_XSETREF(op->gi_qualname, value);
688     return 0;
689 }
690 
691 static PyObject *
gen_getyieldfrom(PyGenObject * gen,void * Py_UNUSED (ignored))692 gen_getyieldfrom(PyGenObject *gen, void *Py_UNUSED(ignored))
693 {
694     PyObject *yf = _PyGen_yf(gen);
695     if (yf == NULL)
696         Py_RETURN_NONE;
697     return yf;
698 }
699 
700 static PyGetSetDef gen_getsetlist[] = {
701     {"__name__", (getter)gen_get_name, (setter)gen_set_name,
702      PyDoc_STR("name of the generator")},
703     {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
704      PyDoc_STR("qualified name of the generator")},
705     {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
706      PyDoc_STR("object being iterated by yield from, or None")},
707     {NULL} /* Sentinel */
708 };
709 
710 static PyMemberDef gen_memberlist[] = {
711     {"gi_frame",     T_OBJECT, offsetof(PyGenObject, gi_frame),    READONLY},
712     {"gi_running",   T_BOOL,   offsetof(PyGenObject, gi_running),  READONLY},
713     {"gi_code",      T_OBJECT, offsetof(PyGenObject, gi_code),     READONLY},
714     {NULL}      /* Sentinel */
715 };
716 
717 static PyMethodDef gen_methods[] = {
718     {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
719     {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
720     {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
721     {NULL, NULL}        /* Sentinel */
722 };
723 
724 PyTypeObject PyGen_Type = {
725     PyVarObject_HEAD_INIT(&PyType_Type, 0)
726     "generator",                                /* tp_name */
727     sizeof(PyGenObject),                        /* tp_basicsize */
728     0,                                          /* tp_itemsize */
729     /* methods */
730     (destructor)gen_dealloc,                    /* tp_dealloc */
731     0,                                          /* tp_vectorcall_offset */
732     0,                                          /* tp_getattr */
733     0,                                          /* tp_setattr */
734     0,                                          /* tp_as_async */
735     (reprfunc)gen_repr,                         /* tp_repr */
736     0,                                          /* tp_as_number */
737     0,                                          /* tp_as_sequence */
738     0,                                          /* tp_as_mapping */
739     0,                                          /* tp_hash */
740     0,                                          /* tp_call */
741     0,                                          /* tp_str */
742     PyObject_GenericGetAttr,                    /* tp_getattro */
743     0,                                          /* tp_setattro */
744     0,                                          /* tp_as_buffer */
745     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
746     0,                                          /* tp_doc */
747     (traverseproc)gen_traverse,                 /* tp_traverse */
748     0,                                          /* tp_clear */
749     0,                                          /* tp_richcompare */
750     offsetof(PyGenObject, gi_weakreflist),      /* tp_weaklistoffset */
751     PyObject_SelfIter,                          /* tp_iter */
752     (iternextfunc)gen_iternext,                 /* tp_iternext */
753     gen_methods,                                /* tp_methods */
754     gen_memberlist,                             /* tp_members */
755     gen_getsetlist,                             /* tp_getset */
756     0,                                          /* tp_base */
757     0,                                          /* tp_dict */
758 
759     0,                                          /* tp_descr_get */
760     0,                                          /* tp_descr_set */
761     0,                                          /* tp_dictoffset */
762     0,                                          /* tp_init */
763     0,                                          /* tp_alloc */
764     0,                                          /* tp_new */
765     0,                                          /* tp_free */
766     0,                                          /* tp_is_gc */
767     0,                                          /* tp_bases */
768     0,                                          /* tp_mro */
769     0,                                          /* tp_cache */
770     0,                                          /* tp_subclasses */
771     0,                                          /* tp_weaklist */
772     0,                                          /* tp_del */
773     0,                                          /* tp_version_tag */
774     _PyGen_Finalize,                            /* tp_finalize */
775 };
776 
777 static PyObject *
gen_new_with_qualname(PyTypeObject * type,PyFrameObject * f,PyObject * name,PyObject * qualname)778 gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
779                       PyObject *name, PyObject *qualname)
780 {
781     PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
782     if (gen == NULL) {
783         Py_DECREF(f);
784         return NULL;
785     }
786     gen->gi_frame = f;
787     f->f_gen = (PyObject *) gen;
788     Py_INCREF(f->f_code);
789     gen->gi_code = (PyObject *)(f->f_code);
790     gen->gi_running = 0;
791     gen->gi_weakreflist = NULL;
792     gen->gi_exc_state.exc_type = NULL;
793     gen->gi_exc_state.exc_value = NULL;
794     gen->gi_exc_state.exc_traceback = NULL;
795     gen->gi_exc_state.previous_item = NULL;
796     if (name != NULL)
797         gen->gi_name = name;
798     else
799         gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
800     Py_INCREF(gen->gi_name);
801     if (qualname != NULL)
802         gen->gi_qualname = qualname;
803     else
804         gen->gi_qualname = gen->gi_name;
805     Py_INCREF(gen->gi_qualname);
806     _PyObject_GC_TRACK(gen);
807     return (PyObject *)gen;
808 }
809 
810 PyObject *
PyGen_NewWithQualName(PyFrameObject * f,PyObject * name,PyObject * qualname)811 PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
812 {
813     return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
814 }
815 
816 PyObject *
PyGen_New(PyFrameObject * f)817 PyGen_New(PyFrameObject *f)
818 {
819     return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
820 }
821 
822 int
PyGen_NeedsFinalizing(PyGenObject * gen)823 PyGen_NeedsFinalizing(PyGenObject *gen)
824 {
825     PyFrameObject *f = gen->gi_frame;
826 
827     if (f == NULL || f->f_stacktop == NULL)
828         return 0; /* no frame or empty blockstack == no finalization */
829 
830     /* Any (exception-handling) block type requires cleanup. */
831     if (f->f_iblock > 0)
832         return 1;
833 
834     /* No blocks, it's safe to skip finalization. */
835     return 0;
836 }
837 
838 /* Coroutine Object */
839 
840 typedef struct {
841     PyObject_HEAD
842     PyCoroObject *cw_coroutine;
843 } PyCoroWrapper;
844 
845 static int
gen_is_coroutine(PyObject * o)846 gen_is_coroutine(PyObject *o)
847 {
848     if (PyGen_CheckExact(o)) {
849         PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
850         if (code->co_flags & CO_ITERABLE_COROUTINE) {
851             return 1;
852         }
853     }
854     return 0;
855 }
856 
857 /*
858  *   This helper function returns an awaitable for `o`:
859  *     - `o` if `o` is a coroutine-object;
860  *     - `type(o)->tp_as_async->am_await(o)`
861  *
862  *   Raises a TypeError if it's not possible to return
863  *   an awaitable and returns NULL.
864  */
865 PyObject *
_PyCoro_GetAwaitableIter(PyObject * o)866 _PyCoro_GetAwaitableIter(PyObject *o)
867 {
868     unaryfunc getter = NULL;
869     PyTypeObject *ot;
870 
871     if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
872         /* 'o' is a coroutine. */
873         Py_INCREF(o);
874         return o;
875     }
876 
877     ot = Py_TYPE(o);
878     if (ot->tp_as_async != NULL) {
879         getter = ot->tp_as_async->am_await;
880     }
881     if (getter != NULL) {
882         PyObject *res = (*getter)(o);
883         if (res != NULL) {
884             if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
885                 /* __await__ must return an *iterator*, not
886                    a coroutine or another awaitable (see PEP 492) */
887                 PyErr_SetString(PyExc_TypeError,
888                                 "__await__() returned a coroutine");
889                 Py_CLEAR(res);
890             } else if (!PyIter_Check(res)) {
891                 PyErr_Format(PyExc_TypeError,
892                              "__await__() returned non-iterator "
893                              "of type '%.100s'",
894                              Py_TYPE(res)->tp_name);
895                 Py_CLEAR(res);
896             }
897         }
898         return res;
899     }
900 
901     PyErr_Format(PyExc_TypeError,
902                  "object %.100s can't be used in 'await' expression",
903                  ot->tp_name);
904     return NULL;
905 }
906 
907 static PyObject *
coro_repr(PyCoroObject * coro)908 coro_repr(PyCoroObject *coro)
909 {
910     return PyUnicode_FromFormat("<coroutine object %S at %p>",
911                                 coro->cr_qualname, coro);
912 }
913 
914 static PyObject *
coro_await(PyCoroObject * coro)915 coro_await(PyCoroObject *coro)
916 {
917     PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
918     if (cw == NULL) {
919         return NULL;
920     }
921     Py_INCREF(coro);
922     cw->cw_coroutine = coro;
923     _PyObject_GC_TRACK(cw);
924     return (PyObject *)cw;
925 }
926 
927 static PyObject *
coro_get_cr_await(PyCoroObject * coro,void * Py_UNUSED (ignored))928 coro_get_cr_await(PyCoroObject *coro, void *Py_UNUSED(ignored))
929 {
930     PyObject *yf = _PyGen_yf((PyGenObject *) coro);
931     if (yf == NULL)
932         Py_RETURN_NONE;
933     return yf;
934 }
935 
936 static PyGetSetDef coro_getsetlist[] = {
937     {"__name__", (getter)gen_get_name, (setter)gen_set_name,
938      PyDoc_STR("name of the coroutine")},
939     {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
940      PyDoc_STR("qualified name of the coroutine")},
941     {"cr_await", (getter)coro_get_cr_await, NULL,
942      PyDoc_STR("object being awaited on, or None")},
943     {NULL} /* Sentinel */
944 };
945 
946 static PyMemberDef coro_memberlist[] = {
947     {"cr_frame",     T_OBJECT, offsetof(PyCoroObject, cr_frame),    READONLY},
948     {"cr_running",   T_BOOL,   offsetof(PyCoroObject, cr_running),  READONLY},
949     {"cr_code",      T_OBJECT, offsetof(PyCoroObject, cr_code),     READONLY},
950     {"cr_origin",    T_OBJECT, offsetof(PyCoroObject, cr_origin),   READONLY},
951     {NULL}      /* Sentinel */
952 };
953 
954 PyDoc_STRVAR(coro_send_doc,
955 "send(arg) -> send 'arg' into coroutine,\n\
956 return next iterated value or raise StopIteration.");
957 
958 PyDoc_STRVAR(coro_throw_doc,
959 "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
960 return next iterated value or raise StopIteration.");
961 
962 PyDoc_STRVAR(coro_close_doc,
963 "close() -> raise GeneratorExit inside coroutine.");
964 
965 static PyMethodDef coro_methods[] = {
966     {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc},
967     {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
968     {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
969     {NULL, NULL}        /* Sentinel */
970 };
971 
972 static PyAsyncMethods coro_as_async = {
973     (unaryfunc)coro_await,                      /* am_await */
974     0,                                          /* am_aiter */
975     0                                           /* am_anext */
976 };
977 
978 PyTypeObject PyCoro_Type = {
979     PyVarObject_HEAD_INIT(&PyType_Type, 0)
980     "coroutine",                                /* tp_name */
981     sizeof(PyCoroObject),                       /* tp_basicsize */
982     0,                                          /* tp_itemsize */
983     /* methods */
984     (destructor)gen_dealloc,                    /* tp_dealloc */
985     0,                                          /* tp_vectorcall_offset */
986     0,                                          /* tp_getattr */
987     0,                                          /* tp_setattr */
988     &coro_as_async,                             /* tp_as_async */
989     (reprfunc)coro_repr,                        /* tp_repr */
990     0,                                          /* tp_as_number */
991     0,                                          /* tp_as_sequence */
992     0,                                          /* tp_as_mapping */
993     0,                                          /* tp_hash */
994     0,                                          /* tp_call */
995     0,                                          /* tp_str */
996     PyObject_GenericGetAttr,                    /* tp_getattro */
997     0,                                          /* tp_setattro */
998     0,                                          /* tp_as_buffer */
999     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1000     0,                                          /* tp_doc */
1001     (traverseproc)gen_traverse,                 /* tp_traverse */
1002     0,                                          /* tp_clear */
1003     0,                                          /* tp_richcompare */
1004     offsetof(PyCoroObject, cr_weakreflist),     /* tp_weaklistoffset */
1005     0,                                          /* tp_iter */
1006     0,                                          /* tp_iternext */
1007     coro_methods,                               /* tp_methods */
1008     coro_memberlist,                            /* tp_members */
1009     coro_getsetlist,                            /* tp_getset */
1010     0,                                          /* tp_base */
1011     0,                                          /* tp_dict */
1012     0,                                          /* tp_descr_get */
1013     0,                                          /* tp_descr_set */
1014     0,                                          /* tp_dictoffset */
1015     0,                                          /* tp_init */
1016     0,                                          /* tp_alloc */
1017     0,                                          /* tp_new */
1018     0,                                          /* tp_free */
1019     0,                                          /* tp_is_gc */
1020     0,                                          /* tp_bases */
1021     0,                                          /* tp_mro */
1022     0,                                          /* tp_cache */
1023     0,                                          /* tp_subclasses */
1024     0,                                          /* tp_weaklist */
1025     0,                                          /* tp_del */
1026     0,                                          /* tp_version_tag */
1027     _PyGen_Finalize,                            /* tp_finalize */
1028 };
1029 
1030 static void
coro_wrapper_dealloc(PyCoroWrapper * cw)1031 coro_wrapper_dealloc(PyCoroWrapper *cw)
1032 {
1033     _PyObject_GC_UNTRACK((PyObject *)cw);
1034     Py_CLEAR(cw->cw_coroutine);
1035     PyObject_GC_Del(cw);
1036 }
1037 
1038 static PyObject *
coro_wrapper_iternext(PyCoroWrapper * cw)1039 coro_wrapper_iternext(PyCoroWrapper *cw)
1040 {
1041     return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0);
1042 }
1043 
1044 static PyObject *
coro_wrapper_send(PyCoroWrapper * cw,PyObject * arg)1045 coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
1046 {
1047     return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0);
1048 }
1049 
1050 static PyObject *
coro_wrapper_throw(PyCoroWrapper * cw,PyObject * args)1051 coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
1052 {
1053     return gen_throw((PyGenObject *)cw->cw_coroutine, args);
1054 }
1055 
1056 static PyObject *
coro_wrapper_close(PyCoroWrapper * cw,PyObject * args)1057 coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
1058 {
1059     return gen_close((PyGenObject *)cw->cw_coroutine, args);
1060 }
1061 
1062 static int
coro_wrapper_traverse(PyCoroWrapper * cw,visitproc visit,void * arg)1063 coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
1064 {
1065     Py_VISIT((PyObject *)cw->cw_coroutine);
1066     return 0;
1067 }
1068 
1069 static PyMethodDef coro_wrapper_methods[] = {
1070     {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
1071     {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
1072     {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
1073     {NULL, NULL}        /* Sentinel */
1074 };
1075 
1076 PyTypeObject _PyCoroWrapper_Type = {
1077     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1078     "coroutine_wrapper",
1079     sizeof(PyCoroWrapper),                      /* tp_basicsize */
1080     0,                                          /* tp_itemsize */
1081     (destructor)coro_wrapper_dealloc,           /* destructor tp_dealloc */
1082     0,                                          /* tp_vectorcall_offset */
1083     0,                                          /* tp_getattr */
1084     0,                                          /* tp_setattr */
1085     0,                                          /* tp_as_async */
1086     0,                                          /* tp_repr */
1087     0,                                          /* tp_as_number */
1088     0,                                          /* tp_as_sequence */
1089     0,                                          /* tp_as_mapping */
1090     0,                                          /* tp_hash */
1091     0,                                          /* tp_call */
1092     0,                                          /* tp_str */
1093     PyObject_GenericGetAttr,                    /* tp_getattro */
1094     0,                                          /* tp_setattro */
1095     0,                                          /* tp_as_buffer */
1096     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1097     "A wrapper object implementing __await__ for coroutines.",
1098     (traverseproc)coro_wrapper_traverse,        /* tp_traverse */
1099     0,                                          /* tp_clear */
1100     0,                                          /* tp_richcompare */
1101     0,                                          /* tp_weaklistoffset */
1102     PyObject_SelfIter,                          /* tp_iter */
1103     (iternextfunc)coro_wrapper_iternext,        /* tp_iternext */
1104     coro_wrapper_methods,                       /* tp_methods */
1105     0,                                          /* tp_members */
1106     0,                                          /* tp_getset */
1107     0,                                          /* tp_base */
1108     0,                                          /* tp_dict */
1109     0,                                          /* tp_descr_get */
1110     0,                                          /* tp_descr_set */
1111     0,                                          /* tp_dictoffset */
1112     0,                                          /* tp_init */
1113     0,                                          /* tp_alloc */
1114     0,                                          /* tp_new */
1115     0,                                          /* tp_free */
1116 };
1117 
1118 static PyObject *
compute_cr_origin(int origin_depth)1119 compute_cr_origin(int origin_depth)
1120 {
1121     PyFrameObject *frame = PyEval_GetFrame();
1122     /* First count how many frames we have */
1123     int frame_count = 0;
1124     for (; frame && frame_count < origin_depth; ++frame_count) {
1125         frame = frame->f_back;
1126     }
1127 
1128     /* Now collect them */
1129     PyObject *cr_origin = PyTuple_New(frame_count);
1130     if (cr_origin == NULL) {
1131         return NULL;
1132     }
1133     frame = PyEval_GetFrame();
1134     for (int i = 0; i < frame_count; ++i) {
1135         PyObject *frameinfo = Py_BuildValue(
1136             "OiO",
1137             frame->f_code->co_filename,
1138             PyFrame_GetLineNumber(frame),
1139             frame->f_code->co_name);
1140         if (!frameinfo) {
1141             Py_DECREF(cr_origin);
1142             return NULL;
1143         }
1144         PyTuple_SET_ITEM(cr_origin, i, frameinfo);
1145         frame = frame->f_back;
1146     }
1147 
1148     return cr_origin;
1149 }
1150 
1151 PyObject *
PyCoro_New(PyFrameObject * f,PyObject * name,PyObject * qualname)1152 PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1153 {
1154     PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
1155     if (!coro) {
1156         return NULL;
1157     }
1158 
1159     PyThreadState *tstate = _PyThreadState_GET();
1160     int origin_depth = tstate->coroutine_origin_tracking_depth;
1161 
1162     if (origin_depth == 0) {
1163         ((PyCoroObject *)coro)->cr_origin = NULL;
1164     } else {
1165         PyObject *cr_origin = compute_cr_origin(origin_depth);
1166         ((PyCoroObject *)coro)->cr_origin = cr_origin;
1167         if (!cr_origin) {
1168             Py_DECREF(coro);
1169             return NULL;
1170         }
1171     }
1172 
1173     return coro;
1174 }
1175 
1176 
1177 /* ========= Asynchronous Generators ========= */
1178 
1179 
1180 typedef enum {
1181     AWAITABLE_STATE_INIT,   /* new awaitable, has not yet been iterated */
1182     AWAITABLE_STATE_ITER,   /* being iterated */
1183     AWAITABLE_STATE_CLOSED, /* closed */
1184 } AwaitableState;
1185 
1186 
1187 typedef struct {
1188     PyObject_HEAD
1189     PyAsyncGenObject *ags_gen;
1190 
1191     /* Can be NULL, when in the __anext__() mode
1192        (equivalent of "asend(None)") */
1193     PyObject *ags_sendval;
1194 
1195     AwaitableState ags_state;
1196 } PyAsyncGenASend;
1197 
1198 
1199 typedef struct {
1200     PyObject_HEAD
1201     PyAsyncGenObject *agt_gen;
1202 
1203     /* Can be NULL, when in the "aclose()" mode
1204        (equivalent of "athrow(GeneratorExit)") */
1205     PyObject *agt_args;
1206 
1207     AwaitableState agt_state;
1208 } PyAsyncGenAThrow;
1209 
1210 
1211 typedef struct {
1212     PyObject_HEAD
1213     PyObject *agw_val;
1214 } _PyAsyncGenWrappedValue;
1215 
1216 
1217 #ifndef _PyAsyncGen_MAXFREELIST
1218 #define _PyAsyncGen_MAXFREELIST 80
1219 #endif
1220 
1221 /* Freelists boost performance 6-10%; they also reduce memory
1222    fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend
1223    are short-living objects that are instantiated for every
1224    __anext__ call.
1225 */
1226 
1227 static _PyAsyncGenWrappedValue *ag_value_freelist[_PyAsyncGen_MAXFREELIST];
1228 static int ag_value_freelist_free = 0;
1229 
1230 static PyAsyncGenASend *ag_asend_freelist[_PyAsyncGen_MAXFREELIST];
1231 static int ag_asend_freelist_free = 0;
1232 
1233 #define _PyAsyncGenWrappedValue_CheckExact(o) \
1234                     (Py_TYPE(o) == &_PyAsyncGenWrappedValue_Type)
1235 
1236 #define PyAsyncGenASend_CheckExact(o) \
1237                     (Py_TYPE(o) == &_PyAsyncGenASend_Type)
1238 
1239 
1240 static int
async_gen_traverse(PyAsyncGenObject * gen,visitproc visit,void * arg)1241 async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg)
1242 {
1243     Py_VISIT(gen->ag_finalizer);
1244     return gen_traverse((PyGenObject*)gen, visit, arg);
1245 }
1246 
1247 
1248 static PyObject *
async_gen_repr(PyAsyncGenObject * o)1249 async_gen_repr(PyAsyncGenObject *o)
1250 {
1251     return PyUnicode_FromFormat("<async_generator object %S at %p>",
1252                                 o->ag_qualname, o);
1253 }
1254 
1255 
1256 static int
async_gen_init_hooks(PyAsyncGenObject * o)1257 async_gen_init_hooks(PyAsyncGenObject *o)
1258 {
1259     PyThreadState *tstate;
1260     PyObject *finalizer;
1261     PyObject *firstiter;
1262 
1263     if (o->ag_hooks_inited) {
1264         return 0;
1265     }
1266 
1267     o->ag_hooks_inited = 1;
1268 
1269     tstate = _PyThreadState_GET();
1270 
1271     finalizer = tstate->async_gen_finalizer;
1272     if (finalizer) {
1273         Py_INCREF(finalizer);
1274         o->ag_finalizer = finalizer;
1275     }
1276 
1277     firstiter = tstate->async_gen_firstiter;
1278     if (firstiter) {
1279         PyObject *res;
1280 
1281         Py_INCREF(firstiter);
1282         res = PyObject_CallFunctionObjArgs(firstiter, o, NULL);
1283         Py_DECREF(firstiter);
1284         if (res == NULL) {
1285             return 1;
1286         }
1287         Py_DECREF(res);
1288     }
1289 
1290     return 0;
1291 }
1292 
1293 
1294 static PyObject *
async_gen_anext(PyAsyncGenObject * o)1295 async_gen_anext(PyAsyncGenObject *o)
1296 {
1297     if (async_gen_init_hooks(o)) {
1298         return NULL;
1299     }
1300     return async_gen_asend_new(o, NULL);
1301 }
1302 
1303 
1304 static PyObject *
async_gen_asend(PyAsyncGenObject * o,PyObject * arg)1305 async_gen_asend(PyAsyncGenObject *o, PyObject *arg)
1306 {
1307     if (async_gen_init_hooks(o)) {
1308         return NULL;
1309     }
1310     return async_gen_asend_new(o, arg);
1311 }
1312 
1313 
1314 static PyObject *
async_gen_aclose(PyAsyncGenObject * o,PyObject * arg)1315 async_gen_aclose(PyAsyncGenObject *o, PyObject *arg)
1316 {
1317     if (async_gen_init_hooks(o)) {
1318         return NULL;
1319     }
1320     return async_gen_athrow_new(o, NULL);
1321 }
1322 
1323 static PyObject *
async_gen_athrow(PyAsyncGenObject * o,PyObject * args)1324 async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
1325 {
1326     if (async_gen_init_hooks(o)) {
1327         return NULL;
1328     }
1329     return async_gen_athrow_new(o, args);
1330 }
1331 
1332 
1333 static PyGetSetDef async_gen_getsetlist[] = {
1334     {"__name__", (getter)gen_get_name, (setter)gen_set_name,
1335      PyDoc_STR("name of the async generator")},
1336     {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
1337      PyDoc_STR("qualified name of the async generator")},
1338     {"ag_await", (getter)coro_get_cr_await, NULL,
1339      PyDoc_STR("object being awaited on, or None")},
1340     {NULL} /* Sentinel */
1341 };
1342 
1343 static PyMemberDef async_gen_memberlist[] = {
1344     {"ag_frame",   T_OBJECT, offsetof(PyAsyncGenObject, ag_frame),   READONLY},
1345     {"ag_running", T_BOOL,   offsetof(PyAsyncGenObject, ag_running_async),
1346         READONLY},
1347     {"ag_code",    T_OBJECT, offsetof(PyAsyncGenObject, ag_code),    READONLY},
1348     {NULL}      /* Sentinel */
1349 };
1350 
1351 PyDoc_STRVAR(async_aclose_doc,
1352 "aclose() -> raise GeneratorExit inside generator.");
1353 
1354 PyDoc_STRVAR(async_asend_doc,
1355 "asend(v) -> send 'v' in generator.");
1356 
1357 PyDoc_STRVAR(async_athrow_doc,
1358 "athrow(typ[,val[,tb]]) -> raise exception in generator.");
1359 
1360 static PyMethodDef async_gen_methods[] = {
1361     {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
1362     {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
1363     {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
1364     {NULL, NULL}        /* Sentinel */
1365 };
1366 
1367 
1368 static PyAsyncMethods async_gen_as_async = {
1369     0,                                          /* am_await */
1370     PyObject_SelfIter,                          /* am_aiter */
1371     (unaryfunc)async_gen_anext                  /* am_anext */
1372 };
1373 
1374 
1375 PyTypeObject PyAsyncGen_Type = {
1376     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1377     "async_generator",                          /* tp_name */
1378     sizeof(PyAsyncGenObject),                   /* tp_basicsize */
1379     0,                                          /* tp_itemsize */
1380     /* methods */
1381     (destructor)gen_dealloc,                    /* tp_dealloc */
1382     0,                                          /* tp_vectorcall_offset */
1383     0,                                          /* tp_getattr */
1384     0,                                          /* tp_setattr */
1385     &async_gen_as_async,                        /* tp_as_async */
1386     (reprfunc)async_gen_repr,                   /* tp_repr */
1387     0,                                          /* tp_as_number */
1388     0,                                          /* tp_as_sequence */
1389     0,                                          /* tp_as_mapping */
1390     0,                                          /* tp_hash */
1391     0,                                          /* tp_call */
1392     0,                                          /* tp_str */
1393     PyObject_GenericGetAttr,                    /* tp_getattro */
1394     0,                                          /* tp_setattro */
1395     0,                                          /* tp_as_buffer */
1396     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1397     0,                                          /* tp_doc */
1398     (traverseproc)async_gen_traverse,           /* tp_traverse */
1399     0,                                          /* tp_clear */
1400     0,                                          /* tp_richcompare */
1401     offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */
1402     0,                                          /* tp_iter */
1403     0,                                          /* tp_iternext */
1404     async_gen_methods,                          /* tp_methods */
1405     async_gen_memberlist,                       /* tp_members */
1406     async_gen_getsetlist,                       /* tp_getset */
1407     0,                                          /* tp_base */
1408     0,                                          /* tp_dict */
1409     0,                                          /* tp_descr_get */
1410     0,                                          /* tp_descr_set */
1411     0,                                          /* tp_dictoffset */
1412     0,                                          /* tp_init */
1413     0,                                          /* tp_alloc */
1414     0,                                          /* tp_new */
1415     0,                                          /* tp_free */
1416     0,                                          /* tp_is_gc */
1417     0,                                          /* tp_bases */
1418     0,                                          /* tp_mro */
1419     0,                                          /* tp_cache */
1420     0,                                          /* tp_subclasses */
1421     0,                                          /* tp_weaklist */
1422     0,                                          /* tp_del */
1423     0,                                          /* tp_version_tag */
1424     _PyGen_Finalize,                            /* tp_finalize */
1425 };
1426 
1427 
1428 PyObject *
PyAsyncGen_New(PyFrameObject * f,PyObject * name,PyObject * qualname)1429 PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1430 {
1431     PyAsyncGenObject *o;
1432     o = (PyAsyncGenObject *)gen_new_with_qualname(
1433         &PyAsyncGen_Type, f, name, qualname);
1434     if (o == NULL) {
1435         return NULL;
1436     }
1437     o->ag_finalizer = NULL;
1438     o->ag_closed = 0;
1439     o->ag_hooks_inited = 0;
1440     o->ag_running_async = 0;
1441     return (PyObject*)o;
1442 }
1443 
1444 
1445 int
PyAsyncGen_ClearFreeLists(void)1446 PyAsyncGen_ClearFreeLists(void)
1447 {
1448     int ret = ag_value_freelist_free + ag_asend_freelist_free;
1449 
1450     while (ag_value_freelist_free) {
1451         _PyAsyncGenWrappedValue *o;
1452         o = ag_value_freelist[--ag_value_freelist_free];
1453         assert(_PyAsyncGenWrappedValue_CheckExact(o));
1454         PyObject_GC_Del(o);
1455     }
1456 
1457     while (ag_asend_freelist_free) {
1458         PyAsyncGenASend *o;
1459         o = ag_asend_freelist[--ag_asend_freelist_free];
1460         assert(Py_TYPE(o) == &_PyAsyncGenASend_Type);
1461         PyObject_GC_Del(o);
1462     }
1463 
1464     return ret;
1465 }
1466 
1467 void
PyAsyncGen_Fini(void)1468 PyAsyncGen_Fini(void)
1469 {
1470     PyAsyncGen_ClearFreeLists();
1471 }
1472 
1473 
1474 static PyObject *
async_gen_unwrap_value(PyAsyncGenObject * gen,PyObject * result)1475 async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
1476 {
1477     if (result == NULL) {
1478         if (!PyErr_Occurred()) {
1479             PyErr_SetNone(PyExc_StopAsyncIteration);
1480         }
1481 
1482         if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)
1483             || PyErr_ExceptionMatches(PyExc_GeneratorExit)
1484         ) {
1485             gen->ag_closed = 1;
1486         }
1487 
1488         gen->ag_running_async = 0;
1489         return NULL;
1490     }
1491 
1492     if (_PyAsyncGenWrappedValue_CheckExact(result)) {
1493         /* async yield */
1494         _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val);
1495         Py_DECREF(result);
1496         gen->ag_running_async = 0;
1497         return NULL;
1498     }
1499 
1500     return result;
1501 }
1502 
1503 
1504 /* ---------- Async Generator ASend Awaitable ------------ */
1505 
1506 
1507 static void
async_gen_asend_dealloc(PyAsyncGenASend * o)1508 async_gen_asend_dealloc(PyAsyncGenASend *o)
1509 {
1510     _PyObject_GC_UNTRACK((PyObject *)o);
1511     Py_CLEAR(o->ags_gen);
1512     Py_CLEAR(o->ags_sendval);
1513     if (ag_asend_freelist_free < _PyAsyncGen_MAXFREELIST) {
1514         assert(PyAsyncGenASend_CheckExact(o));
1515         ag_asend_freelist[ag_asend_freelist_free++] = o;
1516     } else {
1517         PyObject_GC_Del(o);
1518     }
1519 }
1520 
1521 static int
async_gen_asend_traverse(PyAsyncGenASend * o,visitproc visit,void * arg)1522 async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg)
1523 {
1524     Py_VISIT(o->ags_gen);
1525     Py_VISIT(o->ags_sendval);
1526     return 0;
1527 }
1528 
1529 
1530 static PyObject *
async_gen_asend_send(PyAsyncGenASend * o,PyObject * arg)1531 async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg)
1532 {
1533     PyObject *result;
1534 
1535     if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1536         PyErr_SetNone(PyExc_StopIteration);
1537         return NULL;
1538     }
1539 
1540     if (o->ags_state == AWAITABLE_STATE_INIT) {
1541         if (o->ags_gen->ag_running_async) {
1542             PyErr_SetString(
1543                 PyExc_RuntimeError,
1544                 "anext(): asynchronous generator is already running");
1545             return NULL;
1546         }
1547 
1548         if (arg == NULL || arg == Py_None) {
1549             arg = o->ags_sendval;
1550         }
1551         o->ags_state = AWAITABLE_STATE_ITER;
1552     }
1553 
1554     o->ags_gen->ag_running_async = 1;
1555     result = gen_send_ex((PyGenObject*)o->ags_gen, arg, 0, 0);
1556     result = async_gen_unwrap_value(o->ags_gen, result);
1557 
1558     if (result == NULL) {
1559         o->ags_state = AWAITABLE_STATE_CLOSED;
1560     }
1561 
1562     return result;
1563 }
1564 
1565 
1566 static PyObject *
async_gen_asend_iternext(PyAsyncGenASend * o)1567 async_gen_asend_iternext(PyAsyncGenASend *o)
1568 {
1569     return async_gen_asend_send(o, NULL);
1570 }
1571 
1572 
1573 static PyObject *
async_gen_asend_throw(PyAsyncGenASend * o,PyObject * args)1574 async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
1575 {
1576     PyObject *result;
1577 
1578     if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1579         PyErr_SetNone(PyExc_StopIteration);
1580         return NULL;
1581     }
1582 
1583     result = gen_throw((PyGenObject*)o->ags_gen, args);
1584     result = async_gen_unwrap_value(o->ags_gen, result);
1585 
1586     if (result == NULL) {
1587         o->ags_state = AWAITABLE_STATE_CLOSED;
1588     }
1589 
1590     return result;
1591 }
1592 
1593 
1594 static PyObject *
async_gen_asend_close(PyAsyncGenASend * o,PyObject * args)1595 async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
1596 {
1597     o->ags_state = AWAITABLE_STATE_CLOSED;
1598     Py_RETURN_NONE;
1599 }
1600 
1601 
1602 static PyMethodDef async_gen_asend_methods[] = {
1603     {"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
1604     {"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc},
1605     {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
1606     {NULL, NULL}        /* Sentinel */
1607 };
1608 
1609 
1610 static PyAsyncMethods async_gen_asend_as_async = {
1611     PyObject_SelfIter,                          /* am_await */
1612     0,                                          /* am_aiter */
1613     0                                           /* am_anext */
1614 };
1615 
1616 
1617 PyTypeObject _PyAsyncGenASend_Type = {
1618     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1619     "async_generator_asend",                    /* tp_name */
1620     sizeof(PyAsyncGenASend),                    /* tp_basicsize */
1621     0,                                          /* tp_itemsize */
1622     /* methods */
1623     (destructor)async_gen_asend_dealloc,        /* tp_dealloc */
1624     0,                                          /* tp_vectorcall_offset */
1625     0,                                          /* tp_getattr */
1626     0,                                          /* tp_setattr */
1627     &async_gen_asend_as_async,                  /* tp_as_async */
1628     0,                                          /* tp_repr */
1629     0,                                          /* tp_as_number */
1630     0,                                          /* tp_as_sequence */
1631     0,                                          /* tp_as_mapping */
1632     0,                                          /* tp_hash */
1633     0,                                          /* tp_call */
1634     0,                                          /* tp_str */
1635     PyObject_GenericGetAttr,                    /* tp_getattro */
1636     0,                                          /* tp_setattro */
1637     0,                                          /* tp_as_buffer */
1638     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1639     0,                                          /* tp_doc */
1640     (traverseproc)async_gen_asend_traverse,     /* tp_traverse */
1641     0,                                          /* tp_clear */
1642     0,                                          /* tp_richcompare */
1643     0,                                          /* tp_weaklistoffset */
1644     PyObject_SelfIter,                          /* tp_iter */
1645     (iternextfunc)async_gen_asend_iternext,     /* tp_iternext */
1646     async_gen_asend_methods,                    /* tp_methods */
1647     0,                                          /* tp_members */
1648     0,                                          /* tp_getset */
1649     0,                                          /* tp_base */
1650     0,                                          /* tp_dict */
1651     0,                                          /* tp_descr_get */
1652     0,                                          /* tp_descr_set */
1653     0,                                          /* tp_dictoffset */
1654     0,                                          /* tp_init */
1655     0,                                          /* tp_alloc */
1656     0,                                          /* tp_new */
1657 };
1658 
1659 
1660 static PyObject *
async_gen_asend_new(PyAsyncGenObject * gen,PyObject * sendval)1661 async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
1662 {
1663     PyAsyncGenASend *o;
1664     if (ag_asend_freelist_free) {
1665         ag_asend_freelist_free--;
1666         o = ag_asend_freelist[ag_asend_freelist_free];
1667         _Py_NewReference((PyObject *)o);
1668     } else {
1669         o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type);
1670         if (o == NULL) {
1671             return NULL;
1672         }
1673     }
1674 
1675     Py_INCREF(gen);
1676     o->ags_gen = gen;
1677 
1678     Py_XINCREF(sendval);
1679     o->ags_sendval = sendval;
1680 
1681     o->ags_state = AWAITABLE_STATE_INIT;
1682 
1683     _PyObject_GC_TRACK((PyObject*)o);
1684     return (PyObject*)o;
1685 }
1686 
1687 
1688 /* ---------- Async Generator Value Wrapper ------------ */
1689 
1690 
1691 static void
async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue * o)1692 async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
1693 {
1694     _PyObject_GC_UNTRACK((PyObject *)o);
1695     Py_CLEAR(o->agw_val);
1696     if (ag_value_freelist_free < _PyAsyncGen_MAXFREELIST) {
1697         assert(_PyAsyncGenWrappedValue_CheckExact(o));
1698         ag_value_freelist[ag_value_freelist_free++] = o;
1699     } else {
1700         PyObject_GC_Del(o);
1701     }
1702 }
1703 
1704 
1705 static int
async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue * o,visitproc visit,void * arg)1706 async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o,
1707                                visitproc visit, void *arg)
1708 {
1709     Py_VISIT(o->agw_val);
1710     return 0;
1711 }
1712 
1713 
1714 PyTypeObject _PyAsyncGenWrappedValue_Type = {
1715     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1716     "async_generator_wrapped_value",            /* tp_name */
1717     sizeof(_PyAsyncGenWrappedValue),            /* tp_basicsize */
1718     0,                                          /* tp_itemsize */
1719     /* methods */
1720     (destructor)async_gen_wrapped_val_dealloc,  /* tp_dealloc */
1721     0,                                          /* tp_vectorcall_offset */
1722     0,                                          /* tp_getattr */
1723     0,                                          /* tp_setattr */
1724     0,                                          /* tp_as_async */
1725     0,                                          /* tp_repr */
1726     0,                                          /* tp_as_number */
1727     0,                                          /* tp_as_sequence */
1728     0,                                          /* tp_as_mapping */
1729     0,                                          /* tp_hash */
1730     0,                                          /* tp_call */
1731     0,                                          /* tp_str */
1732     PyObject_GenericGetAttr,                    /* tp_getattro */
1733     0,                                          /* tp_setattro */
1734     0,                                          /* tp_as_buffer */
1735     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1736     0,                                          /* tp_doc */
1737     (traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */
1738     0,                                          /* tp_clear */
1739     0,                                          /* tp_richcompare */
1740     0,                                          /* tp_weaklistoffset */
1741     0,                                          /* tp_iter */
1742     0,                                          /* tp_iternext */
1743     0,                                          /* tp_methods */
1744     0,                                          /* tp_members */
1745     0,                                          /* tp_getset */
1746     0,                                          /* tp_base */
1747     0,                                          /* tp_dict */
1748     0,                                          /* tp_descr_get */
1749     0,                                          /* tp_descr_set */
1750     0,                                          /* tp_dictoffset */
1751     0,                                          /* tp_init */
1752     0,                                          /* tp_alloc */
1753     0,                                          /* tp_new */
1754 };
1755 
1756 
1757 PyObject *
_PyAsyncGenValueWrapperNew(PyObject * val)1758 _PyAsyncGenValueWrapperNew(PyObject *val)
1759 {
1760     _PyAsyncGenWrappedValue *o;
1761     assert(val);
1762 
1763     if (ag_value_freelist_free) {
1764         ag_value_freelist_free--;
1765         o = ag_value_freelist[ag_value_freelist_free];
1766         assert(_PyAsyncGenWrappedValue_CheckExact(o));
1767         _Py_NewReference((PyObject*)o);
1768     } else {
1769         o = PyObject_GC_New(_PyAsyncGenWrappedValue,
1770                             &_PyAsyncGenWrappedValue_Type);
1771         if (o == NULL) {
1772             return NULL;
1773         }
1774     }
1775     o->agw_val = val;
1776     Py_INCREF(val);
1777     _PyObject_GC_TRACK((PyObject*)o);
1778     return (PyObject*)o;
1779 }
1780 
1781 
1782 /* ---------- Async Generator AThrow awaitable ------------ */
1783 
1784 
1785 static void
async_gen_athrow_dealloc(PyAsyncGenAThrow * o)1786 async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
1787 {
1788     _PyObject_GC_UNTRACK((PyObject *)o);
1789     Py_CLEAR(o->agt_gen);
1790     Py_CLEAR(o->agt_args);
1791     PyObject_GC_Del(o);
1792 }
1793 
1794 
1795 static int
async_gen_athrow_traverse(PyAsyncGenAThrow * o,visitproc visit,void * arg)1796 async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg)
1797 {
1798     Py_VISIT(o->agt_gen);
1799     Py_VISIT(o->agt_args);
1800     return 0;
1801 }
1802 
1803 
1804 static PyObject *
async_gen_athrow_send(PyAsyncGenAThrow * o,PyObject * arg)1805 async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
1806 {
1807     PyGenObject *gen = (PyGenObject*)o->agt_gen;
1808     PyFrameObject *f = gen->gi_frame;
1809     PyObject *retval;
1810 
1811     if (f == NULL || f->f_stacktop == NULL ||
1812             o->agt_state == AWAITABLE_STATE_CLOSED) {
1813         PyErr_SetNone(PyExc_StopIteration);
1814         return NULL;
1815     }
1816 
1817     if (o->agt_state == AWAITABLE_STATE_INIT) {
1818         if (o->agt_gen->ag_running_async) {
1819             if (o->agt_args == NULL) {
1820                 PyErr_SetString(
1821                     PyExc_RuntimeError,
1822                     "aclose(): asynchronous generator is already running");
1823             }
1824             else {
1825                 PyErr_SetString(
1826                     PyExc_RuntimeError,
1827                     "athrow(): asynchronous generator is already running");
1828             }
1829             return NULL;
1830         }
1831 
1832         if (o->agt_gen->ag_closed) {
1833             o->agt_state = AWAITABLE_STATE_CLOSED;
1834             PyErr_SetNone(PyExc_StopAsyncIteration);
1835             return NULL;
1836         }
1837 
1838         if (arg != Py_None) {
1839             PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
1840             return NULL;
1841         }
1842 
1843         o->agt_state = AWAITABLE_STATE_ITER;
1844         o->agt_gen->ag_running_async = 1;
1845 
1846         if (o->agt_args == NULL) {
1847             /* aclose() mode */
1848             o->agt_gen->ag_closed = 1;
1849 
1850             retval = _gen_throw((PyGenObject *)gen,
1851                                 0,  /* Do not close generator when
1852                                        PyExc_GeneratorExit is passed */
1853                                 PyExc_GeneratorExit, NULL, NULL);
1854 
1855             if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
1856                 Py_DECREF(retval);
1857                 goto yield_close;
1858             }
1859         } else {
1860             PyObject *typ;
1861             PyObject *tb = NULL;
1862             PyObject *val = NULL;
1863 
1864             if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3,
1865                                    &typ, &val, &tb)) {
1866                 return NULL;
1867             }
1868 
1869             retval = _gen_throw((PyGenObject *)gen,
1870                                 0,  /* Do not close generator when
1871                                        PyExc_GeneratorExit is passed */
1872                                 typ, val, tb);
1873             retval = async_gen_unwrap_value(o->agt_gen, retval);
1874         }
1875         if (retval == NULL) {
1876             goto check_error;
1877         }
1878         return retval;
1879     }
1880 
1881     assert(o->agt_state == AWAITABLE_STATE_ITER);
1882 
1883     retval = gen_send_ex((PyGenObject *)gen, arg, 0, 0);
1884     if (o->agt_args) {
1885         return async_gen_unwrap_value(o->agt_gen, retval);
1886     } else {
1887         /* aclose() mode */
1888         if (retval) {
1889             if (_PyAsyncGenWrappedValue_CheckExact(retval)) {
1890                 o->agt_gen->ag_running_async = 0;
1891                 Py_DECREF(retval);
1892                 goto yield_close;
1893             }
1894             else {
1895                 return retval;
1896             }
1897         }
1898         else {
1899             goto check_error;
1900         }
1901     }
1902 
1903 yield_close:
1904     o->agt_gen->ag_running_async = 0;
1905     PyErr_SetString(
1906         PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
1907     return NULL;
1908 
1909 check_error:
1910     o->agt_gen->ag_running_async = 0;
1911     if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
1912             PyErr_ExceptionMatches(PyExc_GeneratorExit))
1913     {
1914         o->agt_state = AWAITABLE_STATE_CLOSED;
1915         if (o->agt_args == NULL) {
1916             /* when aclose() is called we don't want to propagate
1917                StopAsyncIteration or GeneratorExit; just raise
1918                StopIteration, signalling that this 'aclose()' await
1919                is done.
1920             */
1921             PyErr_Clear();
1922             PyErr_SetNone(PyExc_StopIteration);
1923         }
1924     }
1925     return NULL;
1926 }
1927 
1928 
1929 static PyObject *
async_gen_athrow_throw(PyAsyncGenAThrow * o,PyObject * args)1930 async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
1931 {
1932     PyObject *retval;
1933 
1934     if (o->agt_state == AWAITABLE_STATE_CLOSED) {
1935         PyErr_SetNone(PyExc_StopIteration);
1936         return NULL;
1937     }
1938 
1939     retval = gen_throw((PyGenObject*)o->agt_gen, args);
1940     if (o->agt_args) {
1941         return async_gen_unwrap_value(o->agt_gen, retval);
1942     } else {
1943         /* aclose() mode */
1944         if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
1945             o->agt_gen->ag_running_async = 0;
1946             Py_DECREF(retval);
1947             PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
1948             return NULL;
1949         }
1950         if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
1951             PyErr_ExceptionMatches(PyExc_GeneratorExit))
1952         {
1953             /* when aclose() is called we don't want to propagate
1954                StopAsyncIteration or GeneratorExit; just raise
1955                StopIteration, signalling that this 'aclose()' await
1956                is done.
1957             */
1958             PyErr_Clear();
1959             PyErr_SetNone(PyExc_StopIteration);
1960         }
1961         return retval;
1962     }
1963 }
1964 
1965 
1966 static PyObject *
async_gen_athrow_iternext(PyAsyncGenAThrow * o)1967 async_gen_athrow_iternext(PyAsyncGenAThrow *o)
1968 {
1969     return async_gen_athrow_send(o, Py_None);
1970 }
1971 
1972 
1973 static PyObject *
async_gen_athrow_close(PyAsyncGenAThrow * o,PyObject * args)1974 async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
1975 {
1976     o->agt_state = AWAITABLE_STATE_CLOSED;
1977     Py_RETURN_NONE;
1978 }
1979 
1980 
1981 static PyMethodDef async_gen_athrow_methods[] = {
1982     {"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
1983     {"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc},
1984     {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
1985     {NULL, NULL}        /* Sentinel */
1986 };
1987 
1988 
1989 static PyAsyncMethods async_gen_athrow_as_async = {
1990     PyObject_SelfIter,                          /* am_await */
1991     0,                                          /* am_aiter */
1992     0                                           /* am_anext */
1993 };
1994 
1995 
1996 PyTypeObject _PyAsyncGenAThrow_Type = {
1997     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1998     "async_generator_athrow",                   /* tp_name */
1999     sizeof(PyAsyncGenAThrow),                   /* tp_basicsize */
2000     0,                                          /* tp_itemsize */
2001     /* methods */
2002     (destructor)async_gen_athrow_dealloc,       /* tp_dealloc */
2003     0,                                          /* tp_vectorcall_offset */
2004     0,                                          /* tp_getattr */
2005     0,                                          /* tp_setattr */
2006     &async_gen_athrow_as_async,                 /* tp_as_async */
2007     0,                                          /* tp_repr */
2008     0,                                          /* tp_as_number */
2009     0,                                          /* tp_as_sequence */
2010     0,                                          /* tp_as_mapping */
2011     0,                                          /* tp_hash */
2012     0,                                          /* tp_call */
2013     0,                                          /* tp_str */
2014     PyObject_GenericGetAttr,                    /* tp_getattro */
2015     0,                                          /* tp_setattro */
2016     0,                                          /* tp_as_buffer */
2017     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
2018     0,                                          /* tp_doc */
2019     (traverseproc)async_gen_athrow_traverse,    /* tp_traverse */
2020     0,                                          /* tp_clear */
2021     0,                                          /* tp_richcompare */
2022     0,                                          /* tp_weaklistoffset */
2023     PyObject_SelfIter,                          /* tp_iter */
2024     (iternextfunc)async_gen_athrow_iternext,    /* tp_iternext */
2025     async_gen_athrow_methods,                   /* tp_methods */
2026     0,                                          /* tp_members */
2027     0,                                          /* tp_getset */
2028     0,                                          /* tp_base */
2029     0,                                          /* tp_dict */
2030     0,                                          /* tp_descr_get */
2031     0,                                          /* tp_descr_set */
2032     0,                                          /* tp_dictoffset */
2033     0,                                          /* tp_init */
2034     0,                                          /* tp_alloc */
2035     0,                                          /* tp_new */
2036 };
2037 
2038 
2039 static PyObject *
async_gen_athrow_new(PyAsyncGenObject * gen,PyObject * args)2040 async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
2041 {
2042     PyAsyncGenAThrow *o;
2043     o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type);
2044     if (o == NULL) {
2045         return NULL;
2046     }
2047     o->agt_gen = gen;
2048     o->agt_args = args;
2049     o->agt_state = AWAITABLE_STATE_INIT;
2050     Py_INCREF(gen);
2051     Py_XINCREF(args);
2052     _PyObject_GC_TRACK((PyObject*)o);
2053     return (PyObject*)o;
2054 }
2055