From 26b4ef4c46520f595f39d53bbea7f247c9467370 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Feb 2021 22:53:36 +1100 Subject: [PATCH] extmod/vfs_posix_file: Allow closing an already closed file. Signed-off-by: Damien George --- extmod/vfs_posix_file.c | 6 +++++- tests/extmod/vfs_posix.py | 25 +++++++++++++++++++++++++ tests/extmod/vfs_posix.py.exp | 1 + tests/io/file1.py | 3 +++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index 264764b4ee..f3eac98ce3 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -163,7 +163,11 @@ STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) { mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in); - check_fd_is_open(o); + + if (request != MP_STREAM_CLOSE) { + check_fd_is_open(o); + } + switch (request) { case MP_STREAM_FLUSH: { int ret; diff --git a/tests/extmod/vfs_posix.py b/tests/extmod/vfs_posix.py index 3bea99365d..aea447e182 100644 --- a/tests/extmod/vfs_posix.py +++ b/tests/extmod/vfs_posix.py @@ -8,6 +8,15 @@ except (ImportError, AttributeError): print("SKIP") raise SystemExit +# We need a file for testing that doesn't already exist. +# Skip the test if it does exist. +temp_file = "micropy_test_file.txt" +try: + uos.stat(temp_file) + print("SKIP") + raise SystemExit +except OSError: + pass # getcwd and chdir curdir = uos.getcwd() @@ -21,3 +30,19 @@ print(type(uos.stat("/"))) # listdir and ilistdir print(type(uos.listdir("/"))) + +# file create +f = open(temp_file, "w") +f.write("hello") +f.close() + +# close on a closed file should succeed +f.close() + +# file read +f = open(temp_file, "r") +print(f.read()) +f.close() + +# remove +uos.remove(temp_file) diff --git a/tests/extmod/vfs_posix.py.exp b/tests/extmod/vfs_posix.py.exp index 1b1f59b43e..c0a4ed000b 100644 --- a/tests/extmod/vfs_posix.py.exp +++ b/tests/extmod/vfs_posix.py.exp @@ -2,3 +2,4 @@ True +hello diff --git a/tests/io/file1.py b/tests/io/file1.py index 2a46c9c63e..de30045d31 100644 --- a/tests/io/file1.py +++ b/tests/io/file1.py @@ -44,3 +44,6 @@ try: except OSError: print("OSError") f.close() + +# close() on a closed file +f.close()