diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index f96a3bcbca9..6e655f8be71 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -157,6 +157,11 @@ Other Language Changes * Assignment expressions can now be used unparenthesized within set literals and set comprehensions, as well as in sequence indexes (but not slices). +* Two new builtin functions -- :func:`aiter` and :func:`anext` have been added + to provide asynchronous counterparts to :func:`iter` and :func:`next`, + respectively. + (Contributed by Joshua Bronson, Justin Wang and Daniel Pope in :issue:`31861`) + New Modules =========== diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 71c4f27d27b..9ccdfcabd4e 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3814,6 +3814,9 @@ class TestSignatureDefinitions(unittest.TestCase): needs_groups = {"range", "slice", "dir", "getattr", "next", "iter", "vars"} no_signature |= needs_groups + # These have unpresentable parameter default values of NULL + needs_null = {"aiter", "anext"} + no_signature |= needs_null # These need PEP 457 groups or a signature change to accept None needs_semantic_update = {"round"} no_signature |= needs_semantic_update diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 470982b2861..e532c2a488d 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1539,7 +1539,7 @@ In the second form, the callable is called until it returns the sentinel."); aiter as builtin_aiter aiterable: object - sentinel: object = None + sentinel: object = NULL / Return an async iterator for an async iterable object. @@ -1547,9 +1547,9 @@ Return an async iterator for an async iterable object. static PyObject * builtin_aiter_impl(PyObject *module, PyObject *aiterable, PyObject *sentinel) -/*[clinic end generated code: output=ea120e90169d9f32 input=586f672fb18a94a5]*/ +/*[clinic end generated code: output=ea120e90169d9f32 input=a12efceda1863b3c]*/ { - if (sentinel == Py_None) { + if (sentinel == NULL) { return PyObject_GetAiter(aiterable); } @@ -1566,7 +1566,7 @@ builtin_aiter_impl(PyObject *module, PyObject *aiterable, PyObject *sentinel) anext as builtin_anext aiterator: object - default: object = None + default: object = NULL / Return the next item from the async iterator. @@ -1575,7 +1575,7 @@ Return the next item from the async iterator. static PyObject * builtin_anext_impl(PyObject *module, PyObject *aiterator, PyObject *default_value) -/*[clinic end generated code: output=f02c060c163a81fa input=6051f80000c06306]*/ +/*[clinic end generated code: output=f02c060c163a81fa input=699d11f4e38eca24]*/ { PyTypeObject *t; PyObject *awaitable; @@ -1589,7 +1589,7 @@ builtin_anext_impl(PyObject *module, PyObject *aiterator, } awaitable = (*t->tp_as_async->am_anext)(aiterator); - if (default_value == Py_None) { + if (default_value == NULL) { return awaitable; } diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 4482e0d9c33..822496ca82a 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -531,7 +531,7 @@ PyDoc_STRVAR(builtin_hex__doc__, {"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__}, PyDoc_STRVAR(builtin_aiter__doc__, -"aiter($module, aiterable, sentinel=None, /)\n" +"aiter($module, aiterable, sentinel=, /)\n" "--\n" "\n" "Return an async iterator for an async iterable object."); @@ -547,7 +547,7 @@ builtin_aiter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; PyObject *aiterable; - PyObject *sentinel = Py_None; + PyObject *sentinel = NULL; if (!_PyArg_CheckPositional("aiter", nargs, 1, 2)) { goto exit; @@ -565,7 +565,7 @@ exit: } PyDoc_STRVAR(builtin_anext__doc__, -"anext($module, aiterator, default=None, /)\n" +"anext($module, aiterator, default=, /)\n" "--\n" "\n" "Return the next item from the async iterator."); @@ -582,7 +582,7 @@ builtin_anext(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; PyObject *aiterator; - PyObject *default_value = Py_None; + PyObject *default_value = NULL; if (!_PyArg_CheckPositional("anext", nargs, 1, 2)) { goto exit; @@ -899,4 +899,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=02b80d529a5c972b input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b5248d88ee495198 input=a9049054013a1b77]*/