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