Skip to content

Commit 743ded1

Browse files
committed
GH-117714: replace athrow().close() and asend().close() stubs with implimentations
1 parent 3131a70 commit 743ded1

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

Objects/genobject.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "pycore_pystate.h" // _PyThreadState_GET()
1616

1717
#include "pystats.h"
18+
#include "pyerrors.h"
1819

1920
static PyObject *gen_close(PyGenObject *, PyObject *);
2021
static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
@@ -1843,8 +1844,25 @@ async_gen_asend_throw(PyAsyncGenASend *o, PyObject *const *args, Py_ssize_t narg
18431844
static PyObject *
18441845
async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
18451846
{
1846-
o->ags_state = AWAITABLE_STATE_CLOSED;
1847-
Py_RETURN_NONE;
1847+
PyObject *result;
1848+
if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1849+
Py_RETURN_NONE;
1850+
}
1851+
result = async_gen_asend_throw(o, &PyExc_GeneratorExit, 1);
1852+
if (result == NULL) {
1853+
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
1854+
PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
1855+
PyErr_ExceptionMatches(PyExc_GeneratorExit))
1856+
{
1857+
PyErr_Clear();
1858+
Py_RETURN_NONE;
1859+
}
1860+
return result;
1861+
} else {
1862+
Py_DECREF(result);
1863+
PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
1864+
return NULL;
1865+
}
18481866
}
18491867

18501868
static void
@@ -2288,8 +2306,25 @@ async_gen_athrow_iternext(PyAsyncGenAThrow *o)
22882306
static PyObject *
22892307
async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
22902308
{
2291-
o->agt_state = AWAITABLE_STATE_CLOSED;
2292-
Py_RETURN_NONE;
2309+
PyObject *result;
2310+
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
2311+
Py_RETURN_NONE;
2312+
}
2313+
result = async_gen_athrow_throw(o, &PyExc_GeneratorExit, 1);
2314+
if (result == NULL) {
2315+
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
2316+
PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
2317+
PyErr_ExceptionMatches(PyExc_GeneratorExit))
2318+
{
2319+
PyErr_Clear();
2320+
Py_RETURN_NONE;
2321+
}
2322+
return result;
2323+
} else {
2324+
Py_DECREF(result);
2325+
PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
2326+
return NULL;
2327+
}
22932328
}
22942329

22952330

0 commit comments

Comments
 (0)