pocketpy/tests/28_iter.py
BLUELOVETH 4d2af4b83d
allow next to be compatible with cpython (#241)
* add `itertools`

* Update _generated.h

* allow `next` to be compatible with cpython
2024-04-14 00:08:41 +08:00

57 lines
849 B
Python

a = [1, 2, 3]
a = iter(a)
total = 0
while True:
obj = next(a)
if obj is StopIteration:
break
total += obj
assert total == 6
class Task:
def __init__(self, n):
self.n = n
def __iter__(self):
self.i = 0
return self
def __next__(self):
if self.i == self.n:
return StopIteration
self.i += 1
return self.i
a = Task(3)
assert sum(a) == 6
i = iter(Task(5))
assert next(i) == 1
assert next(i) == 2
assert next(i) == 3
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