diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-26 19:27:58 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-26 19:27:58 +0000 |
commit | 66eaf84b8c2db6afad7ec49eb296a019a2f377df (patch) | |
tree | a05406b020ba4102ffd90fdfb5357a301b9de637 /tests/basics/iter1.py | |
parent | 688e220d268609ec1a5be7a9b532637fe8c1f765 (diff) | |
download | micropython-66eaf84b8c2db6afad7ec49eb296a019a2f377df.tar.gz micropython-66eaf84b8c2db6afad7ec49eb296a019a2f377df.zip |
py: Replace mp_const_stop_iteration object with MP_OBJ_NULL.
Diffstat (limited to 'tests/basics/iter1.py')
-rw-r--r-- | tests/basics/iter1.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/basics/iter1.py b/tests/basics/iter1.py new file mode 100644 index 0000000000..c2ef86a635 --- /dev/null +++ b/tests/basics/iter1.py @@ -0,0 +1,52 @@ +# test user defined iterators + +class MyStopIteration(StopIteration): + pass + +class myiter: + def __init__(self, i): + self.i = i + + def __iter__(self): + return self + + def __next__(self): + if self.i <= 0: + # stop in the usual way + raise StopIteration + elif self.i == 10: + # stop with an argument + raise StopIteration(42) + elif self.i == 20: + # raise a non-stop exception + raise TypeError + elif self.i == 30: + # raise a user-defined stop iteration + print('raising MyStopIteration') + raise MyStopIteration + else: + # return the next value + self.i -= 1 + return self.i + +for i in myiter(5): + print(i) + +for i in myiter(12): + print(i) + +try: + for i in myiter(22): + print(i) +except TypeError: + print('raised TypeError') + +try: + for i in myiter(5): + print(i) + raise StopIteration +except StopIteration: + print('raised StopIteration') + +for i in myiter(32): + print(i) |