Coverage Report

Created: 2026-06-21 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/genobject.c
Line
Count
Source
1
/* Generator object implementation */
2
3
#define _PY_INTERPRETER
4
5
#include "Python.h"
6
#include "pycore_call.h"          // _PyObject_CallNoArgs()
7
#include "pycore_ceval.h"         // _PyEval_EvalFrame()
8
#include "pycore_frame.h"         // _PyInterpreterFrame
9
#include "pycore_freelist.h"      // _Py_FREELIST_FREE()
10
#include "pycore_gc.h"            // _PyGC_CLEAR_FINALIZED()
11
#include "pycore_genobject.h"     // _PyGen_SetStopIterationValue()
12
#include "pycore_interpframe.h"   // _PyFrame_GetCode()
13
#include "pycore_lock.h"          // _Py_yield()
14
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
15
#include "pycore_object.h"        // _PyObject_GC_UNTRACK()
16
#include "pycore_opcode_utils.h"  // RESUME_AFTER_YIELD_FROM
17
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_UINT8_RELAXED()
18
#include "pycore_pyerrors.h"      // _PyErr_ClearExcState()
19
#include "pycore_pystate.h"       // _PyThreadState_GET()
20
#include "pycore_warnings.h"      // _PyErr_WarnUnawaitedCoroutine()
21
#include "pycore_weakref.h"       // FT_CLEAR_WEAKREFS()
22
23
24
#include "opcode_ids.h"           // RESUME, etc
25
26
// Forward declarations
27
static PyObject* gen_close(PyObject *, PyObject *);
28
static PyObject* async_gen_asend_new(PyAsyncGenObject *, PyObject *);
29
static PyObject* async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
30
31
32
#define _PyGen_CAST(op) \
33
77.3M
    _Py_CAST(PyGenObject*, (op))
34
#define _PyCoroObject_CAST(op) \
35
0
    (assert(PyCoro_CheckExact(op)), \
36
0
     _Py_CAST(PyCoroObject*, (op)))
37
#define _PyAsyncGenObject_CAST(op) \
38
0
    _Py_CAST(PyAsyncGenObject*, (op))
39
40
#ifdef Py_GIL_DISABLED
41
static bool
42
gen_try_set_frame_state(PyGenObject *gen, int8_t *expected, int8_t state)
43
{
44
    if (*expected == FRAME_SUSPENDED_YIELD_FROM_LOCKED) {
45
        // Wait for the in-progress gi_yieldfrom read to complete
46
        _Py_yield();
47
        *expected = _Py_atomic_load_int8_relaxed(&gen->gi_frame_state);
48
        return false;
49
    }
50
    return _Py_atomic_compare_exchange_int8(&gen->gi_frame_state, expected, state);
51
}
52
53
# define _Py_GEN_TRY_SET_FRAME_STATE(gen, expected, state) \
54
    gen_try_set_frame_state((gen), &(expected), (state))
55
#else
56
# define _Py_GEN_TRY_SET_FRAME_STATE(gen, expected, state) \
57
52.5M
    ((gen)->gi_frame_state = (state), true)
58
#endif
59
60
61
static const char *NON_INIT_CORO_MSG = "can't send non-None value to a "
62
                                 "just-started coroutine";
63
64
static const char *ASYNC_GEN_IGNORED_EXIT_MSG =
65
                                 "async generator ignored GeneratorExit";
66
67
/* Returns a borrowed reference */
68
static inline PyCodeObject *
69
157k
_PyGen_GetCode(PyGenObject *gen) {
70
157k
    return _PyFrame_GetCode(&gen->gi_iframe);
71
157k
}
72
73
PyCodeObject *
74
0
PyGen_GetCode(PyGenObject *gen) {
75
0
    assert(PyGen_Check(gen));
76
0
    PyCodeObject *res = _PyGen_GetCode(gen);
77
0
    Py_INCREF(res);
78
0
    return res;
79
0
}
80
81
static int
82
gen_traverse(PyObject *self, visitproc visit, void *arg)
83
438k
{
84
438k
    PyGenObject *gen = _PyGen_CAST(self);
85
438k
    Py_VISIT(gen->gi_name);
86
438k
    Py_VISIT(gen->gi_qualname);
87
438k
    if (gen->gi_frame_state != FRAME_CLEARED) {
88
437k
        _PyInterpreterFrame *frame = &gen->gi_iframe;
89
437k
        assert(frame->frame_obj == NULL ||
90
437k
               frame->frame_obj->f_frame->owner == FRAME_OWNED_BY_GENERATOR);
91
437k
        int err = _PyFrame_Traverse(frame, visit, arg);
92
437k
        if (err) {
93
0
            return err;
94
0
        }
95
437k
    }
96
787
    else {
97
        // We still need to visit the code object when the frame is cleared to
98
        // ensure that it's kept alive if the reference is deferred.
99
787
        _Py_VISIT_STACKREF(gen->gi_iframe.f_executable);
100
787
    }
101
    /* No need to visit cr_origin, because it's just tuples/str/int, so can't
102
       participate in a reference cycle. */
103
438k
    Py_VISIT(gen->gi_exc_state.exc_value);
104
438k
    return 0;
105
438k
}
106
107
static void
108
gen_finalize(PyObject *self)
109
24.4M
{
110
24.4M
    PyGenObject *gen = (PyGenObject *)self;
111
112
24.4M
    if (FRAME_STATE_FINISHED(gen->gi_frame_state)) {
113
        /* Generator isn't paused, so no need to close */
114
24.3M
        return;
115
24.3M
    }
116
117
157k
    if (PyAsyncGen_CheckExact(self)) {
118
36
        PyAsyncGenObject *agen = (PyAsyncGenObject*)self;
119
36
        PyObject *finalizer = agen->ag_origin_or_finalizer;
120
36
        if (finalizer && !agen->ag_closed) {
121
            /* Save the current exception, if any. */
122
0
            PyObject *exc = PyErr_GetRaisedException();
123
124
0
            PyObject *res = PyObject_CallOneArg(finalizer, self);
125
0
            if (res == NULL) {
126
0
                PyErr_FormatUnraisable("Exception ignored while "
127
0
                                       "finalizing generator %R", self);
128
0
            }
129
0
            else {
130
0
                Py_DECREF(res);
131
0
            }
132
            /* Restore the saved exception. */
133
0
            PyErr_SetRaisedException(exc);
134
0
            return;
135
0
        }
136
36
    }
137
138
    /* Save the current exception, if any. */
139
157k
    PyObject *exc = PyErr_GetRaisedException();
140
141
    /* If `gen` is a coroutine, and if it was never awaited on,
142
       issue a RuntimeWarning. */
143
157k
    assert(_PyGen_GetCode(gen) != NULL);
144
157k
    if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE &&
145
0
        gen->gi_frame_state == FRAME_CREATED)
146
0
    {
147
0
        _PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
148
0
    }
149
157k
    else {
150
157k
        PyObject *res = gen_close((PyObject*)gen, NULL);
151
157k
        if (res == NULL) {
152
0
            if (PyErr_Occurred()) {
153
0
                PyErr_FormatUnraisable("Exception ignored while "
154
0
                                       "closing generator %R", self);
155
0
            }
156
0
        }
157
157k
        else {
158
157k
            Py_DECREF(res);
159
157k
        }
160
157k
    }
161
162
    /* Restore the saved exception. */
163
157k
    PyErr_SetRaisedException(exc);
164
157k
}
165
166
static void
167
gen_clear_frame(PyGenObject *gen)
168
157k
{
169
157k
    assert(FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state) == FRAME_CLEARED);
170
157k
    _PyInterpreterFrame *frame = &gen->gi_iframe;
171
157k
    _PyThreadState_UpdateLastProfiledFrame(_PyThreadState_GET(), frame, frame->previous);
172
157k
    frame->previous = NULL;
173
157k
    _PyFrame_ClearExceptCode(frame);
174
157k
    _PyErr_ClearExcState(&gen->gi_exc_state);
175
157k
}
176
177
int
178
_PyGen_ClearFrame(PyGenObject *gen)
179
0
{
180
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
181
0
    do {
182
0
        if (FRAME_STATE_FINISHED(frame_state)) {
183
0
            return 0;
184
0
        }
185
0
        else if (frame_state == FRAME_EXECUTING) {
186
0
            PyErr_SetString(PyExc_RuntimeError,
187
0
                            "cannot clear an executing frame");
188
0
            return -1;
189
0
        }
190
0
        else if (FRAME_STATE_SUSPENDED(frame_state)) {
191
0
            PyErr_SetString(PyExc_RuntimeError,
192
0
                            "cannot clear an suspended frame");
193
0
            return -1;
194
0
        }
195
0
        assert(frame_state == FRAME_CREATED);
196
0
    } while (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_CLEARED));
197
198
0
    if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE) {
199
0
        _PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
200
0
    }
201
0
    gen_clear_frame(gen);
202
0
    return 0;
203
0
}
204
205
static void
206
gen_dealloc(PyObject *self)
207
24.4M
{
208
24.4M
    PyGenObject *gen = _PyGen_CAST(self);
209
210
24.4M
    _PyObject_GC_UNTRACK(gen);
211
212
24.4M
    FT_CLEAR_WEAKREFS(self, gen->gi_weakreflist);
213
214
24.4M
    _PyObject_GC_TRACK(self);
215
216
24.4M
    if (PyObject_CallFinalizerFromDealloc(self))
217
0
        return;                     /* resurrected.  :( */
218
219
24.4M
    _PyObject_GC_UNTRACK(self);
220
24.4M
    if (PyAsyncGen_CheckExact(gen)) {
221
        /* We have to handle this case for asynchronous generators
222
           right here, because this code has to be between UNTRACK
223
           and GC_Del. */
224
36
        Py_CLEAR(((PyAsyncGenObject*)gen)->ag_origin_or_finalizer);
225
36
    }
226
24.4M
    if (PyCoro_CheckExact(gen)) {
227
36
        Py_CLEAR(((PyCoroObject *)gen)->cr_origin_or_finalizer);
228
36
    }
229
24.4M
    if (gen->gi_frame_state != FRAME_CLEARED) {
230
0
        gen->gi_frame_state = FRAME_CLEARED;
231
0
        gen_clear_frame(gen);
232
0
    }
233
24.4M
    assert(gen->gi_exc_state.exc_value == NULL);
234
24.4M
    PyStackRef_CLEAR(gen->gi_iframe.f_executable);
235
24.4M
    Py_CLEAR(gen->gi_name);
236
24.4M
    Py_CLEAR(gen->gi_qualname);
237
238
24.4M
    PyObject_GC_Del(gen);
239
24.4M
}
240
241
static void
242
gen_raise_already_executing_error(PyGenObject *gen)
243
0
{
244
0
    const char *msg = "generator already executing";
245
0
    if (PyCoro_CheckExact(gen)) {
246
0
        msg = "coroutine already executing";
247
0
    }
248
0
    else if (PyAsyncGen_CheckExact(gen)) {
249
0
        msg = "async generator already executing";
250
0
    }
251
0
    PyErr_SetString(PyExc_ValueError, msg);
252
0
}
253
254
// Send 'arg' into 'gen'. On success, return PYGEN_NEXT or PYGEN_RETURN.
255
// Returns PYGEN_ERROR on failure. 'presult' is set to the yielded or
256
// returned value.
257
// The generator must be in the FRAME_EXECUTING state when this function
258
// is called.
259
static PySendResult
260
gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, int exc)
261
52.2M
{
262
52.2M
    assert(FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state) == FRAME_EXECUTING);
263
264
52.2M
    PyThreadState *tstate = _PyThreadState_GET();
265
52.2M
    _PyInterpreterFrame *frame = &gen->gi_iframe;
266
267
    /* Push arg onto the frame's value stack */
268
52.2M
    PyObject *arg_obj = arg ? arg : Py_None;
269
52.2M
    _PyFrame_StackPush(frame, PyStackRef_FromPyObjectNew(arg_obj));
270
271
52.2M
    _PyErr_StackItem *prev_exc_info = tstate->exc_info;
272
52.2M
    gen->gi_exc_state.previous_item = prev_exc_info;
273
52.2M
    tstate->exc_info = &gen->gi_exc_state;
274
275
52.2M
    if (exc) {
276
48.9k
        assert(_PyErr_Occurred(tstate));
277
48.9k
        _PyErr_ChainStackItem();
278
48.9k
    }
279
280
52.2M
    EVAL_CALL_STAT_INC(EVAL_CALL_GENERATOR);
281
52.2M
    PyObject *result = _PyEval_EvalFrame(tstate, frame, exc);
282
52.2M
    assert(tstate->exc_info == prev_exc_info);
283
52.2M
#ifndef Py_GIL_DISABLED
284
52.2M
    assert(gen->gi_exc_state.previous_item == NULL);
285
52.2M
    assert(frame->previous == NULL);
286
52.2M
    assert(gen->gi_frame_state != FRAME_EXECUTING);
287
52.2M
#endif
288
289
    // The generator_return_kind field is used to distinguish between a
290
    // yield and a return from within _PyEval_EvalFrame(). Earlier versions
291
    // of CPython (prior to 3.15) used gi_frame_state for this purpose, but
292
    // that requires the GIL for thread-safety.
293
52.2M
    int return_kind = ((_PyThreadStateImpl *)tstate)->generator_return_kind;
294
295
52.2M
    if (return_kind == GENERATOR_YIELD) {
296
41.0M
        assert(result != NULL && !_PyErr_Occurred(tstate));
297
41.0M
#ifndef Py_GIL_DISABLED
298
41.0M
        assert(FRAME_STATE_SUSPENDED(gen->gi_frame_state));
299
41.0M
#endif
300
41.0M
        *presult = result;
301
41.0M
        return PYGEN_NEXT;
302
41.0M
    }
303
304
52.2M
    assert(return_kind == GENERATOR_RETURN);
305
11.1M
    assert(gen->gi_exc_state.exc_value == NULL);
306
11.1M
    assert(FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state) == FRAME_CLEARED);
307
308
    /* If the generator just returned (as opposed to yielding), signal
309
     * that the generator is exhausted. */
