diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index e269ef593c..76ac23685a 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -120,9 +120,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_flush_obj, file_obj_flush); STATIC mp_obj_t file_obj_close(mp_obj_t self_in) { pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); - FRESULT res = f_close(&self->fp); - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); + // if fs==NULL then the file is closed and in that case this method is a no-op + if (self->fp.fs != NULL) { + FRESULT res = f_close(&self->fp); + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } } return mp_const_none; } diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py index 26fec78281..de8d4953c4 100644 --- a/tests/extmod/vfs_fat_fileio.py +++ b/tests/extmod/vfs_fat_fileio.py @@ -48,6 +48,7 @@ print(str(f)[:17], str(f)[-1:]) f.write("hello!") f.flush() f.close() +f.close() # allowed try: f.write("world!") except OSError as e: @@ -63,11 +64,6 @@ try: except OSError as e: print(e.args[0] == uerrno.EINVAL) -try: - f.close() -except OSError as e: - print(e.args[0] == uerrno.EINVAL) - try: vfs.open("foo_file.txt", "x") except OSError as e: diff --git a/tests/extmod/vfs_fat_fileio.py.exp b/tests/extmod/vfs_fat_fileio.py.exp index 9f0edb31ef..c438bc850c 100644 --- a/tests/extmod/vfs_fat_fileio.py.exp +++ b/tests/extmod/vfs_fat_fileio.py.exp @@ -3,7 +3,6 @@ True True True True -True hello!world! 12 h