extmod/vfs_posix_file: Allow closing an already closed file.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-02-11 22:53:36 +11:00
parent c7aaee2b2b
commit 26b4ef4c46
4 changed files with 34 additions and 1 deletions

View File

@ -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) { 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); mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
if (request != MP_STREAM_CLOSE) {
check_fd_is_open(o); check_fd_is_open(o);
}
switch (request) { switch (request) {
case MP_STREAM_FLUSH: { case MP_STREAM_FLUSH: {
int ret; int ret;

View File

@ -8,6 +8,15 @@ except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit 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 # getcwd and chdir
curdir = uos.getcwd() curdir = uos.getcwd()
@ -21,3 +30,19 @@ print(type(uos.stat("/")))
# listdir and ilistdir # listdir and ilistdir
print(type(uos.listdir("/"))) 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)

View File

@ -2,3 +2,4 @@
True True
<class 'tuple'> <class 'tuple'>
<class 'list'> <class 'list'>
hello

View File

@ -44,3 +44,6 @@ try:
except OSError: except OSError:
print("OSError") print("OSError")
f.close() f.close()
# close() on a closed file
f.close()