310
11.1M
    if (result) {
311
11.1M
        assert(result == Py_None || !PyAsyncGen_CheckExact(gen));
312
11.1M
        if (result == Py_None && !PyAsyncGen_CheckExact(gen) && !arg) {
313
            /* Return NULL if called by gen_iternext() */
314
11.1M
            Py_CLEAR(result);
315
11.1M
        }
316
11.1M
    }
317
67.8k
    else {
318
67.8k
        assert(!PyErr_ExceptionMatches(PyExc_StopIteration));
319
67.8k
        assert(!PyAsyncGen_CheckExact(gen) ||
320
67.8k
            !PyErr_ExceptionMatches(PyExc_StopAsyncIteration));
321
67.8k
    }
322
323
11.1M
    *presult = result;
324
11.1M
    return result ? PYGEN_RETURN : PYGEN_ERROR;
325
52.2M
}
326
327
// Set the generator 'gen' to the executing state and send 'arg' into it.
328
// See gen_send_ex2() for details.
329
static PySendResult
330
gen_send_ex(PyGenObject *gen, PyObject *arg, PyObject **presult)
331
52.2M
{
332
52.2M
    *presult = NULL;
333
52.2M
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
334
52.2M
    do {
335
52.2M
        if (frame_state == FRAME_CREATED && arg && arg != Py_None) {
336
0
            const char *msg = "can't send non-None value to a "
337
0
                                "just-started generator";
338
0
            if (PyCoro_CheckExact(gen)) {
339
0
                msg = NON_INIT_CORO_MSG;
340
0
            }
341
0
            else if (PyAsyncGen_CheckExact(gen)) {
342
0
                msg = "can't send non-None value to a "
343
0
                        "just-started async generator";
344
0
            }
345
0
            PyErr_SetString(PyExc_TypeError, msg);
346
0
            return PYGEN_ERROR;
347
0
        }
348
52.2M
        if (frame_state == FRAME_EXECUTING) {
349
0
            gen_raise_already_executing_error(gen);
350
0
            return PYGEN_ERROR;
351
0
        }
352
52.2M
        if (FRAME_STATE_FINISHED(frame_state)) {
353
21.5k
            if (PyCoro_CheckExact(gen)) {
354
                /* `gen` is an exhausted coroutine: raise an error,
355
                except when called from gen_close(), which should
356
                always be a silent method. */
357
0
                PyErr_SetString(
358
0
                    PyExc_RuntimeError,
359
0
                    "cannot reuse already awaited coroutine");
360
0
            }
361
21.5k
            else if (arg) {
362
                /* `gen` is an exhausted generator:
363
                only return value if called from send(). */
364
0
                *presult = Py_None;
365
0
                return PYGEN_RETURN;
366
0
            }
367
21.5k
            return PYGEN_ERROR;
368
21.5k
        }
369
370
52.2M
        assert((frame_state == FRAME_CREATED) ||
371
52.2M
               FRAME_STATE_SUSPENDED(frame_state));
372
52.2M
    } while (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_EXECUTING));
373
374
52.2M
    return gen_send_ex2(gen, arg, presult, 0);
375
52.2M
}
376
377
static PySendResult
378
PyGen_am_send(PyObject *self, PyObject *arg, PyObject **result)
379
0
{
380
0
    PyGenObject *gen = _PyGen_CAST(self);
381
0
    return gen_send_ex(gen, arg, result);
382
0
}
383
384
static PyObject *
385
gen_set_stop_iteration(PyGenObject *gen, PyObject *result)
386
0
{
387
0
    if (PyAsyncGen_CheckExact(gen)) {
388
0
        assert(result == Py_None);
389
0
        PyErr_SetNone(PyExc_StopAsyncIteration);
390
0
    }
391
0
    else if (result == Py_None) {
392
0
        PyErr_SetNone(PyExc_StopIteration);
393
0
    }
394
0
    else {
395
0
        _PyGen_SetStopIterationValue(result);
396
0
    }
397
0
    Py_DECREF(result);
398
0
    return NULL;
399
0
}
400
401
PyDoc_STRVAR(send_doc,
402
"send(value) -> send 'value' into generator,\n\
403
return next yielded value or raise StopIteration.");
404
405
static PyObject *
406
gen_send(PyObject *op, PyObject *arg)
407
0
{
408
0
    PyObject *result;
409
0
    PyGenObject *gen = _PyGen_CAST(op);
410
0
    if (gen_send_ex(gen, arg, &result) == PYGEN_RETURN) {
411
0
        return gen_set_stop_iteration(gen, result);
412
0
    }
413
0
    return result;
414
0
}
415
416
PyDoc_STRVAR(close_doc,
417
"close() -> raise GeneratorExit inside generator.");
418
419
/*
420
 *   This helper function is used by gen_close and gen_throw to
421
 *   close a subiterator being delegated to by yield-from.
422
 */
423
424
static int
425
gen_close_iter(PyObject *yf)
426
0
{
427
0
    PyObject *retval = NULL;
428
429
0
    if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
430
0
        retval = gen_close((PyObject *)yf, NULL);
431
0
        if (retval == NULL)
432
0
            return -1;
433
0
    }
434
0
    else {
435
0
        PyObject *meth;
436
0
        if (PyObject_GetOptionalAttr(yf, &_Py_ID(close), &meth) < 0) {
437
0
            PyErr_FormatUnraisable("Exception ignored while "
438
0
                                   "closing generator %R", yf);
439
0
        }
440
0
        if (meth) {
441
0
            retval = _PyObject_CallNoArgs(meth);
442
0
            Py_DECREF(meth);
443
0
            if (retval == NULL)
444
0
                return -1;
445
0
        }
446
0
    }
447
0
    Py_XDECREF(retval);
448
0
    return 0;
449
0
}
450
451
static inline bool
452
is_resume(_Py_CODEUNIT *instr)
453
23.4k
{
454
23.4k
    uint8_t code = FT_ATOMIC_LOAD_UINT8_RELAXED(instr->op.code);
455
23.4k
    return (
456
23.4k
        code == RESUME ||
457
21.9k
        code == RESUME_CHECK ||
458
0
        code == RESUME_CHECK_JIT ||
459
0
        code == INSTRUMENTED_RESUME
460
23.4k
    );
461
23.4k
}
462
463
static PyObject *
464
gen_close(PyObject *self, PyObject *args)
465
157k
{
466
157k
    PyGenObject *gen = _PyGen_CAST(self);
467
468
157k
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
469
157k
    do {
470
157k
        if (frame_state == FRAME_CREATED) {
471
            // && (1) to avoid -Wunreachable-code warning on Clang
472
134k
            if (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_CLEARED) && (1)) {
473
0
                continue;
474
0
            }
475
134k
            gen_clear_frame(gen);
476
134k
            Py_RETURN_NONE;
477
134k
        }
478
479
23.4k
        if (FRAME_STATE_FINISHED(frame_state)) {
480
0
            Py_RETURN_NONE;
481
0
        }
482
483
23.4k
        if (frame_state == FRAME_EXECUTING) {
484
0
            gen_raise_already_executing_error(gen);
485
0
            return NULL;
486
0
        }
487
488
23.4k
        assert(FRAME_STATE_SUSPENDED(frame_state));
489
23.4k
    } while (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_EXECUTING));
490
491
23.4k
    int err = 0;
492
23.4k
    _PyInterpreterFrame *frame = &gen->gi_iframe;
493
23.4k
    if (frame_state == FRAME_SUSPENDED_YIELD_FROM) {
494
0
        PyObject *yf = PyStackRef_AsPyObjectNew(_PyFrame_StackPeek(frame, 2));
495
0
        err = gen_close_iter(yf);
496
0
        Py_DECREF(yf);
497
0
    }
498
499
23.4k
    if (is_resume(frame->instr_ptr)) {
500
23.4k
        bool no_unwind_tools = _PyEval_NoToolsForUnwind(_PyThreadState_GET(), frame);
501
        /* We can safely ignore the outermost try block
502
         * as it is automatically generated to handle
503
         * StopIteration. */
504
23.4k
        int oparg = frame->instr_ptr->op.arg;
505
23.4k
        if (oparg & RESUME_OPARG_DEPTH1_MASK && no_unwind_tools) {
506
            // RESUME after YIELD_VALUE and exception depth is 1
507
23.0k
            assert((oparg & RESUME_OPARG_LOCATION_MASK) != RESUME_AT_FUNC_START);
508
23.0k
            FT_ATOMIC_STORE_INT8_RELEASE(gen->gi_frame_state, FRAME_CLEARED);
509
23.0k
            gen_clear_frame(gen);
510
23.0k
            Py_RETURN_NONE;
511
23.0k
        }
512
23.4k
    }
513
374
    if (err == 0) {
514
374
        PyErr_SetNone(PyExc_GeneratorExit);
515
374
    }
516
517
374
    PyObject *retval;
518
374
    if (gen_send_ex2(gen, Py_None, &retval, 1) == PYGEN_RETURN) {
519
        // the generator returned a value while closing, return the value here
520
0
        assert(!PyErr_Occurred());
521
0
        return retval;
522
0
    }
523
374
    else if (retval) {
524
0
        const char *msg = "generator ignored GeneratorExit";
525
0
        if (PyCoro_CheckExact(gen)) {
526
0
            msg = "coroutine ignored GeneratorExit";
527
0
        } else if (PyAsyncGen_CheckExact(gen)) {
528
0
            msg = ASYNC_GEN_IGNORED_EXIT_MSG;
529
0
        }
530
0
        Py_DECREF(retval);
531
0
        PyErr_SetString(PyExc_RuntimeError, msg);
532
0
        return NULL;
533
0
    }
534
374
    assert(PyErr_Occurred());
535
536
374
    if (PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
537
374
        PyErr_Clear();          /* ignore this error */
538
374
        Py_RETURN_NONE;
539
374
    }
540
0
    return NULL;
541
374
}
542
543
// Set an exception for a gen.throw() call.
544
// Return 0 on success, -1 on failure.
545
static int
546
gen_set_exception(PyObject *typ, PyObject *val, PyObject *tb)
547
48.5k
{
548
    /* First, check the traceback argument, replacing None with
549
       NULL. */
550
48.5k
    if (tb == Py_None) {
551
0
        tb = NULL;
552
0
    }
553
48.5k
    else if (tb != NULL && !PyTraceBack_Check(tb)) {
554
0
        PyErr_SetString(PyExc_TypeError,
555
0
            "throw() third argument must be a traceback object");
556
0
        return -1;
557
0
    }
558
559
48.5k
    Py_INCREF(typ);
560
48.5k
    Py_XINCREF(val);
561
48.5k
    Py_XINCREF(tb);
562
563
48.5k
    if (PyExceptionClass_Check(typ)) {
564
0
        PyErr_NormalizeException(&typ, &val, &tb);
565
0
    }
566
48.5k
    else if (PyExceptionInstance_Check(typ)) {
567
        /* Raising an instance.  The value should be a dummy. */
568
48.5k
        if (val && val != Py_None) {
569
0
            PyErr_SetString(PyExc_TypeError,
570
0
              "instance exception may not have a separate value");
571
0
            goto failed_throw;
572
0
        }
573
48.5k
        else {
574
            /* Normalize to raise <class>, <instance> */
575
48.5k
            Py_XSETREF(val, typ);
576
48.5k
            typ = Py_NewRef(PyExceptionInstance_Class(typ));
577
578
48.5k
            if (tb == NULL)
579
                /* Returns NULL if there's no traceback */
580
48.5k
                tb = PyException_GetTraceback(val);
581
48.5k
        }
582
48.5k
    }
583
0
    else {
584
        /* Not something you can raise.  throw() fails. */
585
0
        PyErr_Format(PyExc_TypeError,
586
0
                     "exceptions must be classes or instances "
587
0
                     "deriving from BaseException, not %s",
588
0
                     Py_TYPE(typ)->tp_name);
589
0
            goto failed_throw;
590
0
    }
591
592
48.5k
    PyErr_Restore(typ, val, tb);
593
48.5k
    return 0;
594
595
0
failed_throw:
596
    /* Didn't use our arguments, so restore their original refcounts */
597
0
    Py_DECREF(typ);
598
0
    Py_XDECREF(val);
599
0
    Py_XDECREF(tb);
600
0
    return -1;
601
48.5k
}
602
603
static PyObject *
604
gen_throw_current_exception(PyGenObject *gen)
605
48.5k
{
606
48.5k
    assert(FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state) == FRAME_EXECUTING);
607
608
48.5k
    PyObject *result;
609
48.5k
    if (gen_send_ex2(gen, Py_None, &result, 1) == PYGEN_RETURN) {
610
0
        return gen_set_stop_iteration(gen, result);
611
0
    }
612
48.5k
    return result;
