summaryrefslogtreecommitdiff
path: root/tests/micropython
diff options
context:
space:
mode:
authorDavid Lechner <david@lechnology.com>2020-03-22 21:26:08 -0500
committerDamien George <damien.p.george@gmail.com>2020-03-30 13:21:58 +1100
commit3dc324d3f1312e40d3a8ed87e7244966bb756f26 (patch)
tree94ff44f8eabba0039582c245b901173597edd11e /tests/micropython
parent488613bca6c460340ed2995ae5cafafe22d0bfff (diff)
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.
Diffstat (limited to 'tests/micropython')
-rw-r--r--tests/micropython/const.py6
-rw-r--r--tests/micropython/const2.py22
-rw-r--r--tests/micropython/const_error.py2
-rw-r--r--tests/micropython/const_intbig.py4
-rw-r--r--tests/micropython/decorator.py4
-rw-r--r--tests/micropython/decorator_error.py2
-rw-r--r--tests/micropython/emg_exc.py3
-rw-r--r--tests/micropython/emg_exc.py.exp2
-rw-r--r--tests/micropython/extreme_exc.py93
-rw-r--r--tests/micropython/heap_lock.py6
-rw-r--r--tests/micropython/heapalloc.py27
-rw-r--r--tests/micropython/heapalloc_bytesio2.py1
-rw-r--r--tests/micropython/heapalloc_exc_raise.py2
-rw-r--r--tests/micropython/heapalloc_fail_bytearray.py31
-rw-r--r--tests/micropython/heapalloc_fail_dict.py8
-rw-r--r--tests/micropython/heapalloc_fail_list.py9
-rw-r--r--tests/micropython/heapalloc_fail_memoryview.py11
-rw-r--r--tests/micropython/heapalloc_fail_set.py4
-rw-r--r--tests/micropython/heapalloc_fail_tuple.py2
-rw-r--r--tests/micropython/heapalloc_inst_call.py5
-rw-r--r--tests/micropython/heapalloc_iter.py17
-rw-r--r--tests/micropython/heapalloc_super.py17
-rw-r--r--tests/micropython/heapalloc_traceback.py5
-rw-r--r--tests/micropython/heapalloc_traceback.py.exp2
-rw-r--r--tests/micropython/heapalloc_yield_from.py8
-rw-r--r--tests/micropython/kbd_intr.py2
-rw-r--r--tests/micropython/meminfo.py4
-rw-r--r--tests/micropython/memstats.py4
-rw-r--r--tests/micropython/native_closure.py11
-rw-r--r--tests/micropython/native_const.py9
-rw-r--r--tests/micropython/native_const_intbig.py2
-rw-r--r--tests/micropython/native_gen.py4
-rw-r--r--tests/micropython/native_misc.py9
-rw-r--r--tests/micropython/native_try.py14
-rw-r--r--tests/micropython/native_try_deep.py4
-rw-r--r--tests/micropython/native_with.py16
-rw-r--r--tests/micropython/opt_level.py6
-rw-r--r--tests/micropython/opt_level_lineno.py2
-rw-r--r--tests/micropython/schedule.py17
-rw-r--r--tests/micropython/stack_use.py6
-rw-r--r--tests/micropython/viper_addr.py20
-rw-r--r--tests/micropython/viper_args.py35
-rw-r--r--tests/micropython/viper_binop_arith.py42
-rw-r--r--tests/micropython/viper_binop_comp.py3
-rw-r--r--tests/micropython/viper_binop_comp_imm.py1
-rw-r--r--tests/micropython/viper_binop_divmod.py8
-rw-r--r--tests/micropython/viper_binop_multi_comp.py3
-rw-r--r--tests/micropython/viper_cond.py8
-rw-r--r--tests/micropython/viper_const.py9
-rw-r--r--tests/micropython/viper_const_intbig.py2
-rw-r--r--tests/micropython/viper_error.py20
-rw-r--r--tests/micropython/viper_globals.py15
-rw-r--r--tests/micropython/viper_import.py5
-rw-r--r--tests/micropython/viper_misc.py59
-rw-r--r--tests/micropython/viper_misc_intbig.py3
-rw-r--r--tests/micropython/viper_ptr16_load.py13
-rw-r--r--tests/micropython/viper_ptr16_store.py13
-rw-r--r--tests/micropython/viper_ptr32_load.py13
-rw-r--r--tests/micropython/viper_ptr32_store.py13
-rw-r--r--tests/micropython/viper_ptr8_load.py13
-rw-r--r--tests/micropython/viper_ptr8_store.py13
-rw-r--r--tests/micropython/viper_subscr.py7
-rw-r--r--tests/micropython/viper_try.py15
-rw-r--r--tests/micropython/viper_types.py10
-rw-r--r--tests/micropython/viper_with.py16
65 files changed, 577 insertions, 185 deletions
diff --git a/tests/micropython/const.py b/tests/micropython/const.py
index 660a095f2..1faf22be9 100644
--- a/tests/micropython/const.py
+++ b/tests/micropython/const.py
@@ -7,9 +7,11 @@ Y = const(X + 456)
print(X, Y + 1)
+
def f():
print(X, Y + 1)
+
f()
_X = const(12)
@@ -17,9 +19,11 @@ _Y = const(_X + 34)
print(_X, _Y)
+
class A:
Z = const(1)
_Z = const(2)
print(Z, _Z)
-print(hasattr(A, 'Z'), hasattr(A, '_Z'))
+
+print(hasattr(A, "Z"), hasattr(A, "_Z"))
diff --git a/tests/micropython/const2.py b/tests/micropython/const2.py
index 60085a1e0..ed4720122 100644
--- a/tests/micropython/const2.py
+++ b/tests/micropython/const2.py
@@ -8,27 +8,37 @@ Z = const(3)
# import that uses a constant
import micropython as X
-print(globals()['X'])
+
+print(globals()["X"])
# function name that matches a constant
def X():
- print('function X', X)
-globals()['X']()
+ print("function X", X)
+
+
+globals()["X"]()
# arguments that match a constant
def f(X, *Y, **Z):
pass
+
+
f(1)
# class name that matches a constant
class X:
def f(self):
- print('class X', X)
-globals()['X']().f()
+ print("class X", X)
+
+
+globals()["X"]().f()
# constant within a class
class A:
C1 = const(4)
+
def X(self):
- print('method X', Y, C1, self.C1)
+ print("method X", Y, C1, self.C1)
+
+
A().X()
diff --git a/tests/micropython/const_error.py b/tests/micropython/const_error.py
index 6d3d135b5..311cfb4d5 100644
--- a/tests/micropython/const_error.py
+++ b/tests/micropython/const_error.py
@@ -2,12 +2,14 @@
from micropython import const
+
def test_syntax(code):
try:
exec(code)
except SyntaxError:
print("SyntaxError")
+
# argument not a constant
test_syntax("a = const(x)")
diff --git a/tests/micropython/const_intbig.py b/tests/micropython/const_intbig.py
index e74902652..27990c8c2 100644
--- a/tests/micropython/const_intbig.py
+++ b/tests/micropython/const_intbig.py
@@ -3,8 +3,8 @@
from micropython import const
# check we can make consts from bignums
-Z1 = const(0xffffffff)
-Z2 = const(0xffffffffffffffff)
+Z1 = const(0xFFFFFFFF)
+Z2 = const(0xFFFFFFFFFFFFFFFF)
print(hex(Z1), hex(Z2))
# check arithmetic with bignum
diff --git a/tests/micropython/decorator.py b/tests/micropython/decorator.py
index bf688968a..2e7cf1777 100644
--- a/tests/micropython/decorator.py
+++ b/tests/micropython/decorator.py
@@ -1,7 +1,9 @@
# test micropython-specific decorators
+
@micropython.bytecode
def f():
- return 'bytecode'
+ return "bytecode"
+
print(f())
diff --git a/tests/micropython/decorator_error.py b/tests/micropython/decorator_error.py
index c7da3119f..94772ac1e 100644
--- a/tests/micropython/decorator_error.py
+++ b/tests/micropython/decorator_error.py
@@ -1,11 +1,13 @@
# test syntax errors for uPy-specific decorators
+
def test_syntax(code):
try:
exec(code)
except SyntaxError:
print("SyntaxError")
+
# invalid micropython decorators
test_syntax("@micropython.a\ndef f(): pass")
test_syntax("@micropython.a.b\ndef f(): pass")
diff --git a/tests/micropython/emg_exc.py b/tests/micropython/emg_exc.py
index 4a9fa18bc..bca4d2d9f 100644
--- a/tests/micropython/emg_exc.py
+++ b/tests/micropython/emg_exc.py
@@ -2,6 +2,7 @@
import micropython
import sys
+
try:
import uio
except ImportError:
@@ -14,6 +15,7 @@ try:
except AttributeError:
pass
+
def f():
micropython.heap_lock()
try:
@@ -31,4 +33,5 @@ def f():
else:
print(l)
+
f()
diff --git a/tests/micropython/emg_exc.py.exp b/tests/micropython/emg_exc.py.exp
index fd2cfb272..0d4b80ce2 100644
--- a/tests/micropython/emg_exc.py.exp
+++ b/tests/micropython/emg_exc.py.exp
@@ -1,4 +1,4 @@
Traceback (most recent call last):
-, line 20, in f
+, line 22, in f
ValueError: 1
diff --git a/tests/micropython/extreme_exc.py b/tests/micropython/extreme_exc.py
index b9db96406..dae5b1518 100644
--- a/tests/micropython/extreme_exc.py
+++ b/tests/micropython/extreme_exc.py
@@ -5,8 +5,13 @@ import micropython
# Check for stackless build, which can't call functions without
# allocating a frame on the heap.
try:
- def stackless(): pass
- micropython.heap_lock(); stackless(); micropython.heap_unlock()
+
+ def stackless():
+ pass
+
+ micropython.heap_lock()
+ stackless()
+ micropython.heap_unlock()
except RuntimeError:
print("SKIP")
raise SystemExit
@@ -17,11 +22,78 @@ try:
except AttributeError:
pass
+
def main():
# create an exception with many args while heap is locked
# should revert to empty tuple for args
micropython.heap_lock()
- e = Exception(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
+ e = Exception(
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ )
micropython.heap_unlock()
print(repr(e))
@@ -29,9 +101,12 @@ def main():
# should use emergency exception buffer and truncate the message
def f():
pass
+
micropython.heap_lock()
try:
- f(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1)
+ f(
+ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1
+ )
except Exception as er:
e = er
micropython.heap_unlock()
@@ -46,7 +121,9 @@ def main():
except MemoryError:
break
try:
- f(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1)
+ f(
+ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1
+ )
except Exception as er:
e = er
lst[0] = None
@@ -57,6 +134,7 @@ def main():
# should use emergency exception and be unable to resize traceback array
def g():
g()
+
micropython.heap_lock()
try:
g()
@@ -67,13 +145,15 @@ def main():
# create an exception on the heap with some traceback on the heap, but then
# raise it with the heap locked so it can't allocate any more traceback
- exc = Exception('my exception')
+ exc = Exception("my exception")
try:
raise exc
except:
pass
+
def h(e):
raise e
+
micropython.heap_lock()
try:
h(exc)
@@ -82,4 +162,5 @@ def main():
micropython.heap_unlock()
print(repr(e))
+
main()
diff --git a/tests/micropython/heap_lock.py b/tests/micropython/heap_lock.py
index 6d770d9ec..f2892a6dc 100644
--- a/tests/micropython/heap_lock.py
+++ b/tests/micropython/heap_lock.py
@@ -12,13 +12,13 @@ micropython.heap_lock()
try:
print([])
except MemoryError:
- print('MemoryError')
+ print("MemoryError")
# expansion of a heap block
try:
l.extend(l2)
except MemoryError:
- print('MemoryError')
+ print("MemoryError")
print(micropython.heap_unlock())
@@ -26,7 +26,7 @@ print(micropython.heap_unlock())
try:
print([])
except MemoryError:
- print('MemoryError')
+ print("MemoryError")
micropython.heap_unlock()
diff --git a/tests/micropython/heapalloc.py b/tests/micropython/heapalloc.py
index f74bb92c8..99f157105 100644
--- a/tests/micropython/heapalloc.py
+++ b/tests/micropython/heapalloc.py
@@ -5,18 +5,26 @@ import micropython
# Check for stackless build, which can't call functions without
# allocating a frame on heap.
try:
- def stackless(): pass
- micropython.heap_lock(); stackless(); micropython.heap_unlock()
+
+ def stackless():
+ pass
+
+ micropython.heap_lock()
+ stackless()
+ micropython.heap_unlock()
except RuntimeError:
print("SKIP")
raise SystemExit
+
def f1(a):
print(a)
+
def f2(a, b=2):
print(a, b)
+
def f3(a, b, c, d):
x1 = x2 = a
x3 = x4 = b
@@ -24,19 +32,22 @@ def f3(a, b, c, d):
x7 = x8 = d
print(x1, x3, x5, x7, x2 + x4 + x6 + x8)
+
global_var = 1
+
def test():
global global_var, global_exc
- global_var = 2 # set an existing global variable
+ global_var = 2 # set an existing global variable
for i in range(2): # for loop
- f1(i) # function call
- f1(i * 2 + 1) # binary operation with small ints
- f1(a=i) # keyword arguments
- f2(i) # default arg (second one)
- f2(i, i) # 2 args
+ f1(i) # function call
+ f1(i * 2 + 1) # binary operation with small ints
+ f1(a=i) # keyword arguments
+ f2(i) # default arg (second one)
+ f2(i, i) # 2 args
f3(1, 2, 3, 4) # function with lots of local state
+
# call test() with heap allocation disabled
micropython.heap_lock()
test()
diff --git a/tests/micropython/heapalloc_bytesio2.py b/tests/micropython/heapalloc_bytesio2.py
index cd76f5807..3b9f14127 100644
--- a/tests/micropython/heapalloc_bytesio2.py
+++ b/tests/micropython/heapalloc_bytesio2.py
@@ -3,6 +3,7 @@
try:
import uio
import micropython
+
micropython.mem_total
except (ImportError, AttributeError):
print("SKIP")
diff --git a/tests/micropython/heapalloc_exc_raise.py b/tests/micropython/heapalloc_exc_raise.py
index fb63a84bf..99810e007 100644
--- a/tests/micropython/heapalloc_exc_raise.py
+++ b/tests/micropython/heapalloc_exc_raise.py
@@ -4,6 +4,7 @@ import micropython
e = ValueError("error")
+
def func():
micropython.heap_lock()
try:
@@ -19,5 +20,6 @@ def func():
print(e2)
micropython.heap_unlock()
+
func()
print("ok")
diff --git a/tests/micropython/heapalloc_fail_bytearray.py b/tests/micropython/heapalloc_fail_bytearray.py
index fbf585c7f..1bf7ddd60 100644
--- a/tests/micropython/heapalloc_fail_bytearray.py
+++ b/tests/micropython/heapalloc_fail_bytearray.py
@@ -2,9 +2,12 @@
import micropython
+
class GetSlice:
def __getitem__(self, idx):
return idx
+
+
sl = GetSlice()[:]
# create bytearray
@@ -12,15 +15,15 @@ micropython.heap_lock()
try:
bytearray(4)
except MemoryError:
- print('MemoryError: bytearray create')
+ print("MemoryError: bytearray create")
micropython.heap_unlock()
# create bytearray from bytes
micropython.heap_lock()
try:
- bytearray(b'0123')
+ bytearray(b"0123")
except MemoryError:
- print('MemoryError: bytearray create from bytes')
+ print("MemoryError: bytearray create from bytes")
micropython.heap_unlock()
# create bytearray from iterator
@@ -29,25 +32,25 @@ micropython.heap_lock()
try:
bytearray(r)
except MemoryError:
- print('MemoryError: bytearray create from iter')
+ print("MemoryError: bytearray create from iter")
micropython.heap_unlock()
# bytearray add
b = bytearray(4)
micropython.heap_lock()
try:
- b + b'01'
+ b + b"01"
except MemoryError:
- print('MemoryError: bytearray.__add__')
+ print("MemoryError: bytearray.__add__")
micropython.heap_unlock()
# bytearray iadd
b = bytearray(4)
micropython.heap_lock()
try:
- b += b'01234567'
+ b += b"01234567"
except MemoryError:
- print('MemoryError: bytearray.__iadd__')
+ print("MemoryError: bytearray.__iadd__")
micropython.heap_unlock()
print(b)
@@ -58,16 +61,16 @@ try:
for i in range(100):
b.append(1)
except MemoryError:
- print('MemoryError: bytearray.append')
+ print("MemoryError: bytearray.append")
micropython.heap_unlock()
# bytearray extend
b = bytearray(4)
micropython.heap_lock()
try:
- b.extend(b'01234567')
+ b.extend(b"01234567")
except MemoryError:
- print('MemoryError: bytearray.extend')
+ print("MemoryError: bytearray.extend")
micropython.heap_unlock()
# bytearray get with slice
@@ -76,15 +79,15 @@ micropython.heap_lock()
try:
b[sl]
except MemoryError:
- print('MemoryError: bytearray subscr get')
+ print("MemoryError: bytearray subscr get")
micropython.heap_unlock()
# extend bytearray using slice subscr
b = bytearray(4)
micropython.heap_lock()
try:
- b[sl] = b'01234567'
+ b[sl] = b"01234567"
except MemoryError:
- print('MemoryError: bytearray subscr grow')
+ print("MemoryError: bytearray subscr grow")
micropython.heap_unlock()
print(b)
diff --git a/tests/micropython/heapalloc_fail_dict.py b/tests/micropython/heapalloc_fail_dict.py
index ba872bfeb..ce2d158bd 100644
--- a/tests/micropython/heapalloc_fail_dict.py
+++ b/tests/micropython/heapalloc_fail_dict.py
@@ -6,16 +6,16 @@ import micropython
x = 1
micropython.heap_lock()
try:
- {x:x}
+ {x: x}
except MemoryError:
- print('MemoryError: create dict')
+ print("MemoryError: create dict")
micropython.heap_unlock()
# create dict view
-x = {1:1}
+x = {1: 1}
micropython.heap_lock()
try:
x.items()
except MemoryError:
- print('MemoryError: dict.items')
+ print("MemoryError: dict.items")
micropython.heap_unlock()
diff --git a/tests/micropython/heapalloc_fail_list.py b/tests/micropython/heapalloc_fail_list.py
index a54bdb6cf..9a2e9a555 100644
--- a/tests/micropython/heapalloc_fail_list.py
+++ b/tests/micropython/heapalloc_fail_list.py
@@ -2,9 +2,12 @@
import micropython
+
class GetSlice:
def __getitem__(self, idx):
return idx
+
+
sl = GetSlice()[:]
# create slice in VM
@@ -13,7 +16,7 @@ micropython.heap_lock()
try:
print(l[0:1])
except MemoryError:
- print('MemoryError: list index')
+ print("MemoryError: list index")
micropython.heap_unlock()
# get from list using slice
@@ -21,7 +24,7 @@ micropython.heap_lock()
try:
l[sl]
except MemoryError:
- print('MemoryError: list get slice')
+ print("MemoryError: list get slice")
micropython.heap_unlock()
# extend list using slice subscr
@@ -31,6 +34,6 @@ micropython.heap_lock()
try:
l[sl] = l2
except MemoryError:
- print('MemoryError: list extend slice')
+ print("MemoryError: list extend slice")
micropython.heap_unlock()
print(l)
diff --git a/tests/micropython/heapalloc_fail_memoryview.py b/tests/micropython/heapalloc_fail_memoryview.py
index 3ba9015ff..da2d1abff 100644
--- a/tests/micropython/heapalloc_fail_memoryview.py
+++ b/tests/micropython/heapalloc_fail_memoryview.py
@@ -2,24 +2,27 @@
import micropython
+
class GetSlice:
def __getitem__(self, idx):
return idx
+
+
sl = GetSlice()[:]
# create memoryview
micropython.heap_lock()
try:
- memoryview(b'')
+ memoryview(b"")
except MemoryError:
- print('MemoryError: memoryview create')
+ print("MemoryError: memoryview create")
micropython.heap_unlock()
# memoryview get with slice
-m = memoryview(b'')
+m = memoryview(b"")
micropython.heap_lock()
try:
m[sl]
except MemoryError:
- print('MemoryError: memoryview subscr get')
+ print("MemoryError: memoryview subscr get")
micropython.heap_unlock()
diff --git a/tests/micropython/heapalloc_fail_set.py b/tests/micropython/heapalloc_fail_set.py
index 172df27d4..3c347660a 100644
--- a/tests/micropython/heapalloc_fail_set.py
+++ b/tests/micropython/heapalloc_fail_set.py
@@ -8,7 +8,7 @@ micropython.heap_lock()
try:
{x}
except MemoryError:
- print('MemoryError: set create')
+ print("MemoryError: set create")
micropython.heap_unlock()
# set copy
@@ -17,5 +17,5 @@ micropython.heap_lock()
try:
s.copy()
except MemoryError:
- print('MemoryError: set copy')
+ print("MemoryError: set copy")
micropython.heap_unlock()
diff --git a/tests/micropython/heapalloc_fail_tuple.py b/tests/micropython/heapalloc_fail_tuple.py
index 1cd23fb74..de79385e3 100644
--- a/tests/micropython/heapalloc_fail_tuple.py
+++ b/tests/micropython/heapalloc_fail_tuple.py
@@ -8,5 +8,5 @@ micropython.heap_lock()
try:
(x,)
except MemoryError:
- print('MemoryError: tuple create')
+ print("MemoryError: tuple create")
micropython.heap_unlock()
diff --git a/tests/micropython/heapalloc_inst_call.py b/tests/micropython/heapalloc_inst_call.py
index 3cc497b73..14d8826bf 100644
--- a/tests/micropython/heapalloc_inst_call.py
+++ b/tests/micropython/heapalloc_inst_call.py
@@ -2,22 +2,27 @@
# doesn't require heap allocation.
import micropython
+
class Foo0:
def __call__(self):
print("__call__")
+
class Foo1:
def __call__(self, a):
print("__call__", a)
+
class Foo2:
def __call__(self, a, b):
print("__call__", a, b)
+
class Foo3:
def __call__(self, a, b, c):
print("__call__", a, b, c)
+
f0 = Foo0()
f1 = Foo1()
f2 = Foo2()
diff --git a/tests/micropython/heapalloc_iter.py b/tests/micropython/heapalloc_iter.py
index 5a44a558b..18f5322ee 100644
--- a/tests/micropython/heapalloc_iter.py
+++ b/tests/micropython/heapalloc_iter.py
@@ -16,7 +16,8 @@ except ImportError:
try:
from micropython import heap_lock, heap_unlock
except (ImportError, AttributeError):
- heap_lock = heap_unlock = lambda:0
+ heap_lock = heap_unlock = lambda: 0
+
def do_iter(l):
heap_lock()
@@ -24,16 +25,18 @@ def do_iter(l):
print(i)
heap_unlock()
+
def gen_func():
yield 1
yield 2
+
# pre-create collections to iterate over
-ba = bytearray(b'123')
-ar = array.array('H', (123, 456))
+ba = bytearray(b"123")
+ar = array.array("H", (123, 456))
t = (1, 2, 3)
l = [1, 2]
-d = {1:2}
+d = {1: 2}
s = set((1,))
fs = frozenset((1,))
g1 = (100 + x for x in range(2))
@@ -41,7 +44,7 @@ g2 = gen_func()
# test containment (both success and failure) with the heap locked
heap_lock()
-print(49 in b'123', 255 in b'123')
+print(49 in b"123", 255 in b"123")
print(1 in t, -1 in t)
print(1 in l, -1 in l)
print(1 in d, -1 in d)
@@ -49,7 +52,7 @@ print(1 in s, -1 in s)
heap_unlock()
# test unpacking with the heap locked
-unp0 = unp1 = unp2 = None # preallocate slots for globals
+unp0 = unp1 = unp2 = None # preallocate slots for globals
heap_lock()
unp0, unp1, unp2 = t
print(unp0, unp1, unp2)
@@ -65,7 +68,7 @@ print(sum(t))
heap_unlock()
# test iterating over collections with the heap locked
-do_iter(b'123')
+do_iter(b"123")
do_iter(ba)
do_iter(ar)
do_iter(t)
diff --git a/tests/micropython/heapalloc_super.py b/tests/micropython/heapalloc_super.py
index a8c23393e..51afae3d8 100644
--- a/tests/micropython/heapalloc_super.py
+++ b/tests/micropython/heapalloc_super.py
@@ -4,21 +4,30 @@ import micropython
# Check for stackless build, which can't call functions without
# allocating a frame on heap.
try:
- def stackless(): pass
- micropython.heap_lock(); stackless(); micropython.heap_unlock()
+
+ def stackless():
+ pass
+
+ micropython.heap_lock()
+ stackless()
+ micropython.heap_unlock()
except RuntimeError:
print("SKIP")
raise SystemExit
+
class A:
def foo(self):
- print('A foo')
+ print("A foo")
return 42
+
+
class B(A):
def foo(self):
- print('B foo')
+ print("B foo")
print(super().foo())
+
b = B()
micropython.heap_lock()
diff --git a/tests/micropython/heapalloc_traceback.py b/tests/micropython/heapalloc_traceback.py
index 813dea4b2..eabd09388 100644
--- a/tests/micropython/heapalloc_traceback.py
+++ b/tests/micropython/heapalloc_traceback.py
@@ -2,6 +2,7 @@
import micropython
import sys
+
try:
import uio
except ImportError:
@@ -15,6 +16,7 @@ try:
except:
pass
+
def test():
micropython.heap_lock()
global global_exc
@@ -22,9 +24,10 @@ def test():
try:
raise global_exc
except StopIteration:
- print('StopIteration')
+ print("StopIteration")
micropython.heap_unlock()
+
# call test() with heap allocation disabled
test()
diff --git a/tests/micropython/heapalloc_traceback.py.exp b/tests/micropython/heapalloc_traceback.py.exp
index facd0af13..71929db93 100644
--- a/tests/micropython/heapalloc_traceback.py.exp
+++ b/tests/micropython/heapalloc_traceback.py.exp
@@ -1,5 +1,5 @@
StopIteration
Traceback (most recent call last):
- File , line 23, in test
+ File , line 25, in test
StopIteration:
diff --git a/tests/micropython/heapalloc_yield_from.py b/tests/micropython/heapalloc_yield_from.py
index 8443210f3..58788b1db 100644
--- a/tests/micropython/heapalloc_yield_from.py
+++ b/tests/micropython/heapalloc_yield_from.py
@@ -6,8 +6,12 @@ import micropython
def sub_gen(a):
for i in range(a):
yield i
+
+
def gen(g):
yield from g
+
+
g = gen(sub_gen(4))
micropython.heap_lock()
print(next(g))
@@ -18,12 +22,16 @@ micropython.heap_unlock()
class G:
def __init__(self):
self.value = 0
+
def __iter__(self):
return self
+
def __next__(self):
v = self.value
self.value += 1
return v
+
+
g = gen(G())
micropython.heap_lock()
print(next(g))
diff --git a/tests/micropython/kbd_intr.py b/tests/micropython/kbd_intr.py
index 879c9a229..81977aaa5 100644
--- a/tests/micropython/kbd_intr.py
+++ b/tests/micropython/kbd_intr.py
@@ -5,7 +5,7 @@ import micropython
try:
micropython.kbd_intr
except AttributeError:
- print('SKIP')
+ print("SKIP")
raise SystemExit
# just check we can actually call it
diff --git a/tests/micropython/meminfo.py b/tests/micropython/meminfo.py
index 698bbbd21..9df341fbb 100644
--- a/tests/micropython/meminfo.py
+++ b/tests/micropython/meminfo.py
@@ -3,8 +3,8 @@
import micropython
# these functions are not always available
-if not hasattr(micropython, 'mem_info'):
- print('SKIP')
+if not hasattr(micropython, "mem_info"):
+ print("SKIP")
else:
micropython.mem_info()
micropython.mem_info(1)
diff --git a/tests/micropython/memstats.py b/tests/micropython/memstats.py
index 78e4d2473..dee3a4ce2 100644
--- a/tests/micropython/memstats.py
+++ b/tests/micropython/memstats.py
@@ -3,8 +3,8 @@
import micropython
# these functions are not always available
-if not hasattr(micropython, 'mem_total'):
- print('SKIP')
+if not hasattr(micropython, "mem_total"):
+ print("SKIP")
else:
t = micropython.mem_total()
c = micropython.mem_current()
diff --git a/tests/micropython/native_closure.py b/tests/micropython/native_closure.py
index 6c0592e52..07014e90d 100644
--- a/tests/micropython/native_closure.py
+++ b/tests/micropython/native_closure.py
@@ -4,11 +4,15 @@
@micropython.native
def f():
x = 1
+
@micropython.native
def g():
nonlocal x
return x
+
return g
+
+
print(f()())
# closing over an argument
@@ -18,15 +22,22 @@ def f(x):
def g():
nonlocal x
return x
+
return g
+
+
print(f(2)())
# closing over an argument and a normal local
@micropython.native
def f(x):
y = 2 * x
+
@micropython.native
def g(z):
return x + y + z
+
return g
+
+
print(f(2)(3))
diff --git a/tests/micropython/native_const.py b/tests/micropython/native_const.py
index 37b491cf4..b48499550 100644
--- a/tests/micropython/native_const.py
+++ b/tests/micropython/native_const.py
@@ -1,14 +1,21 @@
# test loading constants in native functions
+
@micropython.native
def f():
- return b'bytes'
+ return b"bytes"
+
+
print(f())
+
@micropython.native
def f():
@micropython.native
def g():
return 123
+
return g
+
+
print(f()())
diff --git a/tests/micropython/native_const_intbig.py b/tests/micropython/native_const_intbig.py
index 611b39d8f..69bc1d216 100644
--- a/tests/micropython/native_const_intbig.py
+++ b/tests/micropython/native_const_intbig.py
@@ -1,7 +1,9 @@
# check loading constants
+
@micropython.native
def f():
return 123456789012345678901234567890
+
print(f())
diff --git a/tests/micropython/native_gen.py b/tests/micropython/native_gen.py
index 30c4c37be..fb42f9e25 100644
--- a/tests/micropython/native_gen.py
+++ b/tests/micropython/native_gen.py
@@ -6,6 +6,8 @@ def gen1(x):
yield x
yield x + 1
return x + 2
+
+
g = gen1(3)
print(next(g))
print(next(g))
@@ -18,4 +20,6 @@ except StopIteration as e:
@micropython.native
def gen2(x):
yield from range(x)
+
+
print(list(gen2(3)))
diff --git a/tests/micropython/native_misc.py b/tests/micropython/native_misc.py
index 0cd521de6..7c5415375 100644
--- a/tests/micropython/native_misc.py
+++ b/tests/micropython/native_misc.py
@@ -4,10 +4,13 @@
@micropython.native
def native_test(x):
print(1, [], x)
+
+
native_test(2)
# check that GC doesn't collect the native function
import gc
+
gc.collect()
native_test(3)
@@ -15,17 +18,23 @@ native_test(3)
@micropython.native
def f(a, b):
print(a + b)
+
+
f(1, 2)
# native with 3 args
@micropython.native
def f(a, b, c):
print(a + b + c)
+
+
f(1, 2, 3)
# check not operator
@micropython.native
def f(a):
print(not a)
+
+
f(False)
f(True)
diff --git a/tests/micropython/native_try.py b/tests/micropython/native_try.py
index 2e41bf2ea..492b59085 100644
--- a/tests/micropython/native_try.py
+++ b/tests/micropython/native_try.py
@@ -6,11 +6,13 @@ def f():
try:
fail
finally:
- print('finally')
+ print("finally")
+
+
try:
f()
except NameError:
- print('NameError')
+ print("NameError")
# nested try-except with try-finally
@micropython.native
@@ -19,9 +21,11 @@ def f():
try:
fail
finally:
- print('finally')
+ print("finally")
except NameError:
- print('NameError')
+ print("NameError")
+
+
f()
# check that locals written to in try blocks keep their values
@@ -36,4 +40,6 @@ def f():
print(a)
a = 300
print(a)
+
+
f()
diff --git a/tests/micropython/native_try_deep.py b/tests/micropython/native_try_deep.py
index 7fac4f0f3..3d31248df 100644
--- a/tests/micropython/native_try_deep.py
+++ b/tests/micropython/native_try_deep.py
@@ -30,5 +30,7 @@ def f():
finally:
print(1)
except ValueError:
- print('ValueError')
+ print("ValueError")
+
+
f()
diff --git a/tests/micropython/native_with.py b/tests/micropython/native_with.py
index 343f3e8d3..4e20b2385 100644
--- a/tests/micropython/native_with.py
+++ b/tests/micropython/native_with.py
@@ -1,18 +1,24 @@
# test with handling within a native function
+
class C:
def __init__(self):
- print('__init__')
+ print("__init__")
+
def __enter__(self):
- print('__enter__')
+ print("__enter__")
+
def __exit__(self, a, b, c):
- print('__exit__', a, b, c)
+ print("__exit__", a, b, c)
+
# basic with
@micropython.native
def f():
with C():
print(1)
+
+
f()
# nested with and try-except
@@ -24,5 +30,7 @@ def f():
fail
print(2)
except NameError:
- print('NameError')
+ print("NameError")
+
+
f()
diff --git a/tests/micropython/opt_level.py b/tests/micropython/opt_level.py
index 4e2f2f4ea..dd5493a7a 100644
--- a/tests/micropython/opt_level.py
+++ b/tests/micropython/opt_level.py
@@ -8,7 +8,7 @@ print(micropython.opt_level())
# check that the optimisation levels actually differ
micropython.opt_level(0)
-exec('print(__debug__)')
+exec("print(__debug__)")
micropython.opt_level(1)
-exec('print(__debug__)')
-exec('assert 0')
+exec("print(__debug__)")
+exec("assert 0")
diff --git a/tests/micropython/opt_level_lineno.py b/tests/micropython/opt_level_lineno.py
index 00e573960..d8253e54b 100644
--- a/tests/micropython/opt_level_lineno.py
+++ b/tests/micropython/opt_level_lineno.py
@@ -3,4 +3,4 @@ import micropython as micropython
# check that level 3 doesn't store line numbers
# the expected output is that any line is printed as "line 1"
micropython.opt_level(3)
-exec('try:\n xyz\nexcept NameError as er:\n import sys\n sys.print_exception(er)')
+exec("try:\n xyz\nexcept NameError as er:\n import sys\n sys.print_exception(er)")
diff --git a/tests/micropython/schedule.py b/tests/micropython/schedule.py
index 74f90cb2d..6a91459ea 100644
--- a/tests/micropython/schedule.py
+++ b/tests/micropython/schedule.py
@@ -5,16 +5,18 @@ import micropython
try:
micropython.schedule
except AttributeError:
- print('SKIP')
+ print("SKIP")
raise SystemExit
# Basic test of scheduling a function.
+
def callback(arg):
global done
print(arg)
done = True
+
done = False
micropython.schedule(callback, 1)
while not done:
@@ -23,20 +25,23 @@ while not done:
# Test that callbacks can be scheduled from within a callback, but
# that they don't execute until the outer callback is finished.
+
def callback_inner(arg):
global done
- print('inner')
+ print("inner")
done += 1
+
def callback_outer(arg):
global done
micropython.schedule(callback_inner, 0)
# need a loop so that the VM can check for pending events
for i in range(2):
pass
- print('outer')
+ print("outer")
done += 1
+
done = 0
micropython.schedule(callback_outer, 0)
while done != 2:
@@ -45,15 +50,17 @@ while done != 2:
# Test that scheduling too many callbacks leads to an exception. To do this we
# must schedule from within a callback to guarantee that the scheduler is locked.
+
def callback(arg):
global done
try:
for i in range(100):
- micropython.schedule(lambda x:x, None)
+ micropython.schedule(lambda x: x, None)
except RuntimeError:
- print('RuntimeError')
+ print("RuntimeError")
done = True
+
done = False
micropython.schedule(callback, None)
while not done:
diff --git a/tests/micropython/stack_use.py b/tests/micropython/stack_use.py
index bc714755a..266885d9d 100644
--- a/tests/micropython/stack_use.py
+++ b/tests/micropython/stack_use.py
@@ -1,7 +1,7 @@
# tests stack_use function in micropython module
import micropython
-if not hasattr(micropython, 'stack_use'):
- print('SKIP')
+if not hasattr(micropython, "stack_use"):
+ print("SKIP")
else:
- print(type(micropython.stack_use())) # output varies
+ print(type(micropython.stack_use())) # output varies
diff --git a/tests/micropython/viper_addr.py b/tests/micropython/viper_addr.py
index 0d8efb90b..84bc6c002 100644
--- a/tests/micropython/viper_addr.py
+++ b/tests/micropython/viper_addr.py
@@ -1,39 +1,43 @@
# test passing addresses to viper
+
@micropython.viper
-def get_addr(x:ptr) -> ptr:
+def get_addr(x: ptr) -> ptr:
return x
+
@micropython.viper
-def memset(dest:ptr8, c:int, n:int):
+def memset(dest: ptr8, c: int, n: int):
for i in range(n):
dest[i] = c
+
@micropython.viper
-def memsum(src:ptr8, n:int) -> int:
+def memsum(src: ptr8, n: int) -> int:
s = 0
for i in range(n):
s += src[i]
return s
+
# create array and get its address
-ar = bytearray('0000')
+ar = bytearray("0000")
addr = get_addr(ar)
print(type(ar))
print(type(addr))
print(ar)
# pass array as an object
-memset(ar, ord('1'), len(ar))
+memset(ar, ord("1"), len(ar))
print(ar)
# pass direct pointer to array buffer
-memset(addr, ord('2'), len(ar))
+memset(addr, ord("2"), len(ar))
print(ar)
# pass direct pointer to array buffer, with offset
-memset(addr + 2, ord('3'), len(ar) - 2)
+memset(addr + 2, ord("3"), len(ar) - 2)
print(ar)
# pass a read-only bytes object in
-print(memsum(b'\x01\x02\x03\x04', 4))
+print(memsum(b"\x01\x02\x03\x04", 4))
diff --git a/tests/micropython/viper_args.py b/tests/micropython/viper_args.py
index ee8e82321..8e3225331 100644
--- a/tests/micropython/viper_args.py
+++ b/tests/micropython/viper_args.py
@@ -1,44 +1,67 @@
# test calling viper functions with different number of args
+
@micropython.viper
def f0():
print(0)
+
+
f0()
+
@micropython.viper
-def f1(x1:int):
+def f1(x1: int):
print(x1)
+
+
f1(1)
+
@micropython.viper
-def f2(x1:int, x2:int):
+def f2(x1: int, x2: int):
print(x1, x2)
+
+
f2(1, 2)
+
@micropython.viper
-def f3(x1:int, x2:int, x3:int):
+def f3(x1: int, x2: int, x3: int):
print(x1, x2, x3)
+
+
f3(1, 2, 3)
+
@micropython.viper
-def f4(x1:int, x2:int, x3:int, x4:int):
+def f4(x1: int, x2: int, x3: int, x4: int):
print(x1, x2, x3, x4)
+
+
f4(1, 2, 3, 4)
+
@micropython.viper
-def f5(x1:int, x2:int, x3:int, x4:int, x5:int):
+def f5(x1: int, x2: int, x3: int, x4: int, x5: int):
print(x1, x2, x3, x4, x5)
+
+
f5(1, 2, 3, 4, 5)
+
@micropython.viper
-def f6(x1:int, x2:int, x3:int, x4:int, x5:int, x6:int):
+def f6(x1: int, x2: int, x3: int, x4: int, x5: int, x6: int):
print(x1, x2, x3, x4, x5, x6)
+
+
f6(1, 2, 3, 4, 5, 6)
# test compiling *x, **x, * args (currently unsupported at runtime)
@micropython.viper
def f(*x, **y):
pass
+
+
@micropython.viper
def f(*):
pass
diff --git a/tests/micropython/viper_binop_arith.py b/tests/micropython/viper_binop_arith.py
index 4d711f1a9..2691404b7 100644
--- a/tests/micropython/viper_binop_arith.py
+++ b/tests/micropython/viper_binop_arith.py
@@ -1,27 +1,36 @@
# test arithmetic operators
+
@micropython.viper
-def add(x:int, y:int):
+def add(x: int, y: int):
print(x + y)
print(y + x)
+
+
add(1, 2)
add(42, 3)
add(-1, 2)
add(-42, -3)
+
@micropython.viper
-def sub(x:int, y:int):
+def sub(x: int, y: int):
print(x - y)
print(y - x)
+
+
sub(1, 2)
sub(42, 3)
sub(-1, 2)
sub(-42, -3)
+
@micropython.viper
-def mul(x:int, y:int):
+def mul(x: int, y: int):
print(x * y)
print(y * x)
+
+
mul(0, 1)
mul(1, -1)
mul(1, 2)
@@ -29,41 +38,56 @@ mul(8, 3)
mul(-3, 4)
mul(-9, -6)
+
@micropython.viper
-def shl(x:int, y:int):
+def shl(x: int, y: int):
print(x << y)
+
+
shl(1, 0)
shl(1, 3)
shl(1, 30)
shl(42, 10)
shl(-42, 10)
+
@micropython.viper
-def shr(x:int, y:int):
+def shr(x: int, y: int):
print(x >> y)
+
+
shr(1, 0)
shr(1, 3)
shr(42, 2)
shr(-42, 2)
+
@micropython.viper
-def and_(x:int, y:int):
+def and_(x: int, y: int):
print(x & y, y & x)
+
+
and_(1, 0)
and_(1, 3)
-and_(0xf0, 0x3f)
+and_(0xF0, 0x3F)
and_(-42, 6)
+
@micropython.viper
-def or_(x:int, y:int):
+def or_(x: int, y: int):
print(x | y, y | x)
+
+
or_(1, 0)
or_(1, 2)
or_(-42, 5)
+
@micropython.viper
-def xor(x:int, y:int):
+def xor(x: int, y: int):
print(x ^ y, y ^ x)
+
+
xor(1, 0)
xor(1, 2)
xor(-42, 5)
diff --git a/tests/micropython/viper_binop_comp.py b/tests/micropython/viper_binop_comp.py
index dcf91ed89..a4c0809c8 100644
--- a/tests/micropython/viper_binop_comp.py
+++ b/tests/micropython/viper_binop_comp.py
@@ -1,6 +1,6 @@
# test comparison operators
@micropython.viper
-def f(x:int, y:int):
+def f(x: int, y: int):
if x < y:
print(x, "<", y)
if x > y:
@@ -14,6 +14,7 @@ def f(x:int, y:int):
if x != y:
print(x, "!=", y)
+
f(1, 1)
f(2, 1)
f(1, 2)
diff --git a/tests/micropython/viper_binop_comp_imm.py b/tests/micropython/viper_binop_comp_imm.py
index c7c040895..daab8fcfb 100644
--- a/tests/micropython/viper_binop_comp_imm.py
+++ b/tests/micropython/viper_binop_comp_imm.py
@@ -3,6 +3,7 @@
def f(a: int):
print(a == -1, a == -255, a == -256, a == -257)
+
f(-1)
f(-255)
f(-256)
diff --git a/tests/micropython/viper_binop_divmod.py b/tests/micropython/viper_binop_divmod.py
index 822424982..4b74b527d 100644
--- a/tests/micropython/viper_binop_divmod.py
+++ b/tests/micropython/viper_binop_divmod.py
@@ -1,16 +1,20 @@
# test floor-division and modulo operators
+
@micropython.viper
-def div(x:int, y:int) -> int:
+def div(x: int, y: int) -> int:
return x // y
+
@micropython.viper
-def mod(x:int, y:int) -> int:
+def mod(x: int, y: int) -> int:
return x % y
+
def dm(x, y):
print(div(x, y), mod(x, y))
+
for x in (-6, 6):
for y in range(-7, 8):
if y == 0:
diff --git a/tests/micropython/viper_binop_multi_comp.py b/tests/micropython/viper_binop_multi_comp.py
index 8065db291..997c397d4 100644
--- a/tests/micropython/viper_binop_multi_comp.py
+++ b/tests/micropython/viper_binop_multi_comp.py
@@ -1,6 +1,6 @@
# test multi comparison operators
@micropython.viper
-def f(x:int, y:int):
+def f(x: int, y: int):
if 0 < x < y:
print(x, "<", y)
if 3 > x > y:
@@ -14,6 +14,7 @@ def f(x:int, y:int):
if 2 == x != y:
print(x, "!=", y)
+
f(1, 1)
f(2, 1)
f(1, 2)
diff --git a/tests/micropython/viper_cond.py b/tests/micropython/viper_cond.py
index bbb3f6923..d5ebf837b 100644
--- a/tests/micropython/viper_cond.py
+++ b/tests/micropython/viper_cond.py
@@ -6,6 +6,8 @@ def f():
pass
else:
print("not x", x)
+
+
f()
# using True as a conditional
@@ -14,6 +16,8 @@ def f():
x = True
if x:
print("x", x)
+
+
f()
# using an int as a conditional
@@ -22,6 +26,8 @@ def g():
y = 1
if y:
print("y", y)
+
+
g()
# using an int as a conditional that has the lower 16-bits clear
@@ -30,4 +36,6 @@ def h():
z = 0x10000
if z:
print("z", z)
+
+
h()
diff --git a/tests/micropython/viper_const.py b/tests/micropython/viper_const.py
index 5085ede90..230b282f2 100644
--- a/tests/micropython/viper_const.py
+++ b/tests/micropython/viper_const.py
@@ -1,14 +1,21 @@
# test loading constants in viper functions
+
@micropython.viper
def f():
- return b'bytes'
+ return b"bytes"
+
+
print(f())
+
@micropython.viper
def f():
@micropython.viper
def g() -> int:
return 123
+
return g
+
+
print(f()())
diff --git a/tests/micropython/viper_const_intbig.py b/tests/micropython/viper_const_intbig.py
index 6b4497388..42574820a 100644
--- a/tests/micropython/viper_const_intbig.py
+++ b/tests/micropython/viper_const_intbig.py
@@ -1,7 +1,9 @@
# check loading constants
+
@micropython.viper
def f():
return 123456789012345678901234567890
+
print(f())
diff --git a/tests/micropython/viper_error.py b/tests/micropython/viper_error.py
index ff32f5473..790f3d75c 100644
--- a/tests/micropython/viper_error.py
+++ b/tests/micropython/viper_error.py
@@ -1,11 +1,13 @@
# test syntax and type errors specific to viper code generation
+
def test(code):
try:
exec(code)
except (SyntaxError, ViperTypeError, NotImplementedError) as e:
print(repr(e))
+
# viper: annotations must be identifiers
test("@micropython.viper\ndef f(a:1): pass")
test("@micropython.viper\ndef f() -> 1: pass")
@@ -14,30 +16,36 @@ test("@micropython.viper\ndef f() -> 1: pass")
test("@micropython.viper\ndef f(x:unknown_type): pass")
# local used before type known
-test("""
+test(
+ """
@micropython.viper
def f():
print(x)
x = 1
-""")
+"""
+)
# type mismatch storing to local
-test("""
+test(
+ """
@micropython.viper
def f():
x = 1
y = []
x = y
-""")
+"""
+)
# can't implicitly convert type to bool
-test("""
+test(
+ """
@micropython.viper
def f():
x = ptr(0)
if x:
pass
-""")
+"""
+)
# incorrect return type
test("@micropython.viper\ndef f() -> int: return []")
diff --git a/tests/micropython/viper_globals.py b/tests/micropython/viper_globals.py
index 9c68dc3da..9532dfd89 100644
--- a/tests/micropython/viper_globals.py
+++ b/tests/micropython/viper_globals.py
@@ -2,18 +2,21 @@
gl = {}
-exec("""
+exec(
+ """
@micropython.viper
def f():
return x
-""", gl)
+""",
+ gl,
+)
# x is not yet in the globals, f should not see it
try:
- print(gl['f']())
+ print(gl["f"]())
except NameError:
- print('NameError')
+ print("NameError")
# x is in globals, f should now see it
-gl['x'] = 123
-print(gl['f']())
+gl["x"] = 123
+print(gl["f"]())
diff --git a/tests/micropython/viper_import.py b/tests/micropython/viper_import.py
index 987800744..3df23e17a 100644
--- a/tests/micropython/viper_import.py
+++ b/tests/micropython/viper_import.py
@@ -1,10 +1,15 @@
# test import within viper function
+
@micropython.viper
def f():
import micropython
+
print(micropython.const(1))
from micropython import const
+
print(const(2))
+
+
f()
diff --git a/tests/micropython/viper_misc.py b/tests/micropython/viper_misc.py
index 021e03f2c..41389c751 100644
--- a/tests/micropython/viper_misc.py
+++ b/tests/micropython/viper_misc.py
@@ -2,61 +2,79 @@ import micropython
# viper function taking and returning ints
@micropython.viper
-def viper_int(x:int, y:int) -> int:
+def viper_int(x: int, y: int) -> int:
return x + y + 3
+
+
print(viper_int(1, 2))
# viper function taking and returning objects
@micropython.viper
-def viper_object(x:object, y:object) -> object:
+def viper_object(x: object, y: object) -> object:
return x + y
+
+
print(viper_object(1, 2))
# return None as non-object (should return 0)
@micropython.viper
def viper_ret_none() -> int:
return None
+
+
print(viper_ret_none())
# return Ellipsis as object
@micropython.viper
def viper_ret_ellipsis() -> object:
return ...
+
+
print(viper_ret_ellipsis())
# 3 args
@micropython.viper
-def viper_3args(a:int, b:int, c:int) -> int:
+def viper_3args(a: int, b: int, c: int) -> int:
return a + b + c
+
+
print(viper_3args(1, 2, 3))
# 4 args
@micropython.viper
-def viper_4args(a:int, b:int, c:int, d:int) -> int:
+def viper_4args(a: int, b: int, c: int, d: int) -> int:
return a + b + c + d
+
+
# viper call with 4 args not yet supported
-#print(viper_4args(1, 2, 3, 4))
+# print(viper_4args(1, 2, 3, 4))
# a local (should have automatic type int)
@micropython.viper
-def viper_local(x:int) -> int:
+def viper_local(x: int) -> int:
y = 4
return x + y
+
+
print(viper_local(3))
# without type annotation, types should default to object
@micropython.viper
def viper_no_annotation(x, y):
return x * y
+
+
print(viper_no_annotation(4, 5))
# a for loop
@micropython.viper
-def viper_for(a:int, b:int) -> int:
+def viper_for(a: int, b: int) -> int:
total = 0
for x in range(a, b):
total += x
return total
+
+
print(viper_for(10, 10000))
# accessing a global
@@ -65,42 +83,56 @@ def viper_access_global():
global gl
gl = 1
return gl
+
+
print(viper_access_global(), gl)
# calling print with object and int types
@micropython.viper
-def viper_print(x, y:int):
+def viper_print(x, y: int):
print(x, y + 1)
+
+
viper_print(1, 2)
# convert constants to objects in tuple
@micropython.viper
def viper_tuple_consts(x):
return (x, 1, False, True)
+
+
print(viper_tuple_consts(0))
# making a tuple from an object and an int
@micropython.viper
-def viper_tuple(x, y:int):
+def viper_tuple(x, y: int):
return (x, y + 1)
+
+
print(viper_tuple(1, 2))
# making a list from an object and an int
@micropython.viper
-def viper_list(x, y:int):
+def viper_list(x, y: int):
return [x, y + 1]
+
+
print(viper_list(1, 2))
# making a set from an object and an int
@micropython.viper
-def viper_set(x, y:int):
+def viper_set(x, y: int):
return {x, y + 1}
+
+
print(sorted(list(viper_set(1, 2))))
# raising an exception
@micropython.viper
-def viper_raise(x:int):
+def viper_raise(x: int):
raise OSError(x)
+
+
try:
viper_raise(1)
except OSError as e:
@@ -110,7 +142,10 @@ except OSError as e:
@micropython.viper
def viper_gc() -> int:
return 1
+
+
print(viper_gc())
import gc
+
gc.collect()
print(viper_gc())
diff --git a/tests/micropython/viper_misc_intbig.py b/tests/micropython/viper_misc_intbig.py
index e036435c7..055c08d8e 100644
--- a/tests/micropython/viper_misc_intbig.py
+++ b/tests/micropython/viper_misc_intbig.py
@@ -4,5 +4,8 @@ import micropython
@micropython.viper
def viper_uint() -> uint:
return uint(-1)
+
+
import sys
+
print(viper_uint() == (sys.maxsize << 1 | 1))
diff --git a/tests/micropython/viper_ptr16_load.py b/tests/micropython/viper_ptr16_load.py
index 0b865eb9e..30c85d066 100644
--- a/tests/micropython/viper_ptr16_load.py
+++ b/tests/micropython/viper_ptr16_load.py
@@ -1,21 +1,25 @@
# test loading from ptr16 type
# only works on little endian machines
+
@micropython.viper
-def get(src:ptr16) -> int:
+def get(src: ptr16) -> int:
return src[0]
+
@micropython.viper
-def get1(src:ptr16) -> int:
+def get1(src: ptr16) -> int:
return src[1]
+
@micropython.viper
-def memadd(src:ptr16, n:int) -> int:
+def memadd(src: ptr16, n: int) -> int:
sum = 0
for i in range(n):
sum += src[i]
return sum
+
@micropython.viper
def memadd2(src_in) -> int:
src = ptr16(src_in)
@@ -25,7 +29,8 @@ def memadd2(src_in) -> int:
sum += src[i]
return sum
-b = bytearray(b'1234')
+
+b = bytearray(b"1234")
print(b)
print(get(b), get1(b))
print(memadd(b, 2))
diff --git a/tests/micropython/viper_ptr16_store.py b/tests/micropython/viper_ptr16_store.py
index 5a5f25d17..3ca5a027c 100644
--- a/tests/micropython/viper_ptr16_store.py
+++ b/tests/micropython/viper_ptr16_store.py
@@ -1,25 +1,30 @@
# test ptr16 type
+
@micropython.viper
-def set(dest:ptr16, val:int):
+def set(dest: ptr16, val: int):
dest[0] = val
+
@micropython.viper
-def set1(dest:ptr16, val:int):
+def set1(dest: ptr16, val: int):
dest[1] = val
+
@micropython.viper
-def memset(dest:ptr16, val:int, n:int):
+def memset(dest: ptr16, val: int, n: int):
for i in range(n):
dest[i] = val
+
@micropython.viper
-def memset2(dest_in, val:int):
+def memset2(dest_in, val: int):
dest = ptr16(dest_in)
n = int(len(dest_in)) >> 1
for i in range(n):
dest[i] = val
+
b = bytearray(4)
print(b)
diff --git a/tests/micropython/viper_ptr32_load.py b/tests/micropython/viper_ptr32_load.py
index 4d8b3846d..b0b90bcaf 100644
--- a/tests/micropython/viper_ptr32_load.py
+++ b/tests/micropython/viper_ptr32_load.py
@@ -1,20 +1,24 @@
# test loading from ptr32 type
+
@micropython.viper
-def get(src:ptr32) -> int:
+def get(src: ptr32) -> int:
return src[0]
+
@micropython.viper
-def get1(src:ptr32) -> int:
+def get1(src: ptr32) -> int:
return src[1]
+
@micropython.viper
-def memadd(src:ptr32, n:int) -> int:
+def memadd(src: ptr32, n: int) -> int:
sum = 0
for i in range(n):
sum += src[i]
return sum
+
@micropython.viper
def memadd2(src_in) -> int:
src = ptr32(src_in)
@@ -24,7 +28,8 @@ def memadd2(src_in) -> int:
sum += src[i]
return sum
-b = bytearray(b'\x12\x12\x12\x12\x34\x34\x34\x34')
+
+b = bytearray(b"\x12\x12\x12\x12\x34\x34\x34\x34")
print(b)
print(hex(get(b)), hex(get1(b)))
print(hex(memadd(b, 2)))
diff --git a/tests/micropython/viper_ptr32_store.py b/tests/micropython/viper_ptr32_store.py
index 973086e4a..ff0c371ab 100644
--- a/tests/micropython/viper_ptr32_store.py
+++ b/tests/micropython/viper_ptr32_store.py
@@ -1,25 +1,30 @@
# test store to ptr32 type
+
@micropython.viper
-def set(dest:ptr32, val:int):
+def set(dest: ptr32, val: int):
dest[0] = val
+
@micropython.viper
-def set1(dest:ptr32, val:int):
+def set1(dest: ptr32, val: int):
dest[1] = val
+
@micropython.viper
-def memset(dest:ptr32, val:int, n:int):
+def memset(dest: ptr32, val: int, n: int):
for i in range(n):
dest[i] = val
+
@micropython.viper
-def memset2(dest_in, val:int):
+def memset2(dest_in, val: int):
dest = ptr32(dest_in)
n = int(len(dest_in)) >> 2
for i in range(n):
dest[i] = val
+
b = bytearray(8)
print(b)
diff --git a/tests/micropython/viper_ptr8_load.py b/tests/micropython/viper_ptr8_load.py
index 0ccf8a1d7..d871bfb68 100644
--- a/tests/micropython/viper_ptr8_load.py
+++ b/tests/micropython/viper_ptr8_load.py
@@ -1,20 +1,24 @@
# test loading from ptr8 type
+
@micropython.viper
-def get(src:ptr8) -> int:
+def get(src: ptr8) -> int:
return src[0]
+
@micropython.viper
-def get1(src:ptr8) -> int:
+def get1(src: ptr8) -> int:
return src[1]
+
@micropython.viper
-def memadd(src:ptr8, n:int) -> int:
+def memadd(src: ptr8, n: int) -> int:
sum = 0
for i in range(n):
sum += src[i]
return sum
+
@micropython.viper
def memadd2(src_in) -> int:
src = ptr8(src_in)
@@ -24,7 +28,8 @@ def memadd2(src_in) -> int:
sum += src[i]
return sum
-b = bytearray(b'1234')
+
+b = bytearray(b"1234")
print(b)
print(get(b), get1(b))
print(memadd(b, 4))
diff --git a/tests/micropython/viper_ptr8_store.py b/tests/micropython/viper_ptr8_store.py
index 5a8622eb1..baa9e2c6d 100644
--- a/tests/micropython/viper_ptr8_store.py
+++ b/tests/micropython/viper_ptr8_store.py
@@ -1,25 +1,30 @@
# test ptr8 type
+
@micropython.viper
-def set(dest:ptr8, val:int):
+def set(dest: ptr8, val: int):
dest[0] = val
+
@micropython.viper
-def set1(dest:ptr8, val:int):
+def set1(dest: ptr8, val: int):
dest[1] = val
+
@micropython.viper
-def memset(dest:ptr8, val:int, n:int):
+def memset(dest: ptr8, val: int, n: int):
for i in range(n):
dest[i] = val
+
@micropython.viper
-def memset2(dest_in, val:int):
+def memset2(dest_in, val: int):
dest = ptr8(dest_in)
n = int(len(dest_in))
for i in range(n):
dest[i] = val
+
b = bytearray(4)
print(b)
diff --git a/tests/micropython/viper_subscr.py b/tests/micropython/viper_subscr.py
index 2198ed731..bcaabd3fb 100644
--- a/tests/micropython/viper_subscr.py
+++ b/tests/micropython/viper_subscr.py
@@ -1,15 +1,18 @@
# test standard Python subscr using viper types
+
@micropython.viper
-def get(dest, i:int):
+def get(dest, i: int):
i += 1
return dest[i]
+
@micropython.viper
-def set(dest, i:int, val:int):
+def set(dest, i: int, val: int):
i += 1
dest[i] = val + 1
+
ar = [i for i in range(3)]
for i in range(len(ar)):
diff --git a/tests/micropython/viper_try.py b/tests/micropython/viper_try.py
index d75b3418e..61335af22 100644
--- a/tests/micropython/viper_try.py
+++ b/tests/micropython/viper_try.py
@@ -6,11 +6,13 @@ def f():
try:
fail
finally:
- print('finally')
+ print("finally")
+
+
try:
f()
except NameError:
- print('NameError')
+ print("NameError")
# nested try-except with try-finally
@micropython.viper
@@ -19,9 +21,11 @@ def f():
try:
fail
finally:
- print('finally')
+ print("finally")
except NameError:
- print('NameError')
+ print("NameError")
+
+
f()
# check that locals written to in try blocks keep their values
@@ -36,5 +40,6 @@ def f():
print(a)
a = 300
print(a)
-f()
+
+f()
diff --git a/tests/micropython/viper_types.py b/tests/micropython/viper_types.py
index ae72c0cf3..3af148171 100644
--- a/tests/micropython/viper_types.py
+++ b/tests/micropython/viper_types.py
@@ -4,8 +4,10 @@ import micropython
# converting incoming arg to bool
@micropython.viper
-def f1(x:bool):
+def f1(x: bool):
print(x)
+
+
f1(0)
f1(1)
f1([])
@@ -13,8 +15,10 @@ f1([1])
# taking and returning a bool
@micropython.viper
-def f2(x:bool) -> bool:
+def f2(x: bool) -> bool:
return x
+
+
print(f2([]))
print(f2([1]))
@@ -22,5 +26,7 @@ print(f2([1]))
@micropython.viper
def f3(x) -> bool:
return bool(x)
+
+
print(f3([]))
print(f3(-1))
diff --git a/tests/micropython/viper_with.py b/tests/micropython/viper_with.py
index 2bc3c4f1b..d640c8ae0 100644
--- a/tests/micropython/viper_with.py
+++ b/tests/micropython/viper_with.py
@@ -1,18 +1,24 @@
# test with handling within a viper function
+
class C:
def __init__(self):
- print('__init__')
+ print("__init__")
+
def __enter__(self):
- print('__enter__')
+ print("__enter__")
+
def __exit__(self, a, b, c):
- print('__exit__', a, b, c)
+ print("__exit__", a, b, c)
+
# basic with
@micropython.viper
def f():
with C():
print(1)
+
+
f()
# nested with and try-except
@@ -24,5 +30,7 @@ def f():
fail
print(2)
except NameError:
- print('NameError')
+ print("NameError")
+
+
f()