RustPython icon indicating copy to clipboard operation
RustPython copied to clipboard

Add `aiter` and `anext` builtin function

Open youknowone opened this issue 3 years ago • 4 comments

Feature

  • [x] builtins.aiter: https://docs.python.org/3/library/functions.html#aiter #3646
  • [ ] builtins.anext: https://docs.python.org/3/library/functions.html#anext (See @Snowapril 's comment)

Python Documentation

youknowone avatar Apr 14 '22 09:04 youknowone

builtins.aiter can be implemented quite easy with just a few lines of code. but for builtins.anext, it seems anext_awaitable must be implemented first.

static PyObject *
builtin_anext_impl(PyObject *module, PyObject *aiterator,
                   PyObject *default_value)
{
    // ...skip
    awaitable = (*t->tp_as_async->am_anext)(aiterator);
    if (default_value == NULL) {
        return awaitable;
    }

    PyObject* new_awaitable = PyAnextAwaitable_New(
            awaitable, default_value);
    Py_DECREF(awaitable);
    return new_awaitable;
}

Snowapril avatar Apr 14 '22 09:04 Snowapril

Thanks for the "good for issue". I am probably misunderstanding things. the slots for tp_as_async->am_aiter is missing. According to cpython that would be the correct way to check if the object has __aiter__ implementaed, Is this the correct way to proceed, probably an outline of how this could be done would be great

sum12 avatar Apr 17 '22 21:04 sum12

@sum12 you are right. that's the right way. But instead of the right way, you can implement it by calling __aiter__ directly.

If you are interested in the right way, You can do it by looking at other slots, like IterNext or Callable. The right way would not be the 'good first issue'.

youknowone avatar Apr 18 '22 09:04 youknowone

hey @youknowone @sum12 @Snowapril , i see builtins.anext is still not implemented, however builtins.aiter is. is it possible for me to take on this issue? from what I have understood, I think anext calls the aiter to fetch an async iterator which is then processed in the anext func. I'm assuming I can use this as a starting point to model anext_awaitable.

Any guidance would be appreciated!

sidx04 avatar May 26 '25 11:05 sidx04