613
48.5k
}
614
615
PyDoc_STRVAR(throw_doc,
616
"throw(value)\n\
617
throw(type[,value[,tb]])\n\
618
\n\
619
Raise exception in generator, return next yielded value or raise\n\
620
StopIteration.\n\
621
the (type, val, tb) signature is deprecated, \n\
622
and may be removed in a future version of Python.");
623
624
static PyObject *
625
_gen_throw(PyGenObject *gen, int close_on_genexit,
626
           PyObject *typ, PyObject *val, PyObject *tb)
627
48.5k
{
628
48.5k
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
629
48.5k
    do {
630
48.5k
        if (frame_state == FRAME_EXECUTING) {
631
0
            gen_raise_already_executing_error(gen);
632
0
            return NULL;
633
0
        }
634
635
48.5k
        if (FRAME_STATE_FINISHED(frame_state)) {
636
0
            if (PyCoro_CheckExact(gen)) {
637
                /* `gen` is an exhausted coroutine: raise an error */
638
0
                PyErr_SetString(
639
0
                    PyExc_RuntimeError,
640
0
                    "cannot reuse already awaited coroutine");
641
0
                return NULL;
642
0
            }
643
0
            gen_set_exception(typ, val, tb);
644
0
            return NULL;
645
0
        }
646
647
48.5k
        assert((frame_state == FRAME_CREATED) ||
648
48.5k
               FRAME_STATE_SUSPENDED(frame_state));
649
48.5k
    } while (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_EXECUTING));
650
651
48.5k
    if (frame_state == FRAME_SUSPENDED_YIELD_FROM) {
652
0
        _PyInterpreterFrame *frame = &gen->gi_iframe;
653
0
        PyObject *yf = PyStackRef_AsPyObjectNew(_PyFrame_StackPeek(frame, 2));
654
0
        PyObject *ret;
655
0
        int err;
656
0
        if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
657
0
            close_on_genexit
658
0
        ) {
659
            /* Asynchronous generators *should not* be closed right away.
660
               We have to allow some awaits to work it through, hence the
661
               `close_on_genexit` parameter here.
662
            */
663
0
            err = gen_close_iter(yf);
664
0
            Py_DECREF(yf);
665
0
            if (err < 0) {
666
0
                return gen_throw_current_exception(gen);
667
0
            }
668
0
            goto throw_here;
669
0
        }
670
0
        PyThreadState *tstate = _PyThreadState_GET();
671
0
        assert(tstate != NULL);
672
0
        if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
673
            /* `yf` is a generator or a coroutine. */
674
675
            /* Link frame into the stack to enable complete backtraces. */
676
            /* XXX We should probably be updating the current frame somewhere in
677
               ceval.c. */
678
0
            _PyInterpreterFrame *prev = tstate->current_frame;
679
0
            frame->previous = prev;
680
0
            tstate->current_frame = frame;
681
            /* Close the generator that we are currently iterating with
682
               'yield from' or awaiting on with 'await'. */
683
0
            ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
684
0
                             typ, val, tb);
685
0
            _PyThreadState_UpdateLastProfiledFrame(tstate, frame, prev);
686
0
            tstate->current_frame = prev;
687
0
            frame->previous = NULL;
688
0
        }
689
0
        else {
690
            /* `yf` is an iterator or a coroutine-like object. */
691
0
            PyObject *meth;
692
0
            if (PyObject_GetOptionalAttr(yf, &_Py_ID(throw), &meth) < 0) {
693
0
                Py_DECREF(yf);
694
0
                FT_ATOMIC_STORE_INT8_RELEASE(gen->gi_frame_state, frame_state);
695
0
                return NULL;
696
0
            }
697
0
            if (meth == NULL) {
698
0
                Py_DECREF(yf);
699
0
                goto throw_here;
700
0
            }
701
702
0
            _PyInterpreterFrame *prev = tstate->current_frame;
703
0
            frame->previous = prev;
704
0
            tstate->current_frame = frame;
705
0
            ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
706
0
            _PyThreadState_UpdateLastProfiledFrame(tstate, frame, prev);
707
0
            tstate->current_frame = prev;
708
0
            frame->previous = NULL;
709
0
            Py_DECREF(meth);
710
0
        }
711
0
        Py_DECREF(yf);
712
0
        if (!ret) {
713
0
            return gen_throw_current_exception(gen);
714
0
        }
715
0
        FT_ATOMIC_STORE_INT8_RELEASE(gen->gi_frame_state, frame_state);
716
0
        return ret;
717
0
    }
718
719
48.5k
throw_here:
720
48.5k
    assert(FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state) == FRAME_EXECUTING);
721
48.5k
    if (gen_set_exception(typ, val, tb) < 0) {
722
0
        FT_ATOMIC_STORE_INT8_RELEASE(gen->gi_frame_state, frame_state);
723
0
        return NULL;
724
0
    }
725
48.5k
    return gen_throw_current_exception(gen);
726
48.5k
}
727
728
729
static PyObject *
730
gen_throw(PyObject *op, PyObject *const *args, Py_ssize_t nargs)
731
48.5k
{
732
48.5k
    PyGenObject *gen = _PyGen_CAST(op);
733
48.5k
    PyObject *typ;
734
48.5k
    PyObject *tb = NULL;
735
48.5k
    PyObject *val = NULL;
736
737
48.5k
    if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) {
738
0
        return NULL;
739
0
    }
740
48.5k
    if (nargs > 1) {
741
0
        if (PyErr_WarnEx(PyExc_DeprecationWarning,
742
0
                            "the (type, exc, tb) signature of throw() is deprecated, "
743
0
                            "use the single-arg signature instead.",
744
0
                            1) < 0) {
745
0
            return NULL;
746
0
        }
747
0
    }
748
48.5k
    typ = args[0];
749
48.5k
    if (nargs == 3) {
750
0
        val = args[1];
751
0
        tb = args[2];
752
0
    }
753
48.5k
    else if (nargs == 2) {
754
0
        val = args[1];
755
0
    }
756
48.5k
    return _gen_throw(gen, 1, typ, val, tb);
757
48.5k
}
758
759
760
static PyObject *
761
gen_iternext(PyObject *self)
762
52.2M
{
763
52.2M
    assert(PyGen_CheckExact(self) || PyCoro_CheckExact(self));
764
52.2M
    PyGenObject *gen = _PyGen_CAST(self);
765
766
52.2M
    PyObject *result;
767
52.2M
    if (gen_send_ex(gen, NULL, &result) == PYGEN_RETURN) {
768
0
        if (result != Py_None) {
769
0
            _PyGen_SetStopIterationValue(result);
770
0
        }
771
0
        Py_CLEAR(result);
772
0
    }
773
52.2M
    return result;
774
52.2M
}
775
776
/*
777
 * Set StopIteration with specified value.  Value can be arbitrary object
778
 * or NULL.
779
 *
780
 * Returns 0 if StopIteration is set and -1 if any other exception is set.
781
 */
782
int
783
_PyGen_SetStopIterationValue(PyObject *value)
784
0
{
785
0
    assert(!PyErr_Occurred());
786
    // Construct an exception instance manually with PyObject_CallOneArg()
787
    // but use PyErr_SetRaisedException() instead of PyErr_SetObject() as
788
    // PyErr_SetObject(exc_type, value) has a fast path when 'value'
789
    // is a tuple, where the value of the StopIteration exception would be
790
    // set to 'value[0]' instead of 'value'.
791
0
    PyObject *exc = value == NULL
792
0
        ? PyObject_CallNoArgs(PyExc_StopIteration)
793
0
        : PyObject_CallOneArg(PyExc_StopIteration, value);
794
0
    if (exc == NULL) {
795
0
        return -1;
796
0
    }
797
0
    PyErr_SetRaisedException(exc /* stolen */);
798
0
    return 0;
799
0
}
800
801
/*
802
 *   If StopIteration exception is set, fetches its 'value'
803
 *   attribute if any, otherwise sets pvalue to None.
804
 *
805
 *   Returns 0 if no exception or StopIteration is set.
806
 *   If any other exception is set, returns -1 and leaves
807
 *   pvalue unchanged.
808
 */
809
810
int
811
_PyGen_FetchStopIterationValue(PyObject **pvalue)
812
0
{
813
0
    PyObject *value = NULL;
814
0
    if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
815
0
        PyObject *exc = PyErr_GetRaisedException();
816
0
        value = Py_NewRef(((PyStopIterationObject *)exc)->value);
817
0
        Py_DECREF(exc);
818
0
    } else if (PyErr_Occurred()) {
819
0
        return -1;
820
0
    }
821
0
    if (value == NULL) {
822
0
        value = Py_NewRef(Py_None);
823
0
    }
824
0
    *pvalue = value;
825
0
    return 0;
826
0
}
827
828
static PyObject *
829
gen_repr(PyObject *self)
830
0
{
831
0
    PyGenObject *gen = _PyGen_CAST(self);
832
0
    return PyUnicode_FromFormat("<generator object %S at %p>",
833
0
                                gen->gi_qualname, gen);
834
0
}
835
836
static PyObject *
837
gen_get_name(PyObject *self, void *Py_UNUSED(ignored))
838
0
{
839
0
    PyGenObject *op = _PyGen_CAST(self);
840
0
    PyObject *name = FT_ATOMIC_LOAD_PTR_ACQUIRE(op->gi_name);
841
0
    return Py_NewRef(name);
842
0
}
843
844
static int
845
gen_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
846
0
{
847
0
    PyGenObject *op = _PyGen_CAST(self);
848
    /* Not legal to del gen.gi_name or to set it to anything
849
     * other than a string object. */
850
0
    if (value == NULL || !PyUnicode_Check(value)) {
851
0
        PyErr_SetString(PyExc_TypeError,
852
0
                        "__name__ must be set to a string object");
853
0
        return -1;
854
0
    }
855
0
    Py_BEGIN_CRITICAL_SECTION(self);
856
    // gh-133931: To prevent use-after-free from other threads that reference
857
    // the gi_name.
858
0
    _PyObject_XSetRefDelayed(&op->gi_name, Py_NewRef(value));
859
0
    Py_END_CRITICAL_SECTION();
860
0
    return 0;
861
0
}
862
863
static PyObject *
864
gen_get_qualname(PyObject *self, void *Py_UNUSED(ignored))
865
0
{
866
0
    PyGenObject *op = _PyGen_CAST(self);
867
0
    PyObject *qualname = FT_ATOMIC_LOAD_PTR_ACQUIRE(op->gi_qualname);
868
0
    return Py_NewRef(qualname);
869
0
}
870
871
static int
872
gen_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
873
0
{
874
0
    PyGenObject *op = _PyGen_CAST(self);
875
    /* Not legal to del gen.__qualname__ or to set it to anything
876
     * other than a string object. */
877
0
    if (value == NULL || !PyUnicode_Check(value)) {
878
0
        PyErr_SetString(PyExc_TypeError,
879
0
                        "__qualname__ must be set to a string object");
880
0
        return -1;
881
0
    }
882
0
    Py_BEGIN_CRITICAL_SECTION(self);
883
    // gh-133931: To prevent use-after-free from other threads that reference
884
    // the gi_qualname.
885
0
    _PyObject_XSetRefDelayed(&op->gi_qualname, Py_NewRef(value));
886
0
    Py_END_CRITICAL_SECTION();
887
0
    return 0;
888
0
}
889
890
static PyObject *
891
gen_getyieldfrom(PyObject *self, void *Py_UNUSED(ignored))
892
0
{
893
0
    PyGenObject *gen = _PyGen_CAST(self);
894
#ifdef Py_GIL_DISABLED
895
    int8_t frame_state = _Py_atomic_load_int8_relaxed(&gen->gi_frame_state);
896
    do {
897
        if (frame_state != FRAME_SUSPENDED_YIELD_FROM &&
898
            frame_state != FRAME_SUSPENDED_YIELD_FROM_LOCKED)
899
        {
900
            Py_RETURN_NONE;
901
        }
902
    } while (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_SUSPENDED_YIELD_FROM_LOCKED));
903
904
    PyObject *result = PyStackRef_AsPyObjectNew(_PyFrame_StackPeek(&gen->gi_iframe, 2));
905
    _Py_atomic_store_int8_release(&gen->gi_frame_state, FRAME_SUSPENDED_YIELD_FROM);
906
    return result;
907
#else
908
0
    int8_t frame_state = gen->gi_frame_state;
909
0
    if (frame_state != FRAME_SUSPENDED_YIELD_FROM) {
910
0
        Py_RETURN_NONE;
911
0
    }
912
0
    return PyStackRef_AsPyObjectNew(_PyFrame_StackPeek(&gen->gi_iframe, 2));
913
0
#endif
914
0
}
915
916
917
static PyObject *
918
gen_getrunning(PyObject *self, void *Py_UNUSED(ignored))
919
0
{
920
0
    PyGenObject *gen = _PyGen_CAST(self);
921
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
922
0
    return frame_state == FRAME_EXECUTING ? Py_True : Py_False;
923
0
}
924
925
static PyObject *
926
gen_getsuspended(PyObject *self, void *Py_UNUSED(ignored))
927
0
{
928
0
    PyGenObject *gen = _PyGen_CAST(self);
929
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
930
0
    return FRAME_STATE_SUSPENDED(frame_state) ? Py_True : Py_False;
931
0
}
932
933
static PyObject *
934
gen_getstate(PyObject *self, void *Py_UNUSED(ignored))
935
0
{
936
0
    PyGenObject *gen = _PyGen_CAST(self);
937
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
938
939
0
    static PyObject *const state_strings[] = {
940
0
        [FRAME_CREATED] = &_Py_ID(GEN_CREATED),
941
0
        [FRAME_SUSPENDED] = &_Py_ID(GEN_SUSPENDED),
942
0
        [FRAME_SUSPENDED_YIELD_FROM] = &_Py_ID(GEN_SUSPENDED),
943
0
        [FRAME_SUSPENDED_YIELD_FROM_LOCKED] = &_Py_ID(GEN_SUSPENDED),
944
0
        [FRAME_EXECUTING] = &_Py_ID(GEN_RUNNING),
945
0
        [FRAME_CLEARED] = &_Py_ID(GEN_CLOSED),
946
0
    };
947
948
0
    assert(frame_state >= 0 &&
949
0
           (size_t)frame_state < Py_ARRAY_LENGTH(state_strings));
950
0
    return state_strings[frame_state];
951
0
}
952
953
static PyObject *
954
_gen_getframe(PyGenObject *gen, const char *const name)
955
0
{
956
0
    if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
957
0
        return NULL;
958
0
    }
959
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
960
0
    if (FRAME_STATE_FINISHED(frame_state)) {
961
0
        Py_RETURN_NONE;
962
0
    }
963
    // TODO: still not thread-safe with free threading
964
0
    return _Py_XNewRef((PyObject *)_PyFrame_GetFrameObject(&gen->gi_iframe));
965
0
}
966
967
static PyObject *
968
gen_getframe(PyObject *self, void *Py_UNUSED(ignored))
969
0
{
970
0
    PyGenObject *gen = _PyGen_CAST(self);
971
0
    return _gen_getframe(gen, "gi_frame");
972
0
}
973
974
static PyObject *
975
_gen_getcode(PyGenObject *gen, const char *const name)
976
0
{
977
0
    if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
978
0
        return NULL;
979
0
    }
