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