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