Skip to content

Commit

Permalink
extmod/vfs_posix_file: Don't create file object unless open succeeds.
Browse files Browse the repository at this point in the history
When passing in already open file handle integer, don't
enable the "handle close" finaliser.

Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
Andrew Leech committed Feb 16, 2024
1 parent 4a2e510 commit 0502cd5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions extmod/vfs_posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ STATIC void vfs_posix_file_print(const mp_print_t *print, mp_obj_t self_in, mp_p
}

mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in) {
mp_obj_vfs_posix_file_t *o = m_new_obj_with_finaliser(mp_obj_vfs_posix_file_t);
const char *mode_s = mp_obj_str_get_str(mode_in);

int mode_rw = 0, mode_x = 0;
Expand Down Expand Up @@ -92,18 +91,21 @@ mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_
}
}

o->base.type = type;

mp_obj_t fid = file_in;

if (mp_obj_is_small_int(fid)) {
// When opening an existing file description, don't close it on deletion
mp_obj_vfs_posix_file_t *o = m_new_obj(mp_obj_vfs_posix_file_t);
o->base.type = type;
o->fd = MP_OBJ_SMALL_INT_VALUE(fid);
return MP_OBJ_FROM_PTR(o);
}

const char *fname = mp_obj_str_get_str(fid);
int fd;
MP_HAL_RETRY_SYSCALL(fd, open(fname, mode_x | mode_rw, 0644), mp_raise_OSError(err));
mp_obj_vfs_posix_file_t *o = m_new_obj_with_finaliser(mp_obj_vfs_posix_file_t);
o->base.type = type;
o->fd = fd;
return MP_OBJ_FROM_PTR(o);
}
Expand Down

0 comments on commit 0502cd5

Please sign in to comment.