2017-02-10 00:09:20 -05:00
|
|
|
# test that iterating doesn't use the heap
|
2017-04-02 17:27:01 -04:00
|
|
|
try:
|
2018-02-14 00:46:44 -05:00
|
|
|
frozenset
|
2019-10-22 02:33:23 -04:00
|
|
|
except NameError:
|
2017-04-02 17:27:01 -04:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2019-10-22 02:33:23 -04:00
|
|
|
try:
|
|
|
|
import uarray as array
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import array
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2017-02-10 00:09:20 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
from micropython import heap_lock, heap_unlock
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
heap_lock = heap_unlock = lambda: 0
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-02-10 00:09:20 -05:00
|
|
|
|
|
|
|
def do_iter(l):
|
2017-12-11 04:56:20 -05:00
|
|
|
heap_lock()
|
2017-02-10 00:09:20 -05:00
|
|
|
for i in l:
|
|
|
|
print(i)
|
2017-12-11 04:56:20 -05:00
|
|
|
heap_unlock()
|
2017-02-10 00:09:20 -05:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-02-10 00:09:20 -05:00
|
|
|
def gen_func():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-02-10 00:09:20 -05:00
|
|
|
# pre-create collections to iterate over
|
|
|
|
ba = bytearray(b"123")
|
|
|
|
ar = array.array("H", (123, 456))
|
|
|
|
t = (1, 2, 3)
|
|
|
|
l = [1, 2]
|
|
|
|
d = {1: 2}
|
2017-04-02 17:27:01 -04:00
|
|
|
s = set((1,))
|
2017-02-10 00:09:20 -05:00
|
|
|
fs = frozenset((1,))
|
|
|
|
g1 = (100 + x for x in range(2))
|
|
|
|
g2 = gen_func()
|
|
|
|
|
2017-02-12 23:45:24 -05:00
|
|
|
# test containment (both success and failure) with the heap locked
|
|
|
|
heap_lock()
|
|
|
|
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)
|
|
|
|
print(1 in s, -1 in s)
|
|
|
|
heap_unlock()
|
|
|
|
|
|
|
|
# test unpacking with the heap locked
|
|
|
|
unp0 = unp1 = unp2 = None # preallocate slots for globals
|
|
|
|
heap_lock()
|
|
|
|
unp0, unp1, unp2 = t
|
|
|
|
print(unp0, unp1, unp2)
|
|
|
|
heap_unlock()
|
|
|
|
|
2017-02-10 00:09:20 -05:00
|
|
|
# test certain builtins with the heap locked
|
|
|
|
heap_lock()
|
|
|
|
print(all(t))
|
|
|
|
print(any(t))
|
|
|
|
print(min(t))
|
|
|
|
print(max(t))
|
|
|
|
print(sum(t))
|
|
|
|
heap_unlock()
|
|
|
|
|
|
|
|
# test iterating over collections with the heap locked
|
|
|
|
do_iter(b"123")
|
|
|
|
do_iter(ba)
|
|
|
|
do_iter(ar)
|
|
|
|
do_iter(t)
|
|
|
|
do_iter(l)
|
|
|
|
do_iter(d)
|
|
|
|
do_iter(s)
|
|
|
|
do_iter(fs)
|
|
|
|
do_iter(g1)
|
|
|
|
do_iter(g2)
|