]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131666: mark `anext_awaitable.close` as a `METH_NOARGS` instead of `METH_VARARGS...
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Tue, 25 Mar 2025 03:33:22 +0000 (04:33 +0100)
committerGitHub <noreply@github.com>
Tue, 25 Mar 2025 03:33:22 +0000 (09:03 +0530)
Lib/test/test_coroutines.py
Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-14-56-00.gh-issue-131666.q0-a-b.rst [new file with mode: 0644]
Objects/iterobject.c

index ae3cd3555002ef79a0013d230b92f35d888fedc0..d78eaaca2796a6ff1a9320bd3d27ba25049e024d 100644 (file)
@@ -1191,6 +1191,17 @@ class CoroutineTest(unittest.TestCase):
         _, 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 (file)
index 0000000..45ac86e
--- /dev/null
@@ -0,0 +1 @@
+Fix signature of ``anext_awaitable.close`` objects. Patch by Bénédikt Tran.
index 539fe360504c40f32ae290bafbbed2cee57796a3..5712e02ae828ab7e6abeb78b4cf17bc7b01cdae7 100644 (file)
@@ -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 */
 };