ยปCore Development>Code coverage>Objects/genobject.c

Python code coverage for Objects/genobject.c

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