From 4d2af4b83db09f84967cd3775963171a40798559 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Sun, 14 Apr 2024 00:08:41 +0800 Subject: [PATCH] allow `next` to be compatible with cpython (#241) * add `itertools` * Update _generated.h * allow `next` to be compatible with cpython --- src/vm.cpp | 2 +- tests/28_iter.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/vm.cpp b/src/vm.cpp index 6eee7b84..56e9c61f 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -729,7 +729,7 @@ void VM::init_builtin_types(){ this->Ellipsis = heap._new(_new_type_object("ellipsis")); this->True = heap._new(tp_bool); this->False = heap._new(tp_bool); - this->StopIteration = heap._new(_new_type_object("StopIterationType")); + this->StopIteration = _all_types[_new_type_object("StopIteration", tp_exception)].obj; this->builtins = new_module("builtins"); diff --git a/tests/28_iter.py b/tests/28_iter.py index e75c7e9a..20bbcd3c 100644 --- a/tests/28_iter.py +++ b/tests/28_iter.py @@ -36,3 +36,21 @@ assert next(i) == 4 assert next(i) == 5 assert next(i) == StopIteration assert next(i) == StopIteration + +import builtins + +def next(obj): + res = builtins.next(obj) + if res is StopIteration: + raise StopIteration + return res + +a = iter([1]) +assert next(a) == 1 + +try: + next(a) + exit(1) +except StopIteration: + pass +