Skip to content

Commit 1393bd3

Browse files
authored
gh-131666: mark anext_awaitable.close as a METH_NOARGS instead of METH_VARARGS (#131671)
1 parent c3b8d73 commit 1393bd3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Lib/test/test_coroutines.py

+11
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):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix signature of ``anext_awaitable.close`` objects. Patch by Bénédikt Tran.

Objects/iterobject.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,11 @@ anextawaitable_proxy(anextawaitableobject *obj, char *meth, PyObject *arg)
414414
if (awaitable == NULL) {
415415
return NULL;
416416
}
417-
// 'arg' may be a tuple (if coming from a METH_VARARGS method)
418-
// or a single object (if coming from a METH_O method).
419-
PyObject *ret = PyObject_CallMethod(awaitable, meth, "O", arg);
417+
// When specified, 'arg' may be a tuple (if coming from a METH_VARARGS
418+
// method) or a single object (if coming from a METH_O method).
419+
PyObject *ret = arg == NULL
420+
? PyObject_CallMethod(awaitable, meth, NULL)
421+
: PyObject_CallMethod(awaitable, meth, "O", arg);
420422
Py_DECREF(awaitable);
421423
if (ret != NULL) {
422424
return ret;
@@ -451,10 +453,10 @@ anextawaitable_throw(PyObject *op, PyObject *args)
451453

452454

453455
static PyObject *
454-
anextawaitable_close(PyObject *op, PyObject *args)
456+
anextawaitable_close(PyObject *op, PyObject *Py_UNUSED(dummy))
455457
{
456458
anextawaitableobject *obj = anextawaitableobject_CAST(op);
457-
return anextawaitable_proxy(obj, "close", args);
459+
return anextawaitable_proxy(obj, "close", NULL);
458460
}
459461

460462

@@ -480,7 +482,7 @@ PyDoc_STRVAR(close_doc,
480482
static PyMethodDef anextawaitable_methods[] = {
481483
{"send", anextawaitable_send, METH_O, send_doc},
482484
{"throw", anextawaitable_throw, METH_VARARGS, throw_doc},
483-
{"close", anextawaitable_close, METH_VARARGS, close_doc},
485+
{"close", anextawaitable_close, METH_NOARGS, close_doc},
484486
{NULL, NULL} /* Sentinel */
485487
};
486488

0 commit comments

Comments
 (0)