Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GetANext #5270

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 1 commit into from
Apr 25, 2024
Merged
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
37 changes: 33 additions & 4 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,11 +994,40 @@ impl ExecutingFrame<'_> {
}
bytecode::Instruction::GetANext => {
let aiter = self.top_value();
let awaitable = vm.call_special_method(aiter, identifier!(vm, __anext__), ())?;
let awaitable = if awaitable.payload_is::<PyCoroutine>() {
awaitable
let awaitable = if aiter.class().is(vm.ctx.types.async_generator) {
vm.call_special_method(aiter, identifier!(vm, __anext__), ())?
} else {
vm.call_special_method(&awaitable, identifier!(vm, __await__), ())?
if !aiter.has_attr("__anext__", vm).unwrap_or(false) {
// TODO: __anext__ must be protocol
let msg = format!(
"'async for' requires an iterator with __anext__ method, got {:.100}",
aiter.class().name()
);
return Err(vm.new_type_error(msg));
}
let next_iter =
vm.call_special_method(aiter, identifier!(vm, __anext__), ())?;

// _PyCoro_GetAwaitableIter in CPython
fn get_awaitable_iter(next_iter: &PyObject, vm: &VirtualMachine) -> PyResult {
let gen_is_coroutine = |_| {
// TODO: cpython gen_is_coroutine
true
};
if next_iter.class().is(vm.ctx.types.coroutine_type)
|| gen_is_coroutine(next_iter)
{
return Ok(next_iter.to_owned());
}
// TODO: error handling
vm.call_special_method(next_iter, identifier!(vm, __await__), ())
}
get_awaitable_iter(&next_iter, vm).map_err(|_| {
vm.new_type_error(format!(
"'async for' received an invalid object from __anext__: {:.200}",
next_iter.class().name()
))
})?
};
self.push_value(awaitable);
Ok(None)
Expand Down