Browse Source

py: Add "io" module.

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

TODO: Make the module #ifdef'ed.
pull/419/head
Paul Sokolovsky 11 years ago
parent
commit
98a627dc03
  1. 1
      py/builtin.h
  2. 3
      py/builtintables.c
  3. 30
      py/modio.c
  4. 5
      py/mpconfig.h
  5. 1
      py/py.mk
  6. 1
      py/qstrdefs.h
  7. 2
      stm/file.c
  8. 2
      stm/mpconfigport.h
  9. 1
      unix-cpy/mpconfigport.h

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

3
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

30
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

5
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

1
py/py.mk

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

1
py/qstrdefs.h

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

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

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

1
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

Loading…
Cancel
Save