Skip to content

Commit b190119

Browse files
committed
GH-117714: replace athrow().close() and asend().close() stubs with implimentations
1 parent fc7e1aa commit b190119

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
@@ -16,6 +16,7 @@
1616
#include "pycore_pystate.h" // _PyThreadState_GET()
1717

1818
#include "pystats.h"
19+
#include "pyerrors.h"
1920

2021
static PyObject *gen_close(PyGenObject *, PyObject *);
2122
static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
@@ -1846,8 +1847,25 @@ async_gen_asend_throw(PyAsyncGenASend *o, PyObject *const *args, Py_ssize_t narg
18461847
static PyObject *
18471848
async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
18481849
{
1849-
o->ags_state = AWAITABLE_STATE_CLOSED;
1850-
Py_RETURN_NONE;
1850+
PyObject *result;
1851+
if (o->ags_state == AWAITABLE_STATE_CLOSED) {
1852+
Py_RETURN_NONE;
1853+
}
1854+
result = async_gen_asend_throw(o, &PyExc_GeneratorExit, 1);
1855+
if (result == NULL) {
1856+
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
1857+
PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
1858+
PyErr_ExceptionMatches(PyExc_GeneratorExit))
1859+
{
1860+
PyErr_Clear();
1861+
Py_RETURN_NONE;
1862+
}
1863+
return result;
1864+
} else {
1865+
Py_DECREF(result);
1866+
PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
1867+
return NULL;
1868+
}
18511869
}
18521870

18531871
static void
@@ -2291,8 +2309,25 @@ async_gen_athrow_iternext(PyAsyncGenAThrow *o)
22912309
static PyObject *
22922310
async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
22932311
{
2294-
o->agt_state = AWAITABLE_STATE_CLOSED;
2295-
Py_RETURN_NONE;
2312+
PyObject *result;
2313+
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
2314+
Py_RETURN_NONE;
2315+
}
2316+
result = async_gen_athrow_throw(o, &PyExc_GeneratorExit, 1);
2317+
if (result == NULL) {
2318+
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
2319+
PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
2320+
PyErr_ExceptionMatches(PyExc_GeneratorExit))
2321+
{
2322+
PyErr_Clear();
2323+
Py_RETURN_NONE;
2324+
}
2325+
return result;
2326+
} else {
2327+
Py_DECREF(result);
2328+
PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
2329+
return NULL;
2330+
}
22962331
}
22972332

22982333

0 commit comments

Comments
 (0)