From 98a627dc03bc02e1b827ead8cc71b02259731551 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 3 Apr 2014 14:57:53 +0300 Subject: 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 + 6 files changed, 41 insertions(+) create mode 100644 py/modio.c (limited to 'py') diff --git a/py/builtin.h b/py/builtin.h index 50269dfe1..3120e5baf 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 2cb9240bb..03fb92e56 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 000000000..ced398358 --- /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 09cc37913..65577f06c 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 e2e83eb36..059029274 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 368b0fc8e..06548d0fa 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) -- cgit v1.2.3