Skip to content

gh-131666: mark anext_awaitable.close as a METH_NOARGS instead of METH_VARARGS #131671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this results in an unawaited coroutine warning

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed here #131708


def test_with_1(self):
class Manager:
def __init__(self, name):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix signature of ``anext_awaitable.close`` objects. Patch by Bénédikt Tran.
14 changes: 8 additions & 6 deletions Objects/iterobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand All @@ -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 */
};

Expand Down
Loading