Skip to content

Commit acd7c22

Browse files
committed
GetANext
1 parent c30080c commit acd7c22

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

vm/src/frame.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -995,11 +995,33 @@ impl ExecutingFrame<'_> {
995995
}
996996
bytecode::Instruction::GetANext => {
997997
let aiter = self.last_value();
998-
let awaitable = vm.call_special_method(&aiter, identifier!(vm, __anext__), ())?;
999-
let awaitable = if awaitable.payload_is::<PyCoroutine>() {
1000-
awaitable
998+
let awaitable = if aiter.class().is(vm.ctx.types.async_generator) {
999+
vm.call_special_method(&aiter, identifier!(vm, __anext__), ())?
10011000
} else {
1002-
vm.call_special_method(&awaitable, identifier!(vm, __await__), ())?
1001+
assert!(false);
1002+
// if !hasattr(aiter, __anext__): raise TypeError("'async for' requires in iterator...")
1003+
let next_iter =
1004+
vm.call_special_method(&aiter, identifier!(vm, __anext__), ())?;
1005+
1006+
// TODO: _PyCoro_GetAwaitableIter
1007+
fn get_awaitable_iter(next_iter: &PyObject, vm: &VirtualMachine) -> PyResult {
1008+
let gen_is_coroutine = |_| {
1009+
// TODO:
1010+
true
1011+
};
1012+
if next_iter.class().is(vm.ctx.types.coroutine_type)
1013+
|| gen_is_coroutine(next_iter)
1014+
{
1015+
return Ok(next_iter.to_owned());
1016+
}
1017+
vm.call_special_method(next_iter, identifier!(vm, __await__), ())
1018+
}
1019+
get_awaitable_iter(&next_iter, vm).map_err(|_| {
1020+
vm.new_type_error(format!(
1021+
"'async for' received an invalid object from __anext__: {:.200}",
1022+
next_iter.class().name()
1023+
))
1024+
})?
10031025
};
10041026
self.push_value(awaitable);
10051027
Ok(None)

0 commit comments

Comments
 (0)