Skip to content

Commit a4c728e

Browse files
committed
fix signature of anext_awaitable.close
1 parent 0e53038 commit a4c728e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Lib/test/test_coroutines.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,17 @@ async def g():
11911191
_, result = run_async(g())
11921192
self.assertIsNone(result.__context__)
11931193

1194+
def test_await_17(self):
1195+
# See https://github.com/python/cpython/issues/131666 for details.
1196+
class A:
1197+
async def __anext__(self):
1198+
raise StopAsyncIteration
1199+
def __aiter__(self):
1200+
return self
1201+
1202+
anext_awaitable = anext(A(), "a").__await__()
1203+
self.assertRaises(TypeError, anext_awaitable.close, 1)
1204+
11941205
def test_with_1(self):
11951206
class Manager:
11961207
def __init__(self, name):

Objects/iterobject.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ anextawaitable_proxy(anextawaitableobject *obj, char *meth, PyObject *arg) {
408408
if (awaitable == NULL) {
409409
return NULL;
410410
}
411-
PyObject *ret = PyObject_CallMethod(awaitable, meth, "O", arg);
411+
PyObject *ret = arg == NULL
412+
? PyObject_CallMethod(awaitable, meth, NULL)
413+
: PyObject_CallMethod(awaitable, meth, "O", arg);
412414
Py_DECREF(awaitable);
413415
if (ret != NULL) {
414416
return ret;
@@ -439,8 +441,8 @@ anextawaitable_throw(anextawaitableobject *obj, PyObject *arg) {
439441

440442

441443
static PyObject *
442-
anextawaitable_close(anextawaitableobject *obj, PyObject *arg) {
443-
return anextawaitable_proxy(obj, "close", arg);
444+
anextawaitable_close(anextawaitableobject *obj, PyObject *Py_UNUSED(dummy)) {
445+
return anextawaitable_proxy(obj, "close", NULL);
444446
}
445447

446448

@@ -466,7 +468,7 @@ PyDoc_STRVAR(close_doc,
466468
static PyMethodDef anextawaitable_methods[] = {
467469
{"send",(PyCFunction)anextawaitable_send, METH_O, send_doc},
468470
{"throw",(PyCFunction)anextawaitable_throw, METH_VARARGS, throw_doc},
469-
{"close",(PyCFunction)anextawaitable_close, METH_VARARGS, close_doc},
471+
{"close",(PyCFunction)anextawaitable_close, METH_NOARGS, close_doc},
470472
{NULL, NULL} /* Sentinel */
471473
};
472474

0 commit comments

Comments
 (0)