980
0
    return Py_NewRef(_PyGen_GetCode(gen));
981
0
}
982
983
static PyObject *
984
gen_getcode(PyObject *self, void *Py_UNUSED(ignored))
985
0
{
986
0
    PyGenObject *gen = _PyGen_CAST(self);
987
0
    return _gen_getcode(gen, "gi_code");
988
0
}
989
990
static PyGetSetDef gen_getsetlist[] = {
991
    {"__name__", gen_get_name, gen_set_name,
992
     PyDoc_STR("name of the generator")},
993
    {"__qualname__", gen_get_qualname, gen_set_qualname,
994
     PyDoc_STR("qualified name of the generator")},
995
    {"gi_yieldfrom", gen_getyieldfrom, NULL,
996
     PyDoc_STR("object being iterated by yield from, or None")},
997
    {"gi_running", gen_getrunning, NULL, NULL},
998
    {"gi_frame", gen_getframe,  NULL, NULL},
999
    {"gi_suspended", gen_getsuspended,  NULL, NULL},
1000
    {"gi_code", gen_getcode,  NULL, NULL},
1001
    {"gi_state", gen_getstate, NULL,
1002
     PyDoc_STR("state of the generator")},
1003
    {NULL} /* Sentinel */
1004
};
1005
1006
static PyMemberDef gen_memberlist[] = {
1007
    {NULL}      /* Sentinel */
1008
};
1009
1010
static PyObject *
1011
gen_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored))
1012
0
{
1013
0
    PyGenObject *gen = _PyGen_CAST(op);
1014
0
    Py_ssize_t res;
1015
0
    res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus);
1016
0
    PyCodeObject *code = _PyGen_GetCode(gen);
1017
0
    res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *);
1018
0
    return PyLong_FromSsize_t(res);
1019
0
}
1020
1021
PyDoc_STRVAR(sizeof__doc__,
1022
"gen.__sizeof__() -> size of gen in memory, in bytes");
1023
1024
static PyMethodDef gen_methods[] = {
1025
    {"send", gen_send, METH_O, send_doc},
1026
    {"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc},
1027
    {"close", gen_close, METH_NOARGS, close_doc},
1028
    {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
1029
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
1030
     PyDoc_STR("generators are generic over the types of their yield, send, and return values")},
1031
    {NULL, NULL}        /* Sentinel */
1032
};
1033
1034
static PyAsyncMethods gen_as_async = {
1035
    0,                                          /* am_await */
1036
    0,                                          /* am_aiter */
1037
    0,                                          /* am_anext */
1038
    PyGen_am_send,                              /* am_send  */
1039
};
1040
1041
1042
PyTypeObject PyGen_Type = {
1043
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1044
    "generator",                                /* tp_name */
1045
    offsetof(PyGenObject, gi_iframe.localsplus), /* tp_basicsize */
1046
    sizeof(PyObject *),                         /* tp_itemsize */
1047
    /* methods */
1048
    gen_dealloc,                                /* tp_dealloc */
1049
    0,                                          /* tp_vectorcall_offset */
1050
    0,                                          /* tp_getattr */
1051
    0,                                          /* tp_setattr */
1052
    &gen_as_async,                              /* tp_as_async */
1053
    gen_repr,                                   /* tp_repr */
1054
    0,                                          /* tp_as_number */
1055
    0,                                          /* tp_as_sequence */
1056
    0,                                          /* tp_as_mapping */
1057
    0,                                          /* tp_hash */
1058
    0,                                          /* tp_call */
1059
    0,                                          /* tp_str */
1060
    PyObject_GenericGetAttr,                    /* tp_getattro */
1061
    0,                                          /* tp_setattro */
1062
    0,                                          /* tp_as_buffer */
1063
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1064
    0,                                          /* tp_doc */
1065
    gen_traverse,                               /* tp_traverse */
1066
    0,                                          /* tp_clear */
1067
    0,                                          /* tp_richcompare */
1068
    offsetof(PyGenObject, gi_weakreflist),      /* tp_weaklistoffset */
1069
    PyObject_SelfIter,                          /* tp_iter */
1070
    gen_iternext,                               /* tp_iternext */
1071
    gen_methods,                                /* tp_methods */
1072
    gen_memberlist,                             /* tp_members */
1073
    gen_getsetlist,                             /* tp_getset */
1074
    0,                                          /* tp_base */
1075
    0,                                          /* tp_dict */
1076
1077
    0,                                          /* tp_descr_get */
1078
    0,                                          /* tp_descr_set */
1079
    0,                                          /* tp_dictoffset */
1080
    0,                                          /* tp_init */
1081
    0,                                          /* tp_alloc */
1082
    0,                                          /* tp_new */
1083
    0,                                          /* tp_free */
1084
    0,                                          /* tp_is_gc */
1085
    0,                                          /* tp_bases */
1086
    0,                                          /* tp_mro */
1087
    0,                                          /* tp_cache */
1088
    0,                                          /* tp_subclasses */
1089
    0,                                          /* tp_weaklist */
1090
    0,                                          /* tp_del */
1091
    0,                                          /* tp_version_tag */
1092
    gen_finalize,                               /* tp_finalize */
1093
};
1094
1095
static PyObject *
1096
make_gen(PyTypeObject *type, PyFunctionObject *func)
1097
24.4M
{
1098
24.4M
    PyCodeObject *code = (PyCodeObject *)func->func_code;
1099
24.4M
    int slots = _PyFrame_NumSlotsForCodeObject(code);
1100
24.4M
    PyGenObject *gen = PyObject_GC_NewVar(PyGenObject, type, slots);
1101
24.4M
    if (gen == NULL) {
1102
0
        return NULL;
1103
0
    }
1104
24.4M
    gen->gi_frame_state = FRAME_CLEARED;
1105
24.4M
    gen->gi_weakreflist = NULL;
1106
24.4M
    gen->gi_exc_state.exc_value = NULL;
1107
24.4M
    gen->gi_exc_state.previous_item = NULL;
1108
24.4M
    gen->gi_iframe.f_executable = PyStackRef_None;
1109
24.4M
    assert(func->func_name != NULL);
1110
24.4M
    gen->gi_name = Py_NewRef(func->func_name);
1111
24.4M
    assert(func->func_qualname != NULL);
1112
24.4M
    gen->gi_qualname = Py_NewRef(func->func_qualname);
1113
24.4M
    _PyObject_GC_TRACK(gen);
1114
24.4M
    return (PyObject *)gen;
1115
24.4M
}
1116
1117
PyObject *
1118
_Py_MakeCoro(PyFunctionObject *func)
1119
24.4M
{
1120
24.4M
    int coro_flags = ((PyCodeObject *)func->func_code)->co_flags &
1121
24.4M
        (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR);
1122
24.4M
    assert(coro_flags);
1123
24.4M
    if (coro_flags == CO_GENERATOR) {
1124
24.4M
        return make_gen(&PyGen_Type, func);
1125
24.4M
    }
1126
72
    if (coro_flags == CO_ASYNC_GENERATOR) {
1127
36
        PyAsyncGenObject *ag;
1128
36
        ag = (PyAsyncGenObject *)make_gen(&PyAsyncGen_Type, func);
1129
36
        if (ag == NULL) {
1130
0
            return NULL;
1131
0
        }
1132
36
        ag->ag_origin_or_finalizer = NULL;
1133
36
        ag->ag_closed = 0;
1134
36
        ag->ag_hooks_inited = 0;
1135
36
        ag->ag_running_async = 0;
1136
36
        return (PyObject*)ag;
1137
36
    }
1138
1139
72
    assert (coro_flags == CO_COROUTINE);
1140
36
    PyObject *coro = make_gen(&PyCoro_Type, func);
1141
36
    if (!coro) {
1142
0
        return NULL;
1143
0
    }
1144
36
    PyThreadState *tstate = _PyThreadState_GET();
1145
36
    int origin_depth = tstate->coroutine_origin_tracking_depth;
1146
1147
36
    if (origin_depth == 0) {
1148
36
        ((PyCoroObject *)coro)->cr_origin_or_finalizer = NULL;
1149
36
    } else {
1150
0
        _PyInterpreterFrame *frame = tstate->current_frame;
1151
0
        assert(frame);
1152
0
        assert(_PyFrame_IsIncomplete(frame));
1153
0
        frame = _PyFrame_GetFirstComplete(frame->previous);
1154
0
        PyObject *cr_origin = _PyCoro_ComputeOrigin(origin_depth, frame);
1155
0
        ((PyCoroObject *)coro)->cr_origin_or_finalizer = cr_origin;
1156
0
        if (!cr_origin) {
1157
0
            Py_DECREF(coro);
1158
0
            return NULL;
1159
0
        }
1160
0
    }
1161
36
    return coro;
1162
36
}
1163
1164
static PyObject *
1165
gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
1166
                      PyObject *name, PyObject *qualname)
1167
0
{
1168
0
    PyCodeObject *code = _PyFrame_GetCode(f->f_frame);
1169
0
    int size = code->co_nlocalsplus + code->co_stacksize;
1170
0
    PyGenObject *gen = PyObject_GC_NewVar(PyGenObject, type, size);
1171
0
    if (gen == NULL) {
1172
0
        Py_DECREF(f);
1173
0
        return NULL;
1174
0
    }
1175
    /* Copy the frame */
1176
0
    assert(f->f_frame->frame_obj == NULL);
1177
0
    assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
1178
0
    _PyInterpreterFrame *frame = &gen->gi_iframe;
1179
0
    _PyFrame_Copy((_PyInterpreterFrame *)f->_f_frame_data, frame);
1180
0
    gen->gi_frame_state = FRAME_CREATED;
1181
0
    assert(frame->frame_obj == f);
1182
0
    f->f_frame = frame;
1183
0
    frame->owner = FRAME_OWNED_BY_GENERATOR;
1184
0
    assert(PyObject_GC_IsTracked((PyObject *)f));
1185
0
    Py_DECREF(f);
1186
0
    gen->gi_weakreflist = NULL;
1187
0
    gen->gi_exc_state.exc_value = NULL;
1188
0
    gen->gi_exc_state.previous_item = NULL;
1189
0
    if (name != NULL)
1190
0
        gen->gi_name = Py_NewRef(name);
1191
0
    else
1192
0
        gen->gi_name = Py_NewRef(_PyGen_GetCode(gen)->co_name);
1193
0
    if (qualname != NULL)
1194
0
        gen->gi_qualname = Py_NewRef(qualname);
1195
0
    else
1196
0
        gen->gi_qualname = Py_NewRef(_PyGen_GetCode(gen)->co_qualname);
1197
0
    _PyObject_GC_TRACK(gen);
1198
0
    return (PyObject *)gen;
1199
0
}
1200
1201
PyObject *
1202
PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
1203
0
{
1204
0
    return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
1205
0
}
1206
1207
PyObject *
1208
PyGen_New(PyFrameObject *f)
1209
0
{
1210
0
    return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
1211
0
}
1212
1213
/* Coroutine Object */
1214
1215
typedef struct {
1216
    PyObject_HEAD
1217
    PyCoroObject *cw_coroutine;
1218
} PyCoroWrapper;
1219
1220
#define _PyCoroWrapper_CAST(op) \
1221
0
    (assert(Py_IS_TYPE((op), &_PyCoroWrapper_Type)), \
1222
0
     _Py_CAST(PyCoroWrapper*, (op)))
1223
1224
1225
static int
1226
gen_is_coroutine(PyObject *o)
1227
0
{
1228
0
    if (PyGen_CheckExact(o)) {
1229
0
        PyCodeObject *code = _PyGen_GetCode((PyGenObject*)o);
1230
0
        if (code->co_flags & CO_ITERABLE_COROUTINE) {
1231
0
            return 1;
1232
0
        }
1233
0
    }
1234
0
    return 0;
1235
0
}
1236
1237
/*
1238
 *   This helper function returns an awaitable for `o`:
1239
 *     - `o` if `o` is a coroutine-object;
1240
 *     - `type(o)->tp_as_async->am_await(o)`
1241
 *
1242
 *   Raises a TypeError if it's not possible to return
1243
 *   an awaitable and returns NULL.
1244
 */
1245
PyObject *
1246
_PyCoro_GetAwaitableIter(PyObject *o)
1247
0
{
1248
0
    unaryfunc getter = NULL;
1249
0
    PyTypeObject *ot;
1250
1251
0
    if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
1252
        /* 'o' is a coroutine. */
1253
0
        return Py_NewRef(o);
1254
0
    }
1255
1256
0
    ot = Py_TYPE(o);
1257
0
    if (ot->tp_as_async != NULL) {
1258
0
        getter = ot->tp_as_async->am_await;
1259
0
    }
1260
0
    if (getter != NULL) {
1261
0
        PyObject *res = (*getter)(o);
1262
0
        if (res != NULL) {
1263
0
            if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
1264
                /* __await__ must return an *iterator*, not
1265
                   a coroutine or another awaitable (see PEP 492) */
1266
0
                PyErr_Format(PyExc_TypeError,
1267
0
                             "%T.__await__() must return an iterator, "
1268
0
                             "not coroutine", o);
1269
0
                Py_CLEAR(res);
1270
0
            } else if (!PyIter_Check(res)) {
1271
0
                PyErr_Format(PyExc_TypeError,
1272
0
                             "%T.__await__() must return an iterator, "
1273
0
                             "not %T", o, res);
1274
0
                Py_CLEAR(res);
1275
0
            }
1276
0
        }
1277
0
        return res;
1278
0
    }
1279
1280
0
    PyErr_Format(PyExc_TypeError,
1281
0
                 "'%.100s' object can't be awaited",
1282
0
                 ot->tp_name);
1283
0
    return NULL;
