py: Add "io" module.

So far just includes "open" function, which should be supplied by a port.

TODO: Make the module #ifdef'ed.
This commit is contained in:
Paul Sokolovsky 2014-04-03 14:57:53 +03:00
parent 8270e3853d
commit 98a627dc03
9 changed files with 46 additions and 0 deletions

View File

@ -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;

View File

@ -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

30
py/modio.c Normal file
View File

@ -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

View File

@ -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

View File

@ -78,6 +78,7 @@ PY_O_BASENAME = \
builtintables.o \
modarray.o \
modcollections.o \
modio.o \
modmath.o \
modmicropython.o \
vm.o \

View File

@ -98,6 +98,7 @@ Q(float)
Q(getattr)
Q(hash)
Q(id)
Q(io)
Q(int)
Q(isinstance)
Q(issubclass)

View File

@ -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);

View File

@ -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)

View File

@ -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