Skip to content

Commit afbb669

Browse files
committed
Add helper for gen_close
1 parent d6cc59a commit afbb669

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

Objects/genobject.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class generator "PyGenObject *" "&PyGen_Type"
2525
#include "clinic/genobject.c.h"
2626

2727
// Forward declarations
28+
static int gen_close(PyGenObject *gen);
2829
static PyObject* async_gen_asend_new(PyAsyncGenObject *, PyObject *);
2930
static PyObject* async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
3031

@@ -125,15 +126,12 @@ _PyGen_Finalize(PyObject *self)
125126
_PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
126127
}
127128
else {
128-
PyObject *res = gen_close(gen, NULL);
129-
if (res == NULL) {
129+
int res = gen_close(gen);
130+
if (res < 0) {
130131
if (PyErr_Occurred()) {
131132
PyErr_WriteUnraisable(self);
132133
}
133134
}
134-
else {
135-
Py_DECREF(res);
136-
}
137135
}
138136

139137
/* Restore the saved exception. */
@@ -339,10 +337,9 @@ static int
339337
gen_close_iter(PyObject *yf)
340338
{
341339
PyObject *retval = NULL;
342-
343340
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
344-
retval = gen_close((PyGenObject *)yf, NULL);
345-
if (retval == NULL)
341+
int retval = gen_close(_PyGen_CAST(yf));
342+
if (retval < 0)
346343
return -1;
347344
}
348345
else {
@@ -470,6 +467,16 @@ gen_close_meth_impl(PyGenObject *self)
470467
return NULL;
471468
}
472469

470+
int gen_close(PyGenObject *gen)
471+
{
472+
PyObject *res;
473+
Py_BEGIN_CRITICAL_SECTION(gen);
474+
res = gen_close_meth_impl(gen);
475+
Py_END_CRITICAL_SECTION();
476+
assert(res == Py_None || res == NULL);
477+
return res == Py_None ? 0 : -1;
478+
}
479+
473480
PyDoc_STRVAR(throw_doc,
474481
"throw(value)\n\
475482
throw(type[,value[,tb]])\n\
@@ -891,7 +898,7 @@ PyDoc_STRVAR(sizeof__doc__,
891898
static PyMethodDef gen_methods[] = {
892899
{"send", gen_send, METH_O, send_doc},
893900
{"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc},
894-
GEN_CLOSE_METHODDEF
901+
GEN_CLOSE_METH_METHODDEF
895902
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
896903
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
897904
{NULL, NULL} /* Sentinel */
@@ -1252,7 +1259,7 @@ PyDoc_STRVAR(coro_close_doc,
12521259
static PyMethodDef coro_methods[] = {
12531260
{"send", gen_send, METH_O, coro_send_doc},
12541261
{"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc},
1255-
GEN_CLOSE_METHODDEF
1262+
GEN_CLOSE_METH_METHODDEF
12561263
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
12571264
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
12581265
{NULL, NULL} /* Sentinel */
@@ -1351,7 +1358,7 @@ static PyObject *
13511358
coro_wrapper_close(PyObject *self, PyObject *args)
13521359
{
13531360
PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
1354-
return gen_close((PyGenObject *)cw->cw_coroutine, args);
1361+
return gen_close(_PyGen_CAST(cw->cw_coroutine)) == 0 ? Py_None : NULL;
13551362
}
13561363

13571364
static int

0 commit comments

Comments
 (0)