1284
0
}
1285
1286
static PyObject *
1287
coro_repr(PyObject *self)
1288
0
{
1289
0
    PyCoroObject *coro = _PyCoroObject_CAST(self);
1290
0
    return PyUnicode_FromFormat("<coroutine object %S at %p>",
1291
0
                                coro->cr_qualname, coro);
1292
0
}
1293
1294
static PyObject *
1295
coro_await(PyObject *coro)
1296
0
{
1297
0
    PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
1298
0
    if (cw == NULL) {
1299
0
        return NULL;
1300
0
    }
1301
0
    cw->cw_coroutine = (PyCoroObject*)Py_NewRef(coro);
1302
0
    _PyObject_GC_TRACK(cw);
1303
0
    return (PyObject *)cw;
1304
0
}
1305
1306
static PyObject *
1307
cr_getframe(PyObject *coro, void *Py_UNUSED(ignored))
1308
0
{
1309
0
    return _gen_getframe(_PyGen_CAST(coro), "cr_frame");
1310
0
}
1311
1312
static PyObject *
1313
cr_getcode(PyObject *coro, void *Py_UNUSED(ignored))
1314
0
{
1315
0
    return _gen_getcode(_PyGen_CAST(coro), "cr_code");
1316
0
}
1317
1318
static PyObject *
1319
cr_getstate(PyObject *self, void *Py_UNUSED(ignored))
1320
0
{
1321
0
    PyGenObject *gen = _PyGen_CAST(self);
1322
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
1323
1324
0
    static PyObject *const state_strings[] = {
1325
0
        [FRAME_CREATED] = &_Py_ID(CORO_CREATED),
1326
0
        [FRAME_SUSPENDED] = &_Py_ID(CORO_SUSPENDED),
1327
0
        [FRAME_SUSPENDED_YIELD_FROM] = &_Py_ID(CORO_SUSPENDED),
1328
0
        [FRAME_SUSPENDED_YIELD_FROM_LOCKED] = &_Py_ID(CORO_SUSPENDED),
1329
0
        [FRAME_EXECUTING] = &_Py_ID(CORO_RUNNING),
1330
0
        [FRAME_CLEARED] = &_Py_ID(CORO_CLOSED),
1331
0
    };
1332
1333
0
    assert(frame_state >= 0 &&
1334
0
           (size_t)frame_state < Py_ARRAY_LENGTH(state_strings));
1335
0
    return state_strings[frame_state];
1336
0
}
1337
1338
static PyGetSetDef coro_getsetlist[] = {
1339
    {"__name__", gen_get_name, gen_set_name,
1340
     PyDoc_STR("name of the coroutine")},
1341
    {"__qualname__", gen_get_qualname, gen_set_qualname,
1342
     PyDoc_STR("qualified name of the coroutine")},
1343
    {"cr_await", gen_getyieldfrom, NULL,
1344
     PyDoc_STR("object being awaited on, or None")},
1345
    {"cr_running", gen_getrunning, NULL, NULL},
1346
    {"cr_frame", cr_getframe, NULL, NULL},
1347
    {"cr_code", cr_getcode, NULL, NULL},
1348
    {"cr_suspended", gen_getsuspended, NULL, NULL},
1349
    {"cr_state", cr_getstate, NULL,
1350
     PyDoc_STR("state of the coroutine")},
1351
    {NULL} /* Sentinel */
1352
};
1353
1354
static PyMemberDef coro_memberlist[] = {
1355
    {"cr_origin",    _Py_T_OBJECT, offsetof(PyCoroObject, cr_origin_or_finalizer),   Py_READONLY},
1356
    {NULL}      /* Sentinel */
1357
};
1358
1359
PyDoc_STRVAR(coro_send_doc,
1360
"send(arg) -> send 'arg' into coroutine,\n\
1361
return next iterated value or raise StopIteration.");
1362
1363
PyDoc_STRVAR(coro_throw_doc,
1364
"throw(value)\n\
1365
throw(type[,value[,traceback]])\n\
1366
\n\
1367
Raise exception in coroutine, return next iterated value or raise\n\
1368
StopIteration.\n\
1369
the (type, val, tb) signature is deprecated, \n\
1370
and may be removed in a future version of Python.");
1371
1372
1373
PyDoc_STRVAR(coro_close_doc,
1374
"close() -> raise GeneratorExit inside coroutine.");
1375
1376
static PyMethodDef coro_methods[] = {
1377
    {"send", gen_send, METH_O, coro_send_doc},
1378
    {"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc},
1379
    {"close", gen_close, METH_NOARGS, coro_close_doc},
1380
    {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
1381
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
1382
     PyDoc_STR("coroutines are generic over the types of their yield, send, and return values")},
1383
    {NULL, NULL}        /* Sentinel */
1384
};
1385
1386
static PyAsyncMethods coro_as_async = {
1387
    coro_await,                                 /* am_await */
1388
    0,                                          /* am_aiter */
1389
    0,                                          /* am_anext */
1390
    PyGen_am_send,                              /* am_send  */
1391
};
1392
1393
PyTypeObject PyCoro_Type = {
1394
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1395
    "coroutine",                                /* tp_name */
1396
    offsetof(PyCoroObject, cr_iframe.localsplus),/* tp_basicsize */
1397
    sizeof(PyObject *),                         /* tp_itemsize */
1398
    /* methods */
1399
    gen_dealloc,                                /* tp_dealloc */
1400
    0,                                          /* tp_vectorcall_offset */
1401
    0,                                          /* tp_getattr */
1402
    0,                                          /* tp_setattr */
1403
    &coro_as_async,                             /* tp_as_async */
1404
    coro_repr,                                  /* tp_repr */
1405
    0,                                          /* tp_as_number */
1406
    0,                                          /* tp_as_sequence */
1407
    0,                                          /* tp_as_mapping */
1408
    0,                                          /* tp_hash */
1409
    0,                                          /* tp_call */
1410
    0,                                          /* tp_str */
1411
    PyObject_GenericGetAttr,                    /* tp_getattro */
1412
    0,                                          /* tp_setattro */
1413
    0,                                          /* tp_as_buffer */
1414
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1415
    0,                                          /* tp_doc */
1416
    gen_traverse,                               /* tp_traverse */
1417
    0,                                          /* tp_clear */
1418
    0,                                          /* tp_richcompare */
1419
    offsetof(PyCoroObject, cr_weakreflist),     /* tp_weaklistoffset */
1420
    0,                                          /* tp_iter */
1421
    0,                                          /* tp_iternext */
1422
    coro_methods,                               /* tp_methods */
1423
    coro_memberlist,                            /* tp_members */
1424
    coro_getsetlist,                            /* tp_getset */
1425
    0,                                          /* tp_base */
1426
    0,                                          /* tp_dict */
1427
    0,                                          /* tp_descr_get */
1428
    0,                                          /* tp_descr_set */
1429
    0,                                          /* tp_dictoffset */
1430
    0,                                          /* tp_init */
1431
    0,                                          /* tp_alloc */
1432
    0,                                          /* tp_new */
1433
    0,                                          /* tp_free */
1434
    0,                                          /* tp_is_gc */
1435
    0,                                          /* tp_bases */
1436
    0,                                          /* tp_mro */
1437
    0,                                          /* tp_cache */
1438
    0,                                          /* tp_subclasses */
1439
    0,                                          /* tp_weaklist */
1440
    0,                                          /* tp_del */
1441
    0,                                          /* tp_version_tag */
1442
    gen_finalize,                               /* tp_finalize */
1443
};
1444
1445
static void
1446
coro_wrapper_dealloc(PyObject *self)
1447
0
{
1448
0
    PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1449
0
    _PyObject_GC_UNTRACK((PyObject *)cw);
1450
0
    Py_CLEAR(cw->cw_coroutine);
1451
0
    PyObject_GC_Del(cw);
1452
0
}
1453
1454
static PyObject *
1455
coro_wrapper_iternext(PyObject *self)
1456
0
{
1457
0
    PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1458
0
    return gen_iternext((PyObject *)cw->cw_coroutine);
1459
0
}
1460
1461
static PyObject *
1462
coro_wrapper_send(PyObject *self, PyObject *arg)
1463
0
{
1464
0
    PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1465
0
    return gen_send((PyObject *)cw->cw_coroutine, arg);
1466
0
}
1467
1468
static PyObject *
1469
coro_wrapper_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1470
0
{
1471
0
    PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1472
0
    return gen_throw((PyObject*)cw->cw_coroutine, args, nargs);
1473
0
}
1474
1475
static PyObject *
1476
coro_wrapper_close(PyObject *self, PyObject *args)
1477
0
{
1478
0
    PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1479
0
    return gen_close((PyObject *)cw->cw_coroutine, args);
1480
0
}
1481
1482
static int
1483
coro_wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1484
0
{
1485
0
    PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1486
0
    Py_VISIT((PyObject *)cw->cw_coroutine);
1487
0
    return 0;
1488
0
}
1489
1490
static PyMethodDef coro_wrapper_methods[] = {
1491
    {"send", coro_wrapper_send, METH_O, coro_send_doc},
1492
    {"throw", _PyCFunction_CAST(coro_wrapper_throw), METH_FASTCALL,
1493
     coro_throw_doc},
1494
    {"close", coro_wrapper_close, METH_NOARGS, coro_close_doc},
1495
    {NULL, NULL}        /* Sentinel */
1496
};
1497
1498
PyTypeObject _PyCoroWrapper_Type = {
1499
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1500
    "coroutine_wrapper",
1501
    sizeof(PyCoroWrapper),                      /* tp_basicsize */
1502
    0,                                          /* tp_itemsize */
1503
    coro_wrapper_dealloc,                       /* destructor tp_dealloc */
1504
    0,                                          /* tp_vectorcall_offset */
1505
    0,                                          /* tp_getattr */
1506
    0,                                          /* tp_setattr */
1507
    0,                                          /* tp_as_async */
1508
    0,                                          /* tp_repr */
1509
    0,                                          /* tp_as_number */
1510
    0,                                          /* tp_as_sequence */
1511
    0,                                          /* tp_as_mapping */
1512
    0,                                          /* tp_hash */
1513
    0,                                          /* tp_call */
1514
    0,                                          /* tp_str */
1515
    PyObject_GenericGetAttr,                    /* tp_getattro */
1516
    0,                                          /* tp_setattro */
1517
    0,                                          /* tp_as_buffer */
1518
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1519
    "A wrapper object implementing __await__ for coroutines.",
1520
    coro_wrapper_traverse,                      /* tp_traverse */
1521
    0,                                          /* tp_clear */
1522
    0,                                          /* tp_richcompare */
1523
    0,                                          /* tp_weaklistoffset */
1524
    PyObject_SelfIter,                          /* tp_iter */
1525
    coro_wrapper_iternext,                      /* tp_iternext */
1526
    coro_wrapper_methods,                       /* tp_methods */
1527
    0,                                          /* tp_members */
1528
    0,                                          /* tp_getset */
1529
    0,                                          /* tp_base */
1530
    0,                                          /* tp_dict */
1531
    0,                                          /* tp_descr_get */
1532
    0,                                          /* tp_descr_set */
1533
    0,                                          /* tp_dictoffset */
1534
    0,                                          /* tp_init */
1535
    0,                                          /* tp_alloc */
1536
    0,                                          /* tp_new */
1537
    0,                                          /* tp_free */
1538
};
1539
1540
PyObject *
1541
_PyCoro_ComputeOrigin(int origin_depth, _PyInterpreterFrame *current_frame)
1542
0
{
1543
0
    _PyInterpreterFrame *frame = current_frame;
1544
    /* First count how many frames we have */
1545
0
    int frame_count = 0;
1546
0
    for (; frame && frame_count < origin_depth; ++frame_count) {
1547
0
        frame = _PyFrame_GetFirstComplete(frame->previous);
1548
0
    }
1549
1550
    /* Now collect them */
1551
0
    PyObject *cr_origin = PyTuple_New(frame_count);
1552
0
    if (cr_origin == NULL) {
1553
0
        return NULL;
1554
0
    }
1555
0
    frame = current_frame;
1556
0
    for (int i = 0; i < frame_count; ++i) {
1557
0
        PyCodeObject *code = _PyFrame_GetCode(frame);
1558
0
        int line = PyUnstable_InterpreterFrame_GetLine(frame);
1559
0
        PyObject *frameinfo = Py_BuildValue("OiO", code->co_filename, line,
1560
0
                                            code->co_name);
1561
0
        if (!frameinfo) {
1562
0
            Py_DECREF(cr_origin);
1563
0
            return NULL;
1564
0
        }
1565
0
        PyTuple_SET_ITEM(cr_origin, i, frameinfo);
1566
0
        frame = _PyFrame_GetFirstComplete(frame->previous);
1567
0
    }
1568
1569
0
    return cr_origin;
1570
0
}
1571
1572
PyObject *
1573
PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1574
0
{
1575
0
    PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
1576
0
    if (!coro) {
1577
0
        return NULL;
1578
0
    }
1579
1580
0
    PyThreadState *tstate = _PyThreadState_GET();
1581
0
    int origin_depth = tstate->coroutine_origin_tracking_depth;
1582
1583
0
    if (origin_depth == 0) {
1584
0
        ((PyCoroObject *)coro)->cr_origin_or_finalizer = NULL;
1585
0
    } else {
1586
0
        PyObject *cr_origin = _PyCoro_ComputeOrigin(origin_depth, _PyEval_GetFrame());
1587
0
        ((PyCoroObject *)coro)->cr_origin_or_finalizer = cr_origin;
1588
0
        if (!cr_origin) {
1589
0
            Py_DECREF(coro);
1590
0
            return NULL;
1591
0
        }
1592
0
    }
1593
1594
0
    return coro;
1595
0
}
1596
1597
1598
/* ========= Asynchronous Generators ========= */
1599
1600
1601
typedef enum {
1602
    AWAITABLE_STATE_INIT,   /* new awaitable, has not yet been iterated */
1603
    AWAITABLE_STATE_ITER,   /* being iterated */
1604
    AWAITABLE_STATE_CLOSED, /* closed */
1605
} AwaitableState;
1606
1607
1608
typedef struct PyAsyncGenASend {
1609
    PyObject_HEAD
1610
    PyAsyncGenObject *ags_gen;
1611
1612
    /* Can be NULL, when in the __anext__() mode
1613
       (equivalent of "asend(None)") */
1614
    PyObject *ags_sendval;
1615
1616
    AwaitableState ags_state;
1617
} PyAsyncGenASend;
1618
1619
#define _PyAsyncGenASend_CAST(op) \
1620
0
    _Py_CAST(PyAsyncGenASend*, (op))
1621
1622
1623
typedef struct PyAsyncGenAThrow {
1624
    PyObject_HEAD
1625
    PyAsyncGenObject *agt_gen;
1626
1627
    /* Can be NULL, when in the "aclose()" mode
1628
       (equivalent of "athrow(GeneratorExit)") */
1629
    PyObject *agt_typ;
1630
    PyObject *agt_tb;
1631
    PyObject *agt_val;
1632
1633
    AwaitableState agt_state;
1634
} PyAsyncGenAThrow;
1635
1636
1637
typedef struct _PyAsyncGenWrappedValue {
1638
    PyObject_HEAD
1639
    PyObject *agw_val;
1640
} _PyAsyncGenWrappedValue;
1641
1642
1643
#define _PyAsyncGenWrappedValue_CheckExact(o) \
1644
0
                    Py_IS_TYPE(o, &_PyAsyncGenWrappedValue_Type)
1645
#define _PyAsyncGenWrappedValue_CAST(op) \
1646
0
    (assert(_PyAsyncGenWrappedValue_CheckExact(op)), \
1647
0
     _Py_CAST(_PyAsyncGenWrappedValue*, (op)))
1648
1649
1650
static int
1651
async_gen_traverse(PyObject *self, visitproc visit, void *arg)
1652
0
{
1653
0
    PyAsyncGenObject *ag = _PyAsyncGenObject_CAST(self);
1654
0
    Py_VISIT(ag->ag_origin_or_finalizer);
1655
0
    return gen_traverse((PyObject*)ag, visit, arg);
1656
0
}
1657
1658
1659
static PyObject *
1660
async_gen_repr(PyObject *self)
1661
0
{
1662
0
    PyAsyncGenObject *o = _PyAsyncGenObject_CAST(self);
1663
0
    return PyUnicode_FromFormat("<async_generator object %S at %p>",
1664
0
                                o->ag_qualname, o);
1665
0
}
1666
1667
1668
static int
1669
async_gen_init_hooks(PyAsyncGenObject *o)
1670
0
{
1671
0
    PyThreadState *tstate;
1672
0
    PyObject *finalizer;
1673
0
    PyObject *firstiter;
1674
1675
0
    if (o->ag_hooks_inited) {
1676
0
        return 0;
1677
0
    }
1678
1679
0
    o->ag_hooks_inited = 1;
1680
1681
0
    tstate = _PyThreadState_GET();
1682
1683
0
    finalizer = tstate->async_gen_finalizer;
1684
0
    if (finalizer) {
1685
0
        o->ag_origin_or_finalizer = Py_NewRef(finalizer);
1686
0
    }
1687
1688
0
    firstiter = tstate->async_gen_firstiter;
1689
0
    if (firstiter) {
1690
0
        PyObject *res;
1691
1692
0
        Py_INCREF(firstiter);
1693
0
        res = PyObject_CallOneArg(firstiter, (PyObject *)o);
1694
0
        Py_DECREF(firstiter);
1695
0
        if (res == NULL) {
1696
0
            return 1;
1697
0
        }
1698
0
        Py_DECREF(res);
1699
0
    }
1700
1701
0
    return 0;
1702
0
}
1703
1704
1705
static PyObject *
1706
async_gen_anext(PyObject *self)
1707
0
{
1708
0
    PyAsyncGenObject *ag = _PyAsyncGenObject_CAST(self);
1709
0
    if (async_gen_init_hooks(ag)) {
1710
0
        return NULL;
1711
0
    }
1712
0
    return async_gen_asend_new(ag, NULL);
1713
0
}
1714
1715
1716
static PyObject *
1717
async_gen_asend(PyObject *op, PyObject *arg)
1718
0
{
1719
0
    PyAsyncGenObject *o = (PyAsyncGenObject*)op;
1720
0
    if (async_gen_init_hooks(o)) {
1721
0
        return NULL;
1722
0
    }
1723
0
    return async_gen_asend_new(o, arg);
1724
0
}
1725
1726
1727
static PyObject *
1728
async_gen_aclose(PyObject *op, PyObject *arg)
1729
0
{
1730
0
    PyAsyncGenObject *o = (PyAsyncGenObject*)op;
1731
0
    if (async_gen_init_hooks(o)) {
1732
0
        return NULL;
1733
0
    }
1734
0
    return async_gen_athrow_new(o, NULL);
1735
0
}
1736
1737
static PyObject *
1738
async_gen_athrow(PyObject *op, PyObject *args)
1739
0
{
1740
0
    PyAsyncGenObject *o = (PyAsyncGenObject*)op;
1741
0
    if (PyTuple_GET_SIZE(args) > 1) {
1742
0
        if (PyErr_WarnEx(PyExc_DeprecationWarning,
1743
0
                            "the (type, exc, tb) signature of athrow() is deprecated, "
1744
0
                            "use the single-arg signature instead.",
1745
0
                            1) < 0) {
1746
0
            return NULL;
1747
0
        }
1748
0
    }
1749
0
    if (async_gen_init_hooks(o)) {
1750
0
        return NULL;
1751
0
    }
1752
0
    return async_gen_athrow_new(o, args);
1753
0
}
1754
1755
static PyObject *
1756
ag_getframe(PyObject *ag, void *Py_UNUSED(ignored))
1757
0
{
1758
0
    return _gen_getframe((PyGenObject *)ag, "ag_frame");
1759
0
}
1760
1761
static PyObject *
1762
ag_getcode(PyObject *gen, void *Py_UNUSED(ignored))
1763
0
{
1764
0
    return _gen_getcode((PyGenObject*)gen, "ag_code");
1765
0
}
1766
1767
static PyObject *
1768
ag_getstate(PyObject *self, void *Py_UNUSED(ignored))
1769
0
{
1770
0
    PyGenObject *gen = _PyGen_CAST(self);
1771
0
    int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
1772
1773
0
    static PyObject *const state_strings[] = {
1774
0
        [FRAME_CREATED] = &_Py_ID(AGEN_CREATED),
1775
0
        [FRAME_SUSPENDED] = &_Py_ID(AGEN_SUSPENDED),
1776
0
        [FRAME_SUSPENDED_YIELD_FROM] = &_Py_ID(AGEN_SUSPENDED),
1777
0
        [FRAME_SUSPENDED_YIELD_FROM_LOCKED] = &_Py_ID(AGEN_SUSPENDED),
1778
0
        [FRAME_EXECUTING] = &_Py_ID(AGEN_RUNNING),
1779
0
        [FRAME_CLEARED] = &_Py_ID(AGEN_CLOSED),
1780
0
    };
1781
1782
0
    assert(frame_state >= 0 &&
1783
0
           (size_t)frame_state < Py_ARRAY_LENGTH(state_strings));
1784
0
    return state_strings[frame_state];
1785
0
}
1786
1787
static PyGetSetDef async_gen_getsetlist[] = {
1788
    {"__name__", gen_get_name, gen_set_name,
1789
     PyDoc_STR("name of the async generator")},
1790
    {"__qualname__", gen_get_qualname, gen_set_qualname,
1791
     PyDoc_STR("qualified name of the async generator")},
1792
    {"ag_await", gen_getyieldfrom, NULL,
1793
     PyDoc_STR("object being awaited on, or None")},
1794
     {"ag_frame", ag_getframe, NULL, NULL},
1795
     {"ag_code", ag_getcode, NULL, NULL},
1796
     {"ag_suspended", gen_getsuspended, NULL, NULL},
1797
     {"ag_state", ag_getstate, NULL,
1798
      PyDoc_STR("state of the async generator")},
1799
    {NULL} /* Sentinel */
1800
};
1801
1802
static PyMemberDef async_gen_memberlist[] = {
1803
    {"ag_running", Py_T_BOOL,   offsetof(PyAsyncGenObject, ag_running_async),
1804
        Py_READONLY},
1805
    {NULL}      /* Sentinel */
1806
};
1807
1808
PyDoc_STRVAR(async_aclose_doc,
1809
"aclose() -> raise GeneratorExit inside generator.");
1810
1811
PyDoc_STRVAR(async_asend_doc,
1812
"asend(v) -> send 'v' in generator.");
1813
1814
PyDoc_STRVAR(async_athrow_doc,
1815
"athrow(value)\n\
1816
athrow(type[,value[,tb]])\n\
1817
\n\
1818
raise exception in generator.\n\
1819
the (type, val, tb) signature is deprecated, \n\
1820
and may be removed in a future version of Python.");
1821
1822
static PyMethodDef async_gen_methods[] = {
1823
    {"asend", async_gen_asend, METH_O, async_asend_doc},
1824
    {"athrow", async_gen_athrow, METH_VARARGS, async_athrow_doc},
1825
    {"aclose", async_gen_aclose, METH_NOARGS, async_aclose_doc},
1826
    {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
1827
    {"__class_getitem__",    Py_GenericAlias,
1828
    METH_O|METH_CLASS,       PyDoc_STR("async generators are generic over the types of their yield and send values")},
1829
    {NULL, NULL}        /* Sentinel */
1830
};
1831
1832
1833
static PyAsyncMethods async_gen_as_async = {
1834
    0,                                          /* am_await */
1835
    PyObject_SelfIter,                          /* am_aiter */
1836
    async_gen_anext,                            /* am_anext */
1837
    PyGen_am_send,                              /* am_send  */
1838
};
1839
1840
1841
PyTypeObject PyAsyncGen_Type = {
1842
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1843
    "async_generator",                          /* tp_name */
1844
    offsetof(PyAsyncGenObject, ag_iframe.localsplus), /* tp_basicsize */
1845
    sizeof(PyObject *),                         /* tp_itemsize */
1846
    /* methods */
1847
    gen_dealloc,                                /* tp_dealloc */
1848
    0,                                          /* tp_vectorcall_offset */
1849
    0,                                          /* tp_getattr */
1850
    0,                                          /* tp_setattr */
1851
    &async_gen_as_async,                        /* tp_as_async */
1852
    async_gen_repr,                             /* tp_repr */
1853
    0,                                          /* tp_as_number */
1854
    0,                                          /* tp_as_sequence */
1855
    0,                                          /* tp_as_mapping */
1856
    0,                                          /* tp_hash */
1857
    0,                                          /* tp_call */
1858
    0,                                          /* tp_str */
1859
    PyObject_GenericGetAttr,                    /* tp_getattro */
1860
    0,                                          /* tp_setattro */
1861
    0,                                          /* tp_as_buffer */
1862
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1863
    0,                                          /* tp_doc */
1864
    async_gen_traverse,                         /* tp_traverse */
1865
    0,                                          /* tp_clear */
1866
    0,                                          /* tp_richcompare */
1867
    offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */
1868
    0,                                          /* tp_iter */
1869
    0,                                          /* tp_iternext */
1870
    async_gen_methods,                          /* tp_methods */
1871
    async_gen_memberlist,                       /* tp_members */
1872
    async_gen_getsetlist,                       /* tp_getset */
1873
    0,                                          /* tp_base */
1874
    0,                                          /* tp_dict */
1875
    0,                                          /* tp_descr_get */
1876
    0,                                          /* tp_descr_set */
1877
    0,                                          /* tp_dictoffset */
1878
    0,                                          /* tp_init */
1879
    0,                                          /* tp_alloc */
1880
    0,                                          /* tp_new */
1881
    0,                                          /* tp_free */
1882
    0,                                          /* tp_is_gc */
1883
    0,                                          /* tp_bases */
1884
    0,                                          /* tp_mro */
1885
    0,                                          /* tp_cache */
1886
    0,                                          /* tp_subclasses */
1887
    0,                                          /* tp_weaklist */
1888
    0,                                          /* tp_del */
1889
    0,                                          /* tp_version_tag */
1890
    gen_finalize,                               /* tp_finalize */
1891
};
1892
1893
1894
PyObject *
1895
PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
1896
0
{
1897
0
    PyAsyncGenObject *ag;
1898
0
    ag = (PyAsyncGenObject *)gen_new_with_qualname(&PyAsyncGen_Type, f,
1899
0
                                                   name, qualname);
1900
0
    if (ag == NULL) {
1901
0
        return NULL;
1902
0
    }
1903
1904
0
    ag->ag_origin_or_finalizer = NULL;
1905
0
    ag->ag_closed = 0;
1906
0
    ag->ag_hooks_inited = 0;
1907
0
    ag->ag_running_async = 0;
1908
0
    return (PyObject*)ag;
1909
0
}
1910
1911
static PyObject *
1912
async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
1913
0
{
1914
0
    if (result == NULL) {
1915
0
        if (!PyErr_Occurred()) {
1916
0
            PyErr_SetNone(PyExc_StopAsyncIteration);
1917
0
        }
1918
1919
0
        if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)
1920
0
            || PyErr_ExceptionMatches(PyExc_GeneratorExit)
1921
0
        ) {
1922
0
            gen->ag_closed = 1;
1923
0
        }
1924
1925
0
        gen->ag_running_async = 0;
1926
0
        return NULL;
1927
0
    }
1928
1929
0
    if (_PyAsyncGenWrappedValue_CheckExact(result)) {
1930
        /* async yield */
1931
0
        _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val);
1932
0
        Py_DECREF(result);
1933
0
        gen->ag_running_async = 0;
1934
0
        return NULL;
1935
0
    }
1936
1937
0
    return result;
1938
0
}
1939
1940
1941
/* ---------- Async Generator ASend Awaitable ------------ */
1942
1943
1944
static void
1945
async_gen_asend_dealloc(PyObject *self)
1946
0
{
1947
0
    assert(PyAsyncGenASend_CheckExact(self));
1948
0
    PyAsyncGenASend *ags = _PyAsyncGenASend_CAST(self);
1949
1950
0
    if (PyObject_CallFinalizerFromDealloc(self)) {
1951
0
        return;
1952
0
    }
1953
1954
0
    _PyObject_GC_UNTRACK(self);
1955
0
    Py_CLEAR(ags->ags_gen);
1956
0
    Py_CLEAR(ags->ags_sendval);
1957
1958
0
    _PyGC_CLEAR_FINALIZED(self);
1959
1960
0
    _Py_FREELIST_FREE(async_gen_asends, self, PyObject_GC_Del);
1961
0
}
1962
1963
static int
1964
async_gen_asend_traverse(PyObject *self, visitproc visit, void *arg)
1965
0
{
1966
0
    PyAsyncGenASend *ags = _PyAsyncGenASend_CAST(self);
1967
0
    Py_VISIT(ags->ags_gen);
1968
0
    Py_VISIT(ags->ags_sendval);
1969
0
    return 0;
1970
0
}
1971
1972
1973
static PyObject *
1974
async_gen_asend_send(PyObject *self, PyObject *arg)
1975
0
{
1976
0
    PyAsyncGenASend *o = _PyAsyncGenASend_CAST(self);
1977
0
    if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1978
0
        PyErr_SetString(
1979
0
            PyExc_RuntimeError,
1980
0
            "cannot reuse already awaited __anext__()/asend()");
1981
0
        return NULL;
1982
0
    }
1983
1984
0
    if (o->ags_state == AWAITABLE_STATE_INIT) {
1985
0
        if (o->ags_gen->ag_running_async) {
1986
0
            o->ags_state = AWAITABLE_STATE_CLOSED;
1987
0
            PyErr_SetString(
1988
0
                PyExc_RuntimeError,
1989
0
                "anext(): asynchronous generator is already running");
1990
0
            return NULL;
1991
0
        }
1992
1993
0
        if (arg == NULL || arg == Py_None) {
1994
0
            arg = o->ags_sendval;
1995
0
        }
1996
0
        o->ags_state = AWAITABLE_STATE_ITER;
1997
0
    }
1998
1999
0
    o->ags_gen->ag_running_async = 1;
2000
0
    PyObject *result = gen_send((PyObject*)o->ags_gen, arg);
2001
0
    result = async_gen_unwrap_value(o->ags_gen, result);
2002
2003
0
    if (result == NULL) {
2004
0
        o->ags_state = AWAITABLE_STATE_CLOSED;
2005
0
    }
2006
2007
0
    return result;
2008
0
}
2009
2010
PySendResult
2011
_PyAsyncGenASend_Send(PyObject *iter, PyObject *arg, PyObject **result)
2012
0
{
2013
0
    *result = async_gen_asend_send(iter, arg);
2014
0
    if (*result != NULL) {
2015
0
        return PYGEN_NEXT;
2016
0
    }
2017
0
    if (_PyGen_FetchStopIterationValue(result) == 0) {
2018
0
        return PYGEN_RETURN;
2019
0
    }
2020
0
    return PYGEN_ERROR;
2021
0
}
2022
2023
2024
static PyObject *
2025
async_gen_asend_iternext(PyObject *ags)
2026
0
{
2027
0
    return async_gen_asend_send(ags, NULL);
2028
0
}
2029
2030
2031
static PyObject *
2032
async_gen_asend_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2033
0
{
2034
0
    PyAsyncGenASend *o = _PyAsyncGenASend_CAST(self);
2035
2036
0
    if (o->ags_state == AWAITABLE_STATE_CLOSED) {
2037
0
        PyErr_SetString(
2038
0
            PyExc_RuntimeError,
2039
0
            "cannot reuse already awaited __anext__()/asend()");
2040
0
        return NULL;
2041
0
    }
2042
2043
0
    if (o->ags_state == AWAITABLE_STATE_INIT) {
2044
0
        if (o->ags_gen->ag_running_async) {
2045
0
            o->ags_state = AWAITABLE_STATE_CLOSED;
2046
0
            PyErr_SetString(
2047
0
                PyExc_RuntimeError,
2048
0
                "anext(): asynchronous generator is already running");
2049
0
            return NULL;
2050
0
        }
2051
2052
0
        o->ags_state = AWAITABLE_STATE_ITER;
2053
0
        o->ags_gen->ag_running_async = 1;
2054
0
    }
2055
2056
0
    PyObject *result = gen_throw((PyObject*)o->ags_gen, args, nargs);
2057
0
    result = async_gen_unwrap_value(o->ags_gen, result);
2058
2059
0
    if (result == NULL) {
2060
0
        o->ags_gen->ag_running_async = 0;
2061
0
        o->ags_state = AWAITABLE_STATE_CLOSED;
2062
0
    }
2063
2064
0
    return result;
2065
0
}
2066
2067
2068
static PyObject *
2069
async_gen_asend_close(PyObject *self, PyObject *args)
2070
0
{
2071
0
    PyAsyncGenASend *o = _PyAsyncGenASend_CAST(self);
2072
0
    if (o->ags_state == AWAITABLE_STATE_CLOSED) {
2073
0
        Py_RETURN_NONE;
2074
0
    }
2075
2076
0
    PyObject *result = async_gen_asend_throw(self, &PyExc_GeneratorExit, 1);
2077
0
    if (result == NULL) {
2078
0
        if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
2079
0
            PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
2080
0
            PyErr_ExceptionMatches(PyExc_GeneratorExit))
2081
0
        {
2082
0
            PyErr_Clear();
2083
0
            Py_RETURN_NONE;
2084
0
        }
2085
0
        return result;
2086
0
    }
2087
2088
0
    Py_DECREF(result);
2089
0
    PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
2090
0
    return NULL;
2091
0
}
2092
2093
static void
2094
async_gen_asend_finalize(PyObject *self)
2095
0
{
2096
0
    PyAsyncGenASend *ags = _PyAsyncGenASend_CAST(self);
2097
0
    if (ags->ags_state == AWAITABLE_STATE_INIT) {
2098
0
        _PyErr_WarnUnawaitedAgenMethod(ags->ags_gen, &_Py_ID(asend));
2099
0
    }
2100
0
}
2101
2102
static PyMethodDef async_gen_asend_methods[] = {
2103
    {"send", async_gen_asend_send, METH_O, send_doc},
2104
    {"throw", _PyCFunction_CAST(async_gen_asend_throw), METH_FASTCALL, throw_doc},
2105
    {"close", async_gen_asend_close, METH_NOARGS, close_doc},
2106
    {NULL, NULL}        /* Sentinel */
2107
};
2108
2109
2110
static PyAsyncMethods async_gen_asend_as_async = {
2111
    .am_await = PyObject_SelfIter,
2112
    .am_send = _PyAsyncGenASend_Send,
2113
};
2114
2115
2116
PyTypeObject _PyAsyncGenASend_Type = {
2117
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2118
    "async_generator_asend",                    /* tp_name */
2119
    sizeof(PyAsyncGenASend),                    /* tp_basicsize */
2120
    0,                                          /* tp_itemsize */
2121
    /* methods */
2122
    async_gen_asend_dealloc,                    /* tp_dealloc */
2123
    0,                                          /* tp_vectorcall_offset */
2124
    0,                                          /* tp_getattr */
2125
    0,                                          /* tp_setattr */
2126
    &async_gen_asend_as_async,                  /* tp_as_async */
2127
    0,                                          /* tp_repr */
2128
    0,                                          /* tp_as_number */
2129
    0,                                          /* tp_as_sequence */
2130
    0,                                          /* tp_as_mapping */
2131
    0,                                          /* tp_hash */
2132
    0,                                          /* tp_call */
2133
    0,                                          /* tp_str */
2134
    PyObject_GenericGetAttr,                    /* tp_getattro */
2135
    0,                                          /* tp_setattro */
2136
    0,                                          /* tp_as_buffer */
2137
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
2138
    0,                                          /* tp_doc */
2139
    async_gen_asend_traverse,                   /* tp_traverse */
2140
    0,                                          /* tp_clear */
2141
    0,                                          /* tp_richcompare */
2142
    0,                                          /* tp_weaklistoffset */
2143
    PyObject_SelfIter,                          /* tp_iter */
2144
    async_gen_asend_iternext,                   /* tp_iternext */
2145
    async_gen_asend_methods,                    /* tp_methods */
2146
    0,                                          /* tp_members */
2147
    0,                                          /* tp_getset */
2148
    0,                                          /* tp_base */
2149
    0,                                          /* tp_dict */
2150
    0,                                          /* tp_descr_get */
2151
    0,                                          /* tp_descr_set */
2152
    0,                                          /* tp_dictoffset */
2153
    0,                                          /* tp_init */
2154
    0,                                          /* tp_alloc */
2155
    0,                                          /* tp_new */
2156
    .tp_finalize = async_gen_asend_finalize,
2157
};
2158
2159
2160
static PyObject *
2161
async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
2162
0
{
2163
0
    PyAsyncGenASend *ags = _Py_FREELIST_POP(PyAsyncGenASend, async_gen_asends);
2164
0
    if (ags == NULL) {
2165
0
        ags = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type);
2166
0
        if (ags == NULL) {
2167
0
            return NULL;
2168
0
        }
2169
0
    }
