Skip to content

Commit

Permalink
Raise ValueError when reading/writing a file that is closed.
Browse files Browse the repository at this point in the history
To be compatible with CPython's behaviour.
  • Loading branch information
dpgeorge committed May 23, 2016
1 parent d231463 commit 019d54f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions source/microbit/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ mp_obj_t microbit_remove(mp_obj_t filename) {
return mp_const_none;
}

static void check_file_open(file_descriptor_obj *self) {
if (!self->open) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file"));
}
}

static int advance(file_descriptor_obj *self, uint32_t n, bool write) {
DEBUG(("FILE DEBUG: Advancing from chunk %d, offset %d.\r\n", self->seek_chunk, self->seek_offset));
self->seek_offset += n;
Expand All @@ -350,7 +356,8 @@ static int advance(file_descriptor_obj *self, uint32_t n, bool write) {

mp_uint_t microbit_file_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
file_descriptor_obj *self = (file_descriptor_obj *)obj;
if (!self->open || self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) {
check_file_open(self);
if (self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) {
*errcode = EBADF;
return MP_STREAM_ERROR;
}
Expand Down Expand Up @@ -379,7 +386,8 @@ mp_uint_t microbit_file_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errco

mp_uint_t microbit_file_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) {
file_descriptor_obj *self = (file_descriptor_obj *)obj;
if (!self->open || !self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) {
check_file_open(self);
if (!self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) {
*errcode = EBADF;
return MP_STREAM_ERROR;
}
Expand Down

0 comments on commit 019d54f

Please sign in to comment.