circuitpython/tests/micropython/heap_lock.py
Andrew Leech 86bfabec11 py/modmicropython: Add heap_locked function to test state of heap.
This commit adds micropython.heap_locked() which returns the current
lock-depth of the heap, and can be used by Python code to check if the heap
is locked or not.  This new function is configured via
MICROPY_PY_MICROPYTHON_HEAP_LOCKED and is disabled by default.

This commit also changes the return value of micropython.heap_unlock() so
it returns the current lock-depth as well.
2020-03-11 16:54:16 +11:00

35 lines
548 B
Python

# check that heap_lock/heap_unlock work as expected
import micropython
l = []
l2 = list(range(100))
micropython.heap_lock()
micropython.heap_lock()
# general allocation on the heap
try:
print([])
except MemoryError:
print('MemoryError')
# expansion of a heap block
try:
l.extend(l2)
except MemoryError:
print('MemoryError')
print(micropython.heap_unlock())
# Should still fail
try:
print([])
except MemoryError:
print('MemoryError')
micropython.heap_unlock()
# check that allocation works after an unlock
print([])