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>
70 lines
1.4 KiB
Python
70 lines
1.4 KiB
Python
# Test weakref.ref() functionality requiring gc.collect().
|
|
# Should be kept in sync with tests/ports/webassembly/weakref_ref_collect.py.
|
|
|
|
try:
|
|
import weakref
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
# gc module must be available if weakref is.
|
|
import gc
|
|
|
|
# Cannot reference non-heap objects.
|
|
for value in (None, False, True, Ellipsis, 0, "", ()):
|
|
try:
|
|
weakref.ref(value)
|
|
except TypeError:
|
|
print(value, "TypeError")
|
|
|
|
|
|
class A:
|
|
def __str__(self):
|
|
return "<A object>"
|
|
|
|
|
|
def callback(r):
|
|
print("callback", r())
|
|
|
|
|
|
def test():
|
|
print("test basic use of ref() with only one argument")
|
|
a = A()
|
|
r = weakref.ref(a)
|
|
print(r())
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
print(r())
|
|
|
|
print("test use of ref() with a callback")
|
|
a = A()
|
|
r = weakref.ref(a, callback)
|
|
print(r())
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
print(r())
|
|
|
|
print("test when weakref gets collected before the object it refs")
|
|
a = A()
|
|
r = weakref.ref(a, callback)
|
|
print(r())
|
|
r = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
a = None
|
|
|
|
print("test a double reference")
|
|
a = A()
|
|
r1 = weakref.ref(a, callback)
|
|
r2 = weakref.ref(a, callback)
|
|
print(r1(), r2())
|
|
a = None
|
|
clean_the_stack = [0, 0, 0, 0]
|
|
gc.collect()
|
|
print(r1(), r2())
|
|
|
|
|
|
test()
|