Skip to content

Commit 90e719a

Browse files
committed
tests/extmod/vfs_fat_fileio1: Add test for calling file obj finaliser.
1 parent 09be031 commit 90e719a

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

tests/extmod/vfs_fat_fileio1.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,17 @@ def ioctl(self, op, arg):
125125
except MemoryError:
126126
print('MemoryError')
127127
micropython.heap_unlock()
128+
129+
# Here we test that the finaliser is actually called during a garbage collection.
130+
import gc
131+
N = 4
132+
for i in range(N):
133+
n = 'x%d' % i
134+
f = vfs.open(n, 'w')
135+
f.write(n)
136+
f = None # release f without closing
137+
[0, 1, 2, 3] # use up Python stack so f is really gone
138+
gc.collect() # should finalise all N files by closing them
139+
for i in range(N):
140+
with vfs.open('x%d' % i, 'r') as f:
141+
print(f.read())

tests/extmod/vfs_fat_fileio1.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ d
1212
True
1313
[('foo_dir', 16384, 0)]
1414
MemoryError
15+
x0
16+
x1
17+
x2
18+
x3

0 commit comments

Comments
 (0)