summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorstijn <stijn@ignitron.net>2022-09-12 14:45:39 +0200
committerDamien George <damien@micropython.org>2022-09-19 23:44:50 +1000
commit9ae8d3820474a9525d707b1e19f7703721aec1c8 (patch)
treee3771befbb0b352ea8b0d488d8064d39c2e988b8 /tests
parentfb77be150636090d69460578ed8458d84db02614 (diff)
downloadmicropython-9ae8d3820474a9525d707b1e19f7703721aec1c8.tar.gz
micropython-9ae8d3820474a9525d707b1e19f7703721aec1c8.zip
extmod/vfs_posix_file: Implement finaliser for files.
Prevent handle leaks when file objects aren't closed explicitly and fix some MICROPY_CPYTHON_COMPAT issues: this wasn't properly adhered to because #ifdef was used so it was always on, and closing files multiple times should be avoided unconditionally.
Diffstat (limited to 'tests')
-rw-r--r--tests/extmod/vfs_posix.py29
-rw-r--r--tests/extmod/vfs_posix.py.exp1
2 files changed, 30 insertions, 0 deletions
diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py
index d193236696..3c23140065 100644
--- a/tests/extmod/vfs_posix.py
+++ b/tests/extmod/vfs_posix.py
@@ -1,6 +1,7 @@
# Test for VfsPosix
try:
+ import gc
import uos
uos.VfsPosix
@@ -51,6 +52,34 @@ f = open(temp_dir + "/test", "r")
print(f.read())
f.close()
+# file finaliser, also see vfs_fat_finaliser.py
+names = [temp_dir + "/x%d" % i for i in range(4)]
+basefd = temp_dir + "/nextfd1"
+nextfd = temp_dir + "/nextfd2"
+
+with open(basefd, "w") as f:
+ base_file_no = f.fileno()
+
+for i in range(1024): # move GC head forwards by allocating a lot of single blocks
+ []
+
+
+def write_files_without_closing():
+ for n in names:
+ open(n, "w").write(n)
+ sorted(list(range(128)), key=lambda x: x) # use up Python and C stack so f is really gone
+
+
+write_files_without_closing()
+gc.collect()
+
+with open(nextfd, "w") as f:
+ next_file_no = f.fileno()
+ print("next_file_no <= base_file_no", next_file_no <= base_file_no)
+
+for n in names + [basefd, nextfd]:
+ uos.remove(n)
+
# rename
uos.rename(temp_dir + "/test", temp_dir + "/test2")
print(uos.listdir(temp_dir))
diff --git a/tests/extmod/vfs_posix.py.exp b/tests/extmod/vfs_posix.py.exp
index eb9ab43106..99922e621d 100644
--- a/tests/extmod/vfs_posix.py.exp
+++ b/tests/extmod/vfs_posix.py.exp
@@ -4,6 +4,7 @@ True
<class 'list'>
<io.TextIOWrapper 2>
hello
+next_file_no <= base_file_no True
['test2']
['test2']
<class 'tuple'>