From 86bfabec11456b892fadd47cecab12157bbd8c0e Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 28 Jun 2019 16:35:51 +1000 Subject: 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. --- tests/micropython/heap_lock.py | 9 +++++++++ tests/micropython/heap_lock.py.exp | 2 ++ tests/micropython/heap_locked.py | 12 ++++++++++++ tests/micropython/heap_locked.py.exp | 2 ++ 4 files changed, 25 insertions(+) create mode 100644 tests/micropython/heap_locked.py create mode 100644 tests/micropython/heap_locked.py.exp (limited to 'tests') diff --git a/tests/micropython/heap_lock.py b/tests/micropython/heap_lock.py index ca3f5806a..6d770d9ec 100644 --- a/tests/micropython/heap_lock.py +++ b/tests/micropython/heap_lock.py @@ -5,6 +5,7 @@ import micropython l = [] l2 = list(range(100)) +micropython.heap_lock() micropython.heap_lock() # general allocation on the heap @@ -19,6 +20,14 @@ try: 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 diff --git a/tests/micropython/heap_lock.py.exp b/tests/micropython/heap_lock.py.exp index 819c32663..ae79c88b8 100644 --- a/tests/micropython/heap_lock.py.exp +++ b/tests/micropython/heap_lock.py.exp @@ -1,3 +1,5 @@ MemoryError MemoryError +1 +MemoryError [] diff --git a/tests/micropython/heap_locked.py b/tests/micropython/heap_locked.py new file mode 100644 index 000000000..d9e5b5d40 --- /dev/null +++ b/tests/micropython/heap_locked.py @@ -0,0 +1,12 @@ +# test micropython.heap_locked() + +import micropython + +if not hasattr(micropython, "heap_locked"): + print("SKIP") + raise SystemExit + +micropython.heap_lock() +print(micropython.heap_locked()) +micropython.heap_unlock() +print(micropython.heap_locked()) diff --git a/tests/micropython/heap_locked.py.exp b/tests/micropython/heap_locked.py.exp new file mode 100644 index 000000000..b261da18d --- /dev/null +++ b/tests/micropython/heap_locked.py.exp @@ -0,0 +1,2 @@ +1 +0 -- cgit v1.2.3