2170
2171
0
    ags->ags_gen = (PyAsyncGenObject*)Py_NewRef(gen);
2172
0
    ags->ags_sendval = Py_XNewRef(sendval);
2173
0
    ags->ags_state = AWAITABLE_STATE_INIT;
2174
2175
0
    _PyObject_GC_TRACK((PyObject*)ags);
2176
0
    return (PyObject*)ags;
2177
0
}
2178
2179
2180
/* ---------- Async Generator Value Wrapper ------------ */
2181
2182
2183
static void
2184
async_gen_wrapped_val_dealloc(PyObject *self)
2185
0
{
2186
0
    _PyAsyncGenWrappedValue *agw = _PyAsyncGenWrappedValue_CAST(self);
2187
0
    _PyObject_GC_UNTRACK(self);
2188
0
    Py_CLEAR(agw->agw_val);
2189
0
    _Py_FREELIST_FREE(async_gens, self, PyObject_GC_Del);
2190
0
}
2191
2192
2193
static int
2194
async_gen_wrapped_val_traverse(PyObject *self, visitproc visit, void *arg)
2195
0
{
2196
0
    _PyAsyncGenWrappedValue *agw = _PyAsyncGenWrappedValue_CAST(self);
2197
0
    Py_VISIT(agw->agw_val);
2198
0
    return 0;
2199
0
}
2200
2201
2202
PyTypeObject _PyAsyncGenWrappedValue_Type = {
2203
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2204
    "async_generator_wrapped_value",            /* tp_name */
2205
    sizeof(_PyAsyncGenWrappedValue),            /* tp_basicsize */
2206
    0,                                          /* tp_itemsize */
2207
    /* methods */
2208
    async_gen_wrapped_val_dealloc,              /* tp_dealloc */
2209
    0,                                          /* tp_vectorcall_offset */
2210
    0,                                          /* tp_getattr */
2211
    0,                                          /* tp_setattr */
2212
    0,                                          /* tp_as_async */
2213
    0,                                          /* tp_repr */
2214
    0,                                          /* tp_as_number */
2215
    0,                                          /* tp_as_sequence */
2216
    0,                                          /* tp_as_mapping */
2217
    0,                                          /* tp_hash */
2218
    0,                                          /* tp_call */
2219
    0,                                          /* tp_str */
2220
    PyObject_GenericGetAttr,                    /* tp_getattro */
2221
    0,                                          /* tp_setattro */
2222
    0,                                          /* tp_as_buffer */
2223
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
2224
    0,                                          /* tp_doc */
2225
    async_gen_wrapped_val_traverse,             /* tp_traverse */
2226
    0,                                          /* tp_clear */
2227
    0,                                          /* tp_richcompare */
2228
    0,                                          /* tp_weaklistoffset */
2229
    0,                                          /* tp_iter */
2230
    0,                                          /* tp_iternext */
2231
    0,                                          /* tp_methods */
2232
    0,                                          /* tp_members */
2233
    0,                                          /* tp_getset */
2234
    0,                                          /* tp_base */
2235
    0,                                          /* tp_dict */
2236
    0,                                          /* tp_descr_get */
2237
    0,                                          /* tp_descr_set */
2238
    0,                                          /* tp_dictoffset */
2239
    0,                                          /* tp_init */
2240
    0,                                          /* tp_alloc */
2241
    0,                                          /* tp_new */
2242
};
2243
2244
2245
PyObject *
2246
_PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val)
2247
0
{
2248
0
    assert(val);
2249
2250
0
    _PyAsyncGenWrappedValue *o = _Py_FREELIST_POP(_PyAsyncGenWrappedValue, async_gens);
2251
0
    if (o == NULL) {
2252
0
        o = PyObject_GC_New(_PyAsyncGenWrappedValue,
2253
0
                            &_PyAsyncGenWrappedValue_Type);
2254
0
        if (o == NULL) {
2255
0
            return NULL;
2256
0
        }
2257
0
    }
2258
0
    assert(_PyAsyncGenWrappedValue_CheckExact(o));
2259
0
    o->agw_val = Py_NewRef(val);
2260
0
    _PyObject_GC_TRACK((PyObject*)o);
2261
0
    return (PyObject*)o;
2262
0
}
2263
2264
2265
/* ---------- Async Generator AThrow awaitable ------------ */
2266
2267
#define _PyAsyncGenAThrow_CAST(op) \
2268
0
    (assert(Py_IS_TYPE((op), &_PyAsyncGenAThrow_Type)), \
2269
0
     _Py_CAST(PyAsyncGenAThrow*, (op)))
