From 90e719a232dbf4039085d4fea2faf1358e408e40 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Feb 2018 15:27:51 +1100 Subject: [PATCH] tests/extmod/vfs_fat_fileio1: Add test for calling file obj finaliser. --- tests/extmod/vfs_fat_fileio1.py | 14 ++++++++++++++ tests/extmod/vfs_fat_fileio1.py.exp | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/tests/extmod/vfs_fat_fileio1.py b/tests/extmod/vfs_fat_fileio1.py index 8c8ec57472..f1f4639ab7 100644 --- a/tests/extmod/vfs_fat_fileio1.py +++ b/tests/extmod/vfs_fat_fileio1.py @@ -125,3 +125,17 @@ try: except MemoryError: print('MemoryError') micropython.heap_unlock() + +# Here we test that the finaliser is actually called during a garbage collection. +import gc +N = 4 +for i in range(N): + n = 'x%d' % i + f = vfs.open(n, 'w') + f.write(n) + f = None # release f without closing + [0, 1, 2, 3] # use up Python stack so f is really gone +gc.collect() # should finalise all N files by closing them +for i in range(N): + with vfs.open('x%d' % i, 'r') as f: + print(f.read()) diff --git a/tests/extmod/vfs_fat_fileio1.py.exp b/tests/extmod/vfs_fat_fileio1.py.exp index a304c75d96..2d4792aa3b 100644 --- a/tests/extmod/vfs_fat_fileio1.py.exp +++ b/tests/extmod/vfs_fat_fileio1.py.exp @@ -12,3 +12,7 @@ d True [('foo_dir', 16384, 0)] MemoryError +x0 +x1 +x2 +x3