From 98a627dc03bc02e1b827ead8cc71b02259731551 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 3 Apr 2014 14:57:53 +0300 Subject: [PATCH] py: Add "io" module. So far just includes "open" function, which should be supplied by a port. TODO: Make the module #ifdef'ed. --- py/builtin.h | 1 + py/builtintables.c | 3 +++ py/modio.c | 30 ++++++++++++++++++++++++++++++ py/mpconfig.h | 5 +++++ py/py.mk | 1 + py/qstrdefs.h | 1 + stm/file.c | 2 ++ stm/mpconfigport.h | 2 ++ unix-cpy/mpconfigport.h | 1 + 9 files changed, 46 insertions(+) create mode 100644 py/modio.c diff --git a/py/builtin.h b/py/builtin.h index 50269dfe1f..3120e5baf6 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -35,5 +35,6 @@ MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj); extern const mp_obj_module_t mp_module_array; extern const mp_obj_module_t mp_module_collections; +extern const mp_obj_module_t mp_module_io; extern const mp_obj_module_t mp_module_math; extern const mp_obj_module_t mp_module_micropython; diff --git a/py/builtintables.c b/py/builtintables.c index 2cb9240bbf..03fb92e569 100644 --- a/py/builtintables.c +++ b/py/builtintables.c @@ -121,6 +121,9 @@ STATIC const mp_builtin_elem_t builtin_module_table[] = { { MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython }, { MP_QSTR_array, (mp_obj_t)&mp_module_array }, +#if MICROPY_ENABLE_MOD_IO + { MP_QSTR_io, (mp_obj_t)&mp_module_io }, +#endif { MP_QSTR_collections, (mp_obj_t)&mp_module_collections }, #if MICROPY_ENABLE_FLOAT diff --git a/py/modio.c b/py/modio.c new file mode 100644 index 0000000000..ced3983581 --- /dev/null +++ b/py/modio.c @@ -0,0 +1,30 @@ +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "builtin.h" + +#if MICROPY_ENABLE_MOD_IO + +STATIC const mp_map_elem_t mp_module_io_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_io) }, + // Note: mp_builtin_open_obj should be defined by port, it's not + // part of the core. + { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, +}; + +STATIC const mp_map_t mp_module_io_globals = { + .all_keys_are_qstrs = 1, + .table_is_fixed_array = 1, + .used = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t), + .alloc = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t), + .table = (mp_map_elem_t*)mp_module_io_globals_table, +}; + +const mp_obj_module_t mp_module_io = { + .base = { &mp_type_module }, + .name = MP_QSTR_io, + .globals = (mp_map_t*)&mp_module_io_globals, +}; + +#endif diff --git a/py/mpconfig.h b/py/mpconfig.h index 09cc37913a..65577f06ca 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -105,6 +105,11 @@ typedef double mp_float_t; #define MICROPY_ENABLE_FLOAT (0) #endif +// Whether to provide "io" module +#ifndef MICROPY_ENABLE_MOD_IO +#define MICROPY_ENABLE_MOD_IO (1) +#endif + // Whether to support slice object and correspondingly // slice subscript operators #ifndef MICROPY_ENABLE_SLICE diff --git a/py/py.mk b/py/py.mk index e2e83eb36a..059029274e 100644 --- a/py/py.mk +++ b/py/py.mk @@ -78,6 +78,7 @@ PY_O_BASENAME = \ builtintables.o \ modarray.o \ modcollections.o \ + modio.o \ modmath.o \ modmicropython.o \ vm.o \ diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 368b0fc8e1..06548d0fa5 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -98,6 +98,7 @@ Q(float) Q(getattr) Q(hash) Q(id) +Q(io) Q(int) Q(isinstance) Q(issubclass) diff --git a/stm/file.c b/stm/file.c index 40ac3ff9ca..6a1162385c 100644 --- a/stm/file.c +++ b/stm/file.c @@ -92,3 +92,5 @@ mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) { } return self; } + +MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_open_obj, pyb_io_open); diff --git a/stm/mpconfigport.h b/stm/mpconfigport.h index 3f48c43f04..4c8338be53 100644 --- a/stm/mpconfigport.h +++ b/stm/mpconfigport.h @@ -18,6 +18,8 @@ #define MICROPY_ENABLE_LFN (0) #define MICROPY_LFN_CODE_PAGE (1) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ +extern const struct _mp_obj_fun_native_t mp_builtin_open_obj; + // type definitions for the specific machine #define BYTES_PER_WORD (4) diff --git a/unix-cpy/mpconfigport.h b/unix-cpy/mpconfigport.h index 6351f0c4fe..752df4f49b 100644 --- a/unix-cpy/mpconfigport.h +++ b/unix-cpy/mpconfigport.h @@ -3,6 +3,7 @@ #define MICROPY_EMIT_CPYTHON (1) #define MICROPY_ENABLE_LEXER_UNIX (1) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) +#define MICROPY_ENABLE_MOD_IO (0) // type definitions for the specific machine