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