MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_awaitable_iter

Function get_awaitable_iter

crates/vm/src/coroutine.rs:293–333  ·  view source on GitHub ↗

Get an awaitable iterator from an object. Returns the object itself if it's a coroutine or iterable coroutine (generator with CO_ITERABLE_COROUTINE flag). Otherwise calls `__await__()` and validates the result.

(obj: PyObjectRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

291/// Returns the object itself if it's a coroutine or iterable coroutine (generator with
292/// CO_ITERABLE_COROUTINE flag). Otherwise calls `__await__()` and validates the result.
293pub fn get_awaitable_iter(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
294 use crate::builtins::{PyCoroutine, PyGenerator};
295 use crate::protocol::PyIter;
296
297 if obj.downcastable::<PyCoroutine>()
298 || obj.downcast_ref::<PyGenerator>().is_some_and(|g| {
299 g.as_coro()
300 .frame()
301 .code
302 .flags
303 .contains(crate::bytecode::CodeFlags::ITERABLE_COROUTINE)
304 })
305 {
306 return Ok(obj);
307 }
308
309 if let Some(await_method) = vm.get_method(obj.clone(), identifier!(vm, __await__)) {
310 let result = await_method?.call((), vm)?;
311 // __await__() must NOT return a coroutine (PEP 492)
312 if result.downcastable::<PyCoroutine>()
313 || result.downcast_ref::<PyGenerator>().is_some_and(|g| {
314 g.as_coro()
315 .frame()
316 .code
317 .flags
318 .contains(crate::bytecode::CodeFlags::ITERABLE_COROUTINE)
319 })
320 {
321 return Err(vm.new_type_error("__await__() returned a coroutine"));
322 }
323 if !PyIter::check(&result) {
324 return Err(vm.new_type_error(format!(
325 "__await__() returned non-iterator of type '{}'",
326 result.class().name()
327 )));
328 }
329 return Ok(result);
330 }
331
332 Err(vm.new_type_error(format!("'{}' object can't be awaited", obj.class().name())))
333}
334
335/// Emit DeprecationWarning for the deprecated 3-argument throw() signature.
336pub fn warn_deprecated_throw_signature(

Callers 1

execute_instructionMethod · 0.85

Calls 8

frameMethod · 0.80
ErrClass · 0.50
checkFunction · 0.50
containsMethod · 0.45
as_coroMethod · 0.45
get_methodMethod · 0.45
cloneMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected