Coverage Report

Created: 2026-02-26 06:53

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