Skip to content

Commit

Permalink
unix/moduos: Convert module to use extmod version.
Browse files Browse the repository at this point in the history
All variants now use extmod/moduos.c as their uos module implementation.
In particular this means they all have MICROPY_VFS enabled and use VfsPosix
for their filesystem.

As part of this, the available functions in uos become more consistent with
other ports:
- coverage variant gets uos.urandom
- minimal and standard variant get: unlink, chdir, getcwd, listdir

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Mar 9, 2022
1 parent ade2720 commit 2b409ef
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 522 deletions.
18 changes: 18 additions & 0 deletions extmod/moduos.c
Expand Up @@ -40,6 +40,10 @@
#include "extmod/vfs_lfs.h"
#endif

#if MICROPY_VFS_POSIX
#include "extmod/vfs_posix.h"
#endif

#if MICROPY_PY_UOS_UNAME
#include "genhdr/mpversion.h"
#endif
Expand Down Expand Up @@ -101,12 +105,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_uos_uname_obj, mp_uos_uname);
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },

#if MICROPY_PY_UOS_GETENV_PUTENV_UNSETENV
{ MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mp_uos_getenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_putenv), MP_ROM_PTR(&mp_uos_putenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_unsetenv), MP_ROM_PTR(&mp_uos_unsetenv_obj) },
#endif
#if MICROPY_PY_UOS_SEP
{ MP_ROM_QSTR(MP_QSTR_sep), MP_ROM_QSTR(MP_QSTR__slash_) },
#endif
#if MICROPY_PY_UOS_SYNC
{ MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&mp_uos_sync_obj) },
#endif
#if MICROPY_PY_UOS_SYSTEM
{ MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&mp_uos_system_obj) },
#endif
#if MICROPY_PY_UOS_UNAME
{ MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&mp_uos_uname_obj) },
#endif
Expand Down Expand Up @@ -135,6 +147,9 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
#if MICROPY_PY_UOS_DUPTERM_NOTIFY
{ MP_ROM_QSTR(MP_QSTR_dupterm_notify), MP_ROM_PTR(&mp_uos_dupterm_notify_obj) },
#endif
#if MICROPY_PY_UOS_ERRNO
{ MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_uos_errno_obj) },
#endif

#if MICROPY_VFS
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mp_vfs_ilistdir_obj) },
Expand All @@ -149,6 +164,9 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
#if MICROPY_VFS_LFS2
{ MP_ROM_QSTR(MP_QSTR_VfsLfs2), MP_ROM_PTR(&mp_type_vfs_lfs2) },
#endif
#if MICROPY_VFS_POSIX
{ MP_ROM_QSTR(MP_QSTR_VfsPosix), MP_ROM_PTR(&mp_type_vfs_posix) },
#endif
#endif
};
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
Expand Down
2 changes: 0 additions & 2 deletions ports/unix/Makefile
Expand Up @@ -225,8 +225,6 @@ SRC_C += \
mpthreadport.c \
input.c \
modmachine.c \
modos.c \
moduos_vfs.c \
modtime.c \
moduselect.c \
alloc.c \
Expand Down
32 changes: 0 additions & 32 deletions ports/unix/main.c
Expand Up @@ -723,38 +723,6 @@ MP_NOINLINE int main_(int argc, char **argv) {
return ret & 0xff;
}

#if !MICROPY_VFS
uint mp_import_stat(const char *path) {
struct stat st;
if (stat(path, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
return MP_IMPORT_STAT_DIR;
} else if (S_ISREG(st.st_mode)) {
return MP_IMPORT_STAT_FILE;
}
}
return MP_IMPORT_STAT_NO_EXIST;
}

#if MICROPY_PY_IO
// Factory function for I/O stream classes, only needed if generic VFS subsystem isn't used.
// Note: buffering and encoding are currently ignored.
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kwargs) {
enum { ARG_file, ARG_mode };
STATIC const mp_arg_t allowed_args[] = {
{ MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_r)} },
{ MP_QSTR_buffering, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_encoding, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kwargs, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
return mp_vfs_posix_file_open(&mp_type_textio, args[ARG_file].u_obj, args[ARG_mode].u_obj);
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
#endif
#endif

void nlr_jump_fail(void *val) {
fprintf(stderr, "FATAL: uncaught NLR %p\n", val);
exit(1);
Expand Down

0 comments on commit 2b409ef

Please sign in to comment.