summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorJoshua Bronson <jabronson@gmail.com>2021-03-23 22:47:21 (GMT)
committerGitHub <noreply@github.com>2021-03-23 22:47:21 (GMT)
commitf0a6fde8827d5d4f7a1c741ab1a8b206b66ffd57 (patch)
tree8ce99219dd179cef43115c42646ef8840e55d68c /Python
parent94faa0724f8cbae6867c491c8e465e35f4fdbfbb (diff)
downloadcpython-f0a6fde8827d5d4f7a1c741ab1a8b206b66ffd57.zip
cpython-f0a6fde8827d5d4f7a1c741ab1a8b206b66ffd57.tar.gz
cpython-f0a6fde8827d5d4f7a1c741ab1a8b206b66ffd57.tar.bz2
bpo-31861: Add aiter and anext to builtins (#23847)
Co-authored-by: jab <jab@users.noreply.github.com> Co-authored-by: Daniel Pope <mauve@mauveweb.co.uk> Co-authored-by: Justin Wang <justin39@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c55
-rw-r--r--Python/clinic/bltinmodule.c.h46
2 files changed, 100 insertions, 1 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index c3f7e39..fd9b97f 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1611,6 +1611,59 @@ In the second form, the callable is called until it returns the sentinel.");
/*[clinic input]
+aiter as builtin_aiter
+
+ async_iterable: object
+ /
+
+Return an AsyncIterator for an AsyncIterable object.
+[clinic start generated code]*/
+
+static PyObject *
+builtin_aiter(PyObject *module, PyObject *async_iterable)
+/*[clinic end generated code: output=1bae108d86f7960e input=473993d0cacc7d23]*/
+{
+ return PyObject_GetAiter(async_iterable);
+}
+
+PyObject *PyAnextAwaitable_New(PyObject *, PyObject *);
+
+/*[clinic input]
+anext as builtin_anext
+
+ aiterator: object
+ default: object = NULL
+ /
+
+Return the next item from the async iterator.
+[clinic start generated code]*/
+
+static PyObject *
+builtin_anext_impl(PyObject *module, PyObject *aiterator,
+ PyObject *default_value)
+/*[clinic end generated code: output=f02c060c163a81fa input=699d11f4e38eca24]*/
+{
+ PyTypeObject *t;
+ PyObject *awaitable;
+
+ t = Py_TYPE(aiterator);
+ if (t->tp_as_async == NULL || t->tp_as_async->am_anext == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not an async iterator",
+ t->tp_name);
+ return NULL;
+ }
+
+ awaitable = (*t->tp_as_async->am_anext)(aiterator);
+ if (default_value == NULL) {
+ return awaitable;
+ }
+
+ return PyAnextAwaitable_New(awaitable, default_value);
+}
+
+
+/*[clinic input]
len as builtin_len
obj: object
@@ -2890,11 +2943,13 @@ static PyMethodDef builtin_methods[] = {
BUILTIN_ISINSTANCE_METHODDEF
BUILTIN_ISSUBCLASS_METHODDEF
{"iter", (PyCFunction)(void(*)(void))builtin_iter, METH_FASTCALL, iter_doc},
+ BUILTIN_AITER_METHODDEF
BUILTIN_LEN_METHODDEF
BUILTIN_LOCALS_METHODDEF
{"max", (PyCFunction)(void(*)(void))builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
{"min", (PyCFunction)(void(*)(void))builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
{"next", (PyCFunction)(void(*)(void))builtin_next, METH_FASTCALL, next_doc},
+ BUILTIN_ANEXT_METHODDEF
BUILTIN_OCT_METHODDEF
BUILTIN_ORD_METHODDEF
BUILTIN_POW_METHODDEF
diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h
index bc3b518..545f5b5 100644
--- a/Python/clinic/bltinmodule.c.h
+++ b/Python/clinic/bltinmodule.c.h
@@ -530,6 +530,50 @@ PyDoc_STRVAR(builtin_hex__doc__,
#define BUILTIN_HEX_METHODDEF \
{"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__},
+PyDoc_STRVAR(builtin_aiter__doc__,
+"aiter($module, async_iterable, /)\n"
+"--\n"
+"\n"
+"Return an AsyncIterator for an AsyncIterable object.");
+
+#define BUILTIN_AITER_METHODDEF \
+ {"aiter", (PyCFunction)builtin_aiter, METH_O, builtin_aiter__doc__},
+
+PyDoc_STRVAR(builtin_anext__doc__,
+"anext($module, aiterator, default=<unrepresentable>, /)\n"
+"--\n"
+"\n"
+"Return the next item from the async iterator.");
+
+#define BUILTIN_ANEXT_METHODDEF \
+ {"anext", (PyCFunction)(void(*)(void))builtin_anext, METH_FASTCALL, builtin_anext__doc__},
+
+static PyObject *
+builtin_anext_impl(PyObject *module, PyObject *aiterator,
+ PyObject *default_value);
+
+static PyObject *
+builtin_anext(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *aiterator;
+ PyObject *default_value = NULL;
+
+ if (!_PyArg_CheckPositional("anext", nargs, 1, 2)) {
+ goto exit;
+ }
+ aiterator = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ default_value = args[1];
+skip_optional:
+ return_value = builtin_anext_impl(module, aiterator, default_value);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(builtin_len__doc__,
"len($module, obj, /)\n"
"--\n"
@@ -830,4 +874,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit:
return return_value;
}
-/*[clinic end generated code: output=e2fcf0201790367c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=da9ae459e9233259 input=a9049054013a1b77]*/