|
17 | 17 |
|
18 | 18 | #include "pystats.h" |
19 | 19 |
|
| 20 | +/*[clinic input] |
| 21 | +class generator "PyGenObject *" "&PyGen_Type" |
| 22 | +[clinic start generated code]*/ |
| 23 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a6c98fdc710c6976]*/ |
| 24 | + |
| 25 | +#include "clinic/genobject.c.h" |
| 26 | + |
20 | 27 | // Forward declarations |
21 | | -static PyObject* gen_close(PyObject *, PyObject *); |
22 | 28 | static PyObject* async_gen_asend_new(PyAsyncGenObject *, PyObject *); |
23 | 29 | static PyObject* async_gen_athrow_new(PyAsyncGenObject *, PyObject *); |
24 | 30 |
|
@@ -119,7 +125,7 @@ _PyGen_Finalize(PyObject *self) |
119 | 125 | _PyErr_WarnUnawaitedCoroutine((PyObject *)gen); |
120 | 126 | } |
121 | 127 | else { |
122 | | - PyObject *res = gen_close((PyObject*)gen, NULL); |
| 128 | + PyObject *res = gen_close(gen, NULL); |
123 | 129 | if (res == NULL) { |
124 | 130 | if (PyErr_Occurred()) { |
125 | 131 | PyErr_WriteUnraisable(self); |
@@ -335,7 +341,7 @@ gen_close_iter(PyObject *yf) |
335 | 341 | PyObject *retval = NULL; |
336 | 342 |
|
337 | 343 | if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) { |
338 | | - retval = gen_close((PyObject *)yf, NULL); |
| 344 | + retval = gen_close((PyGenObject *)yf, NULL); |
339 | 345 | if (retval == NULL) |
340 | 346 | return -1; |
341 | 347 | } |
@@ -389,8 +395,16 @@ _PyGen_yf(PyGenObject *gen) |
389 | 395 | return res; |
390 | 396 | } |
391 | 397 |
|
| 398 | +/*[clinic input] |
| 399 | +@critical_section |
| 400 | +generator.close as gen_close |
| 401 | +
|
| 402 | +raise GeneratorExit inside generator. |
| 403 | +[clinic start generated code]*/ |
| 404 | + |
392 | 405 | static PyObject * |
393 | | -gen_close(PyObject *self, PyObject *args) |
| 406 | +gen_close_impl(PyGenObject *self) |
| 407 | +/*[clinic end generated code: output=2d7adf450173059c input=6c40e85559b6f098]*/ |
394 | 408 | { |
395 | 409 | PyGenObject *gen = _PyGen_CAST(self); |
396 | 410 |
|
@@ -624,7 +638,11 @@ gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs) |
624 | 638 | else if (nargs == 2) { |
625 | 639 | val = args[1]; |
626 | 640 | } |
627 | | - return _gen_throw(gen, 1, typ, val, tb); |
| 641 | + PyObject *res; |
| 642 | + Py_BEGIN_CRITICAL_SECTION(gen); |
| 643 | + res = _gen_throw(gen, 1, typ, val, tb); |
| 644 | + Py_END_CRITICAL_SECTION(); |
| 645 | + return res; |
628 | 646 | } |
629 | 647 |
|
630 | 648 |
|
@@ -856,7 +874,7 @@ PyDoc_STRVAR(sizeof__doc__, |
856 | 874 | static PyMethodDef gen_methods[] = { |
857 | 875 | {"send", gen_send, METH_O, send_doc}, |
858 | 876 | {"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc}, |
859 | | - {"close", gen_close, METH_NOARGS, close_doc}, |
| 877 | + GEN_CLOSE_METHODDEF |
860 | 878 | {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, |
861 | 879 | {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
862 | 880 | {NULL, NULL} /* Sentinel */ |
@@ -1217,7 +1235,7 @@ PyDoc_STRVAR(coro_close_doc, |
1217 | 1235 | static PyMethodDef coro_methods[] = { |
1218 | 1236 | {"send", gen_send, METH_O, coro_send_doc}, |
1219 | 1237 | {"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc}, |
1220 | | - {"close", gen_close, METH_NOARGS, coro_close_doc}, |
| 1238 | + GEN_CLOSE_METHODDEF |
1221 | 1239 | {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, |
1222 | 1240 | {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
1223 | 1241 | {NULL, NULL} /* Sentinel */ |
@@ -1316,7 +1334,7 @@ static PyObject * |
1316 | 1334 | coro_wrapper_close(PyObject *self, PyObject *args) |
1317 | 1335 | { |
1318 | 1336 | PyCoroWrapper *cw = _PyCoroWrapper_CAST(self); |
1319 | | - return gen_close((PyObject *)cw->cw_coroutine, args); |
| 1337 | + return gen_close((PyGenObject *)cw->cw_coroutine, args); |
1320 | 1338 | } |
1321 | 1339 |
|
1322 | 1340 | static int |
|
0 commit comments