Skip to content
Snippets Groups Projects
Commit 56942019 authored by Damien George's avatar Damien George
Browse files

extmod/vfs_fat_file: Make file.close() a no-op if file already closed.

As per CPython semantics.  In particular, file.__del__() should not raise
an exception if the file is already closed.
parent 06e70329
No related branches found
No related tags found
No related merge requests found
...@@ -120,10 +120,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_flush_obj, file_obj_flush); ...@@ -120,10 +120,13 @@ 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) { STATIC mp_obj_t file_obj_close(mp_obj_t self_in) {
pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
// 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); FRESULT res = f_close(&self->fp);
if (res != FR_OK) { if (res != FR_OK) {
mp_raise_OSError(fresult_to_errno_table[res]); mp_raise_OSError(fresult_to_errno_table[res]);
} }
}
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close); STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close);
......
...@@ -48,6 +48,7 @@ print(str(f)[:17], str(f)[-1:]) ...@@ -48,6 +48,7 @@ print(str(f)[:17], str(f)[-1:])
f.write("hello!") f.write("hello!")
f.flush() f.flush()
f.close() f.close()
f.close() # allowed
try: try:
f.write("world!") f.write("world!")
except OSError as e: except OSError as e:
...@@ -63,11 +64,6 @@ try: ...@@ -63,11 +64,6 @@ try:
except OSError as e: except OSError as e:
print(e.args[0] == uerrno.EINVAL) print(e.args[0] == uerrno.EINVAL)
try:
f.close()
except OSError as e:
print(e.args[0] == uerrno.EINVAL)
try: try:
vfs.open("foo_file.txt", "x") vfs.open("foo_file.txt", "x")
except OSError as e: except OSError as e:
......
...@@ -3,7 +3,6 @@ True ...@@ -3,7 +3,6 @@ True
True True
True True
True True
True
hello!world! hello!world!
12 12
h h
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment