From 8455c741a644a1741ad37d9271b902da628cfa3f Mon Sep 17 00:00:00 2001 From: sum12 Date: Tue, 19 Apr 2022 20:15:28 +0200 Subject: [PATCH 1/3] implement aiter the caller is expected to make sure that the passed in object does in fact supports the protocol relates #3609 --- vm/src/stdlib/builtins.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index c13a8299085..657ee563877 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -426,6 +426,11 @@ mod builtins { } } + #[pyfunction] + fn aiter(iter_target: PyObjectRef, vm: &VirtualMachine) -> PyResult { + vm.call_special_method(iter_target, "__aiter__", ()) + } + #[pyfunction] fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { obj.length(vm) From d74a1d58a687fc74d03a5d6f788d723ac2835ccf Mon Sep 17 00:00:00 2001 From: sum12 Date: Tue, 19 Apr 2022 23:12:20 +0200 Subject: [PATCH 2/3] added test for builtin.aiter --- Lib/test/test_asyncgen.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index c8024d659f1..f6d05a61437 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -543,8 +543,6 @@ async def consume(): self.loop.run_until_complete(consume()) self.assertEqual(results, [1, 2]) - # TODO: RUSTPYTHON, NameError: name 'aiter' is not defined - @unittest.expectedFailure def test_aiter_idempotent(self): async def gen(): yield 1 @@ -766,8 +764,6 @@ def run_test(test): run_test(test5) run_test(test6) - # TODO: RUSTPYTHON, NameError: name 'aiter' is not defined - @unittest.expectedFailure def test_aiter_bad_args(self): async def gen(): yield 1 From 5fdf62a6b9336309f6d0e8db2a1b521e0b65bd80 Mon Sep 17 00:00:00 2001 From: sum12 Date: Wed, 20 Apr 2022 10:05:11 +0200 Subject: [PATCH 3/3] Check for PyAsyncGen when calling aiter --- vm/src/stdlib/builtins.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 657ee563877..c1829d3dfda 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -12,6 +12,7 @@ mod builtins { use crate::compile; use crate::{ builtins::{ + asyncgenerator::PyAsyncGen, enumerate::PyReverseSequenceIterator, function::{PyCellRef, PyFunctionRef}, int::PyIntRef, @@ -428,7 +429,11 @@ mod builtins { #[pyfunction] fn aiter(iter_target: PyObjectRef, vm: &VirtualMachine) -> PyResult { - vm.call_special_method(iter_target, "__aiter__", ()) + if iter_target.payload_is::() { + vm.call_special_method(iter_target, "__aiter__", ()) + } else { + Err(vm.new_type_error("wrong argument type".to_owned())) + } } #[pyfunction]