Skip to content

Commit 2cca348

Browse files
committed
tests/basics: Add test for weakref having exception in callback.
Needs a native exp file because native code doesn't print line numbers in the traceback. Signed-off-by: Damien George <damien@micropython.org>
1 parent c91d09a commit 2cca348

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Test weakref ref/finalize raising an exception within the callback.
2+
#
3+
# This test has different output to CPython due to the way that MicroPython
4+
# prints the exception.
5+
6+
try:
7+
import weakref
8+
except ImportError:
9+
print("SKIP")
10+
raise SystemExit
11+
12+
import gc
13+
14+
15+
class A:
16+
def __str__(self):
17+
return "<A object>"
18+
19+
20+
def callback(*args):
21+
raise ValueError("weakref callback", args)
22+
23+
24+
def test():
25+
print("test ref with exception in the callback")
26+
a = A()
27+
r = weakref.ref(a, callback)
28+
a = None
29+
clean_the_stack = [0, 0, 0, 0]
30+
gc.collect()
31+
print("collect done")
32+
33+
print("test finalize with exception in the callback")
34+
a = A()
35+
weakref.finalize(a, callback)
36+
a = None
37+
clean_the_stack = [0, 0, 0, 0]
38+
gc.collect()
39+
print("collect done")
40+
41+
42+
test()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
test ref with exception in the callback
2+
Unhandled exception in weakref callback:
3+
Traceback (most recent call last):
4+
File "\.\+weakref_callback_exception.py", line 21, in callback
5+
ValueError: ('weakref callback', (<ref>,))
6+
collect done
7+
test finalize with exception in the callback
8+
Unhandled exception in weakref callback:
9+
Traceback (most recent call last):
10+
File "\.\+weakref_callback_exception.py", line 21, in callback
11+
ValueError: ('weakref callback', ())
12+
collect done
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test ref with exception in the callback
2+
Unhandled exception in weakref callback:
3+
ValueError: ('weakref callback', (<ref>,))
4+
collect done
5+
test finalize with exception in the callback
6+
Unhandled exception in weakref callback:
7+
ValueError: ('weakref callback', ())
8+
collect done

tests/run-tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"webassembly": (
159159
"basics/string_format_modulo.py", # can't print nulls to stdout
160160
"basics/string_strip.py", # can't print nulls to stdout
161+
"basics/weakref_callback_exception.py", # has different exception printing output
161162
"basics/weakref_ref_collect.py", # requires custom test due to GC behaviour
162163
"basics/weakref_finalize_collect.py", # requires custom test due to GC behaviour
163164
"extmod/asyncio_basic2.py",
@@ -435,6 +436,7 @@ def detect_target_wiring_script(pyb, args):
435436
"micropython/meminfo.py",
436437
"basics/bytes_compare3.py",
437438
"basics/builtin_help.py",
439+
"basics/weakref_callback_exception.py",
438440
"misc/sys_settrace_cov.py",
439441
"net_inet/tls_text_errors.py",
440442
"thread/thread_exc2.py",

0 commit comments

Comments
 (0)