mirror of
https://github.com/micropython/micropython.git
synced 2026-05-01 13:20:14 +02:00
6f96d260e6
The webassembly port needs some additional weakref tests due to the fact that garbage collection only happens when Python execution finishes and JavaScript resumes. The `tests/ports/webassembly/heap_expand.py` expected output also needs to be updated because the amount of GC heap got smaller (weakref WTB takes some of the available RAM). Signed-off-by: Damien George <damien@micropython.org>
77 lines
1.7 KiB
Python
77 lines
1.7 KiB
Python
# Test weakref.finalize() functionality requiring gc.collect().
|
|
# Should be kept in sync with tests/ports/webassembly/weakref_finalize_collect.py.
|
|
|
|
try:
|
|
import weakref
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
# gc module must be available if weakref is.
|
|
import gc
|
|
|
|
|
|
class A:
|
|
def __str__(self):
|
|
return "<A object>"
|
|
|
|
|
|
def callback(*args, **kwargs):
|
|
print("callback({}, {})".format(args, kwargs))
|
|
return 42
|
|
|
|
|
|
def test():
|
|
print("test basic use of finalize() with a simple callback")
|
|
a = A()
|
|
f = weakref.finalize(a, callback)
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
print("alive", f.alive)
|
|
print("peek", f.peek())
|
|
print("detach", f.detach())
|
|
print("call", f())
|
|
|
|
print("test that a callback is passed the correct values")
|
|
a = A()
|
|
f = weakref.finalize(a, callback, 1, 2, kwarg=3)
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
print("alive", f.alive)
|
|
print("peek", f.peek())
|
|
print("detach", f.detach())
|
|
print("call", f())
|
|
|
|
print("test that calling the finalizer cancels the finalizer")
|
|
a = A()
|
|
f = weakref.finalize(a, callback)
|
|
print(f())
|
|
print(a)
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
|
|
print("test that calling detach cancels the finalizer")
|
|
a = A()
|
|
f = weakref.finalize(a, callback)
|
|
print(len(f.detach()))
|
|
print(a)
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
|
|
print("test that finalize does not get collected before its ref does")
|
|
a = A()
|
|
weakref.finalize(a, callback)
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
print("free a")
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
|
|
|
|
test()
|