circuitpython/tests/micropython/native_gen.py
David Lechner 3dc324d3f1 tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
2020-03-30 13:21:58 +11:00

26 lines
358 B
Python

# test for native generators
# simple generator with yield and return
@micropython.native
def gen1(x):
yield x
yield x + 1
return x + 2
g = gen1(3)
print(next(g))
print(next(g))
try:
next(g)
except StopIteration as e:
print(e.args[0])
# using yield from
@micropython.native
def gen2(x):
yield from range(x)
print(list(gen2(3)))