https://github.com/python/cpython/commit/1393bd35485d9a8aa99ceb7389fece91bb3fdefa
commit: 1393bd35485d9a8aa99ceb7389fece91bb3fdefa
branch: main
author: Bénédikt Tran <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-03-25T09:03:22+05:30
summary:
gh-131666: mark `anext_awaitable.close` as a `METH_NOARGS` instead of
`METH_VARARGS` (#131671)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-14-56-00.gh-issue-131666.q0-a-b.rst
M Lib/test/test_coroutines.py
M Objects/iterobject.c
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index ae3cd3555002ef..d78eaaca2796a6 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -1191,6 +1191,17 @@ async def g():
_, result = run_async(g())
self.assertIsNone(result.__context__)
+ def test_await_17(self):
+ # See https://github.com/python/cpython/issues/131666 for details.
+ class A:
+ async def __anext__(self):
+ raise StopAsyncIteration
+ def __aiter__(self):
+ return self
+
+ anext_awaitable = anext(A(), "a").__await__()
+ self.assertRaises(TypeError, anext_awaitable.close, 1)
+
def test_with_1(self):
class Manager:
def __init__(self, name):
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-14-56-00.gh-issue-131666.q0-a-b.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-14-56-00.gh-issue-131666.q0-a-b.rst
new file mode 100644
index 00000000000000..45ac86e520d929
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-14-56-00.gh-issue-131666.q0-a-b.rst
@@ -0,0 +1 @@
+Fix signature of ``anext_awaitable.close`` objects. Patch by Bénédikt Tran.
diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index 539fe360504c40..5712e02ae828ab 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -414,9 +414,11 @@ anextawaitable_proxy(anextawaitableobject *obj, char
*meth, PyObject *arg)
if (awaitable == NULL) {
return NULL;
}
- // 'arg' may be a tuple (if coming from a METH_VARARGS method)
- // or a single object (if coming from a METH_O method).
- PyObject *ret = PyObject_CallMethod(awaitable, meth, "O", arg);
+ // When specified, 'arg' may be a tuple (if coming from a METH_VARARGS
+ // method) or a single object (if coming from a METH_O method).
+ PyObject *ret = arg == NULL
+ ? PyObject_CallMethod(awaitable, meth, NULL)
+ : PyObject_CallMethod(awaitable, meth, "O", arg);
Py_DECREF(awaitable);
if (ret != NULL) {
return ret;
@@ -451,10 +453,10 @@ anextawaitable_throw(PyObject *op, PyObject *args)
static PyObject *
-anextawaitable_close(PyObject *op, PyObject *args)
+anextawaitable_close(PyObject *op, PyObject *Py_UNUSED(dummy))
{
anextawaitableobject *obj = anextawaitableobject_CAST(op);
- return anextawaitable_proxy(obj, "close", args);
+ return anextawaitable_proxy(obj, "close", NULL);
}
@@ -480,7 +482,7 @@ PyDoc_STRVAR(close_doc,
static PyMethodDef anextawaitable_methods[] = {
{"send", anextawaitable_send, METH_O, send_doc},
{"throw", anextawaitable_throw, METH_VARARGS, throw_doc},
- {"close", anextawaitable_close, METH_VARARGS, close_doc},
+ {"close", anextawaitable_close, METH_NOARGS, close_doc},
{NULL, NULL} /* Sentinel */
};
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]