Skip to content

Commit 98a627d

Browse files
committed
py: Add "io" module.
So far just includes "open" function, which should be supplied by a port. TODO: Make the module #ifdef'ed.
1 parent 8270e38 commit 98a627d

9 files changed

Lines changed: 46 additions & 0 deletions

File tree

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj);
3535

3636
extern const mp_obj_module_t mp_module_array;
3737
extern const mp_obj_module_t mp_module_collections;
38+
extern const mp_obj_module_t mp_module_io;
3839
extern const mp_obj_module_t mp_module_math;
3940
extern const mp_obj_module_t mp_module_micropython;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ STATIC const mp_builtin_elem_t builtin_module_table[] = {
121121
{ MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython },
122122

123123
{ MP_QSTR_array, (mp_obj_t)&mp_module_array },
124+
#if MICROPY_ENABLE_MOD_IO
125+
{ MP_QSTR_io, (mp_obj_t)&mp_module_io },
126+
#endif
124127
{ MP_QSTR_collections, (mp_obj_t)&mp_module_collections },
125128

126129
#if MICROPY_ENABLE_FLOAT

py/modio.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "misc.h"
2+
#include "mpconfig.h"
3+
#include "qstr.h"
4+
#include "obj.h"
5+
#include "builtin.h"
6+
7+
#if MICROPY_ENABLE_MOD_IO
8+
9+
STATIC const mp_map_elem_t mp_module_io_globals_table[] = {
10+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_io) },
11+
// Note: mp_builtin_open_obj should be defined by port, it's not
12+
// part of the core.
13+
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
14+
};
15+
16+
STATIC const mp_map_t mp_module_io_globals = {
17+
.all_keys_are_qstrs = 1,
18+
.table_is_fixed_array = 1,
19+
.used = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
20+
.alloc = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
21+
.table = (mp_map_elem_t*)mp_module_io_globals_table,
22+
};
23+
24+
const mp_obj_module_t mp_module_io = {
25+
.base = { &mp_type_module },
26+
.name = MP_QSTR_io,
27+
.globals = (mp_map_t*)&mp_module_io_globals,
28+
};
29+
30+
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ typedef double mp_float_t;
105105
#define MICROPY_ENABLE_FLOAT (0)
106106
#endif
107107

108+
// Whether to provide "io" module
109+
#ifndef MICROPY_ENABLE_MOD_IO
110+
#define MICROPY_ENABLE_MOD_IO (1)
111+
#endif
112+
108113
// Whether to support slice object and correspondingly
109114
// slice subscript operators
110115
#ifndef MICROPY_ENABLE_SLICE

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ PY_O_BASENAME = \
7878
builtintables.o \
7979
modarray.o \
8080
modcollections.o \
81+
modio.o \
8182
modmath.o \
8283
modmicropython.o \
8384
vm.o \

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Q(float)
9898
Q(getattr)
9999
Q(hash)
100100
Q(id)
101+
Q(io)
101102
Q(int)
102103
Q(isinstance)
103104
Q(issubclass)

stm/file.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
9292
}
9393
return self;
9494
}
95+
96+
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_open_obj, pyb_io_open);

stm/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define MICROPY_ENABLE_LFN (0)
1919
#define MICROPY_LFN_CODE_PAGE (1) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
2020

21+
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
22+
2123
// type definitions for the specific machine
2224

2325
#define BYTES_PER_WORD (4)

unix-cpy/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define MICROPY_EMIT_CPYTHON (1)
44
#define MICROPY_ENABLE_LEXER_UNIX (1)
55
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
6+
#define MICROPY_ENABLE_MOD_IO (0)
67

78
// type definitions for the specific machine
89

0 commit comments

Comments
 (0)