2270
2271
static void
2272
async_gen_athrow_dealloc(PyObject *self)
2273
0
{
2274
0
    PyAsyncGenAThrow *agt = _PyAsyncGenAThrow_CAST(self);
2275
0
    if (PyObject_CallFinalizerFromDealloc(self)) {
2276
0
        return;
2277
0
    }
2278
2279
0
    _PyObject_GC_UNTRACK(self);
2280
0
    Py_CLEAR(agt->agt_gen);
2281
0
    Py_XDECREF(agt->agt_typ);
2282
0
    Py_XDECREF(agt->agt_tb);
2283
0
    Py_XDECREF(agt->agt_val);
2284
0
    PyObject_GC_Del(self);
2285
0
}
2286
2287
2288
static int
2289
async_gen_athrow_traverse(PyObject *self, visitproc visit, void *arg)
2290
0
{
2291
0
    PyAsyncGenAThrow *agt = _PyAsyncGenAThrow_CAST(self);
2292
0
    Py_VISIT(agt->agt_gen);
2293
0
    Py_VISIT(agt->agt_typ);
2294
0
    Py_VISIT(agt->agt_tb);
2295
0
    Py_VISIT(agt->agt_val);
2296
0
    return 0;
2297
0
}
2298
2299
2300
static PyObject *
2301
async_gen_athrow_send(PyObject *self, PyObject *arg)
2302
0
{
2303
0
    PyAsyncGenAThrow *o = _PyAsyncGenAThrow_CAST(self);
2304
0
    PyGenObject *gen = _PyGen_CAST(o->agt_gen);
2305
0
    PyObject *retval;
2306
2307
0
    if (o->agt_state == AWAITABLE_STATE_CLOSED) {
2308
0
        PyErr_SetString(
2309
0
            PyExc_RuntimeError,
2310
0
            "cannot reuse already awaited aclose()/athrow()");
2311
0
        return NULL;
2312
0
    }
2313
2314
0
    if (FRAME_STATE_FINISHED(gen->gi_frame_state)) {
2315
0
        o->agt_state = AWAITABLE_STATE_CLOSED;
2316
0
        PyErr_SetNone(PyExc_StopIteration);
2317
0
        return NULL;
2318
0
    }
2319
2320
0
    if (o->agt_state == AWAITABLE_STATE_INIT) {
2321
0
        if (o->agt_gen->ag_running_async) {
2322
0
            o->agt_state = AWAITABLE_STATE_CLOSED;
2323
0
            if (o->agt_typ == NULL) {
2324
0
                PyErr_SetString(
2325
0
                    PyExc_RuntimeError,
2326
0
                    "aclose(): asynchronous generator is already running");
2327
0
            }
2328
0
            else {
2329
0
                PyErr_SetString(
2330
0
                    PyExc_RuntimeError,
2331
0
                    "athrow(): asynchronous generator is already running");
2332
0
            }
2333
0
            return NULL;
2334
0
        }
2335
2336
0
        if (o->agt_gen->ag_closed) {
2337
0
            o->agt_state = AWAITABLE_STATE_CLOSED;
2338
0
            PyErr_SetNone(PyExc_StopAsyncIteration);
2339
0
            return NULL;
2340
0
        }
2341
2342
0
        if (arg != Py_None) {
2343
0
            PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
2344
0
            return NULL;
2345
0
        }
2346
2347
0
        o->agt_state = AWAITABLE_STATE_ITER;
2348
0
        o->agt_gen->ag_running_async = 1;
2349
2350
0
        if (o->agt_typ == NULL) {
2351
            /* aclose() mode */
2352
0
            o->agt_gen->ag_closed = 1;
2353
2354
0
            retval = _gen_throw((PyGenObject *)gen,
2355
0
                                0,  /* Do not close generator when
2356
                                       PyExc_GeneratorExit is passed */
2357
0
                                PyExc_GeneratorExit, NULL, NULL);
2358
2359
0
            if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
2360
0
                Py_DECREF(retval);
2361
0
                goto yield_close;
2362
0
            }
2363
0
        } else {
2364
0
            retval = _gen_throw((PyGenObject *)gen,
2365
0
                                0,  /* Do not close generator when
2366
                                       PyExc_GeneratorExit is passed */
2367
0
                                o->agt_typ, o->agt_val, o->agt_tb);
2368
0
            retval = async_gen_unwrap_value(o->agt_gen, retval);
2369
0
        }
2370
0
        if (retval == NULL) {
2371
0
            goto check_error;
2372
0
        }
2373
0
        return retval;
2374
0
    }
