Skip to content

Commit b76dde1

Browse files
morealclaude
andcommitted
Try __await__ method for generator subclasses in anext_awaitable
For generators without CO_ITERABLE_COROUTINE flag, try to get __await__ method instead of returning an error immediately. This matches CPython's _PyCoro_GetAwaitableIter behavior which allows generator subclasses that define __await__ to be used in await expressions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ae3a2eb commit b76dde1

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

crates/vm/src/builtins/asyncgenerator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ impl PyAnextAwaitable {
604604
{
605605
// Return the generator itself as the iterator
606606
return Ok(wrapped.clone());
607+
}
608+
// Fall through: try to get __await__ method for generator subclasses
609+
if let Some(await_method) = vm.get_method(wrapped.clone(), identifier!(vm, __await__)) {
610+
await_method?.call((), vm)?
607611
} else {
608612
return Err(vm.new_type_error(format!(
609613
"object {} can't be used in 'await' expression",

0 commit comments

Comments
 (0)