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)
| 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. |
| 293 | pub 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. |
| 336 | pub fn warn_deprecated_throw_signature( |
no test coverage detected