2375
2376
0
    assert(o->agt_state == AWAITABLE_STATE_ITER);
2377
2378
0
    retval = gen_send((PyObject *)gen, arg);
2379
0
    if (o->agt_typ) {
2380
0
        return async_gen_unwrap_value(o->agt_gen, retval);
2381
0
    } else {
2382
        /* aclose() mode */
2383
0
        if (retval) {
2384
0
            if (_PyAsyncGenWrappedValue_CheckExact(retval)) {
2385
0
                Py_DECREF(retval);
2386
0
                goto yield_close;
2387
0
            }
2388
0
            else {
2389
0
                return retval;
2390
0
            }
2391
0
        }
2392
0
        else {
2393
0
            goto check_error;
2394
0
        }
2395
0
    }
2396
2397
0
yield_close:
2398
0
    o->agt_gen->ag_running_async = 0;
2399
0
    o->agt_state = AWAITABLE_STATE_CLOSED;
2400
0
    PyErr_SetString(
2401
0
        PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
2402
0
    return NULL;
2403
2404
0
check_error:
2405
0
    o->agt_gen->ag_running_async = 0;
2406
0
    o->agt_state = AWAITABLE_STATE_CLOSED;
2407
0
    if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
2408
0
            PyErr_ExceptionMatches(PyExc_GeneratorExit))
2409
0
    {
2410
0
        if (o->agt_typ == NULL) {
2411
            /* when aclose() is called we don't want to propagate
2412
               StopAsyncIteration or GeneratorExit; just raise
2413
               StopIteration, signalling that this 'aclose()' await
2414
               is done.
2415
            */
2416
0
            PyErr_Clear();
2417
0
            PyErr_SetNone(PyExc_StopIteration);
2418
0
        }
2419
0
    }
2420
0
    return NULL;
2421
0
}
2422
2423
2424
static PyObject *
2425
async_gen_athrow_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2426
0
{
2427
0
    PyAsyncGenAThrow *o = _PyAsyncGenAThrow_CAST(self);
2428
2429
0
    if (o->agt_state == AWAITABLE_STATE_CLOSED) {
2430
0
        PyErr_SetString(
2431
0
            PyExc_RuntimeError,
2432
0
            "cannot reuse already awaited aclose()/athrow()");
2433
0
        return NULL;
2434
0
    }
2435
2436
0
    if (o->agt_state == AWAITABLE_STATE_INIT) {
2437
0
        if (o->agt_gen->ag_running_async) {
2438
0
            o->agt_state = AWAITABLE_STATE_CLOSED;
2439
0
            if (o->agt_typ == NULL) {
2440
0
                PyErr_SetString(
2441
0
                    PyExc_RuntimeError,
2442
0
                    "aclose(): asynchronous generator is already running");
2443
0
            }
2444
0
            else {
2445
0
                PyErr_SetString(
2446
0
                    PyExc_RuntimeError,
2447
0
                    "athrow(): asynchronous generator is already running");
2448
0
            }
2449
0
            return NULL;
2450
0
        }
2451
2452
0
        o->agt_state = AWAITABLE_STATE_ITER;
2453
0
        o->agt_gen->ag_running_async = 1;
2454
0
    }
2455
2456
0
    PyObject *retval = gen_throw((PyObject*)o->agt_gen, args, nargs);
2457
0
    if (o->agt_typ) {
2458
0
        retval = async_gen_unwrap_value(o->agt_gen, retval);
2459
0
        if (retval == NULL) {
2460
0
            o->agt_gen->ag_running_async = 0;
2461
0
            o->agt_state = AWAITABLE_STATE_CLOSED;
2462
0
        }
2463
0
        return retval;
2464
0
    }
2465
0
    else {
2466
        /* aclose() mode */
2467
0
        if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
2468
0
            o->agt_gen->ag_running_async = 0;
2469
0
            o->agt_state = AWAITABLE_STATE_CLOSED;
2470
0
            Py_DECREF(retval);
2471
0
            PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
2472
0
            return NULL;
2473
0
        }
2474
0
        if (retval == NULL) {
2475
0
            o->agt_gen->ag_running_async = 0;
2476
0
            o->agt_state = AWAITABLE_STATE_CLOSED;
2477
0
        }
2478
0
        if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
2479
0
            PyErr_ExceptionMatches(PyExc_GeneratorExit))
2480
0
        {
2481
            /* when aclose() is called we don't want to propagate
2482
               StopAsyncIteration or GeneratorExit; just raise
2483
               StopIteration, signalling that this 'aclose()' await
2484
               is done.
2485
            */
2486
0
            PyErr_Clear();
2487
0
            PyErr_SetNone(PyExc_StopIteration);
2488
0
        }
2489
0
        return retval;
2490
0
    }
2491
0
}
2492
2493
2494
static PyObject *
2495
async_gen_athrow_iternext(PyObject *agt)
2496
0
{
2497
0
    return async_gen_athrow_send(agt, Py_None);
2498
0
}
2499
2500
2501
static PyObject *
2502
async_gen_athrow_close(PyObject *self, PyObject *args)
2503
0
{
2504
0
    PyAsyncGenAThrow *agt = _PyAsyncGenAThrow_CAST(self);
2505
0
    if (agt->agt_state == AWAITABLE_STATE_CLOSED) {
2506
0
        Py_RETURN_NONE;
2507
0
    }
2508
0
    PyObject *result = async_gen_athrow_throw((PyObject*)agt,
2509
0
                                              &PyExc_GeneratorExit, 1);
2510
0
    if (result == NULL) {
2511
0
        if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
2512
0
            PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
2513
0
            PyErr_ExceptionMatches(PyExc_GeneratorExit))
2514
0
        {
2515
0
            PyErr_Clear();
2516
0
            Py_RETURN_NONE;
2517
0
        }
2518
0
        return result;
2519
0
    } else {
2520
0
        Py_DECREF(result);
2521
0
        PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
2522
0
        return NULL;
2523
0
    }
2524
0
}
2525
2526
2527
static void
2528
async_gen_athrow_finalize(PyObject *op)
2529
0
{
2530
0
    PyAsyncGenAThrow *o = (PyAsyncGenAThrow*)op;
2531
0
    if (o->agt_state == AWAITABLE_STATE_INIT) {
2532
0
        PyObject *method = o->agt_typ ? &_Py_ID(athrow) : &_Py_ID(aclose);
2533
0
        _PyErr_WarnUnawaitedAgenMethod(o->agt_gen, method);
2534
0
    }
2535
0
}
2536
2537
static PyMethodDef async_gen_athrow_methods[] = {
2538
    {"send", async_gen_athrow_send, METH_O, send_doc},
2539
    {"throw", _PyCFunction_CAST(async_gen_athrow_throw),
2540
    METH_FASTCALL, throw_doc},
2541
    {"close", async_gen_athrow_close, METH_NOARGS, close_doc},
2542
    {NULL, NULL}        /* Sentinel */
2543
};
2544
2545
2546
static PyAsyncMethods async_gen_athrow_as_async = {
2547
    PyObject_SelfIter,                          /* am_await */
2548
    0,                                          /* am_aiter */
2549
    0,                                          /* am_anext */
2550
    0,                                          /* am_send  */
2551
};
2552
2553
2554
PyTypeObject _PyAsyncGenAThrow_Type = {
2555
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2556
    "async_generator_athrow",                   /* tp_name */
2557
    sizeof(PyAsyncGenAThrow),                   /* tp_basicsize */
2558
    0,                                          /* tp_itemsize */
2559
    /* methods */
2560
    async_gen_athrow_dealloc,                   /* tp_dealloc */
2561
    0,                                          /* tp_vectorcall_offset */
2562
    0,                                          /* tp_getattr */
2563
    0,                                          /* tp_setattr */
2564
    &async_gen_athrow_as_async,                 /* tp_as_async */
2565
    0,                                          /* tp_repr */
2566
    0,                                          /* tp_as_number */
2567
    0,                                          /* tp_as_sequence */
2568
    0,                                          /* tp_as_mapping */
2569
    0,                                          /* tp_hash */
2570
    0,                                          /* tp_call */
2571
    0,                                          /* tp_str */
2572
    PyObject_GenericGetAttr,                    /* tp_getattro */
2573
    0,                                          /* tp_setattro */
2574
    0,                                          /* tp_as_buffer */
2575
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
2576
    0,                                          /* tp_doc */
2577
    async_gen_athrow_traverse,                  /* tp_traverse */
2578
    0,                                          /* tp_clear */
2579
    0,                                          /* tp_richcompare */
2580
    0,                                          /* tp_weaklistoffset */
2581
    PyObject_SelfIter,                          /* tp_iter */
2582
    async_gen_athrow_iternext,                  /* tp_iternext */
2583
    async_gen_athrow_methods,                   /* tp_methods */
2584
    0,                                          /* tp_members */
2585
    0,                                          /* tp_getset */
2586
    0,                                          /* tp_base */
2587
    0,                                          /* tp_dict */
2588
    0,                                          /* tp_descr_get */
2589
    0,                                          /* tp_descr_set */
2590
    0,                                          /* tp_dictoffset */
2591
    0,                                          /* tp_init */
2592
    0,                                          /* tp_alloc */
2593
    0,                                          /* tp_new */
2594
    .tp_finalize = async_gen_athrow_finalize,
2595
};
2596
2597
2598
static PyObject *
2599
async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
2600
0
{
2601
0
    PyObject *typ = NULL;
2602
0
    PyObject *tb = NULL;
2603
0
    PyObject *val = NULL;
2604
0
    if (args && !PyArg_UnpackTuple(args, "athrow", 1, 3, &typ, &val, &tb)) {
2605
0
        return NULL;
2606
0
    }
2607
2608
0
    PyAsyncGenAThrow *o;
2609
0
    o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type);
2610
0
    if (o == NULL) {
2611
0
        return NULL;
2612
0
    }
2613
0
    o->agt_gen = (PyAsyncGenObject*)Py_NewRef(gen);
2614
0
    o->agt_typ = Py_XNewRef(typ);
2615
0
    o->agt_tb = Py_XNewRef(tb);
2616
0
    o->agt_val = Py_XNewRef(val);
2617
2618
0
    o->agt_state = AWAITABLE_STATE_INIT;
2619
0
    _PyObject_GC_TRACK((PyObject*)o);
2620
0
    return (PyObject*)o;
2621
0
}