8000 Merge pull request #476 from pfalcon/static-sys · lurch/micropython@640e7e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 640e7e4

Browse files
committed
Merge pull request micropython#476 from pfalcon/static-sys
Convert sys module to static allocation
2 parents f95c68e + 4165cd1 commit 640e7e4

17 files changed

+100
-48
lines changed

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ extern const mp_obj_module_t mp_module_io;
4545
extern const mp_obj_module_t mp_module_math;
4646
extern const mp_obj_module_t mp_module_micropython;
4747
extern const mp_obj_module_t mp_module_struct;
48+
extern const mp_obj_module_t mp_module_sys;

py/builtinimport.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
#define PATH_SEP_CHAR '/'
3030

31-
mp_obj_t mp_sys_path;
32-
3331
mp_import_stat_t stat_dir_or_file(vstr_t *path) {
3432
//printf("stat %s\n", vstr_str(path));
3533
mp_import_stat_t stat = mp_import_stat(vstr_str(path));
@@ -48,9 +46,7 @@ mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
4846
// extract the list of paths
4947
uint path_num = 0;
5048
mp_obj_t *path_items;
51-
if (mp_sys_path != MP_OBJ_NULL) {
52-
mp_obj_list_get(mp_sys_path, &path_num, &path_items);
53-
}
49+
mp_obj_list_get(mp_sys_path, &path_num, &path_items);
5450

5551
if (path_num == 0) {
5652
// mp_sys_path is empty, so just use the given file name

py/builtintables.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
134134
#if MICROPY_ENABLE_FLOAT
135135
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },
136136
#endif
137+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },
137138

138139
// extra builtin modules as defined by a port
139140
MICROPY_EXTRA_BUILTIN_MODULES

py/modsys.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "misc.h"
2+
#include "mpconfig.h"
3+
#include "qstr.h"
4+
#include "obj.h"
5+
#include "builtin.h"
6+
#include "runtime.h"
7+
#include "objlist.h"
8+
9+
#if MICROPY_ENABLE_MOD_SYS
10+
11+
// These should be implemented by ports, specific types don't matter,
12+
// only addresses.
13+
struct _dummy_t;
14+
extern struct _dummy_t mp_sys_stdin_obj;
15+
extern struct _dummy_t mp_sys_stdout_obj;
16+
extern struct _dummy_t mp_sys_stderr_obj;
17+
18+
mp_obj_list_t mp_sys_path_obj;
19+
mp_obj_list_t mp_sys_argv_obj;
20+
21+
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
22+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
23+
{ MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj },
24+
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj },
25+
26+
#if MICROPY_MOD_SYS_STDFILES
27+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdin), (mp_obj_t)&mp_sys_stdin_obj },
28+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdout), (mp_obj_t)&mp_sys_stdout_obj },
29+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj },
30+
#endif
31+
};
32+
33+
STATIC const mp_obj_dict_t mp_module_sys_globals = {
34+
.base = {&mp_type_dict},
35+
.map = {
36+
.all_keys_are_qstrs = 1,
37+
.table_is_fixed_array = 1,
38+
.used = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
39+
.alloc = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
40+
.table = (mp_map_elem_t*)mp_module_sys_globals_table,
41+
},
42+
};
43+
44+
const mp_obj_module_t mp_module_sys = {
45+
.base = { &mp_type_module },
46+
.name = MP_QSTR_sys,
47+
.globals = (mp_obj_dict_t*)&mp_module_sys_globals,
48+
};
49+
50+
#endif

py/mpconfig.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ typedef double mp_float_t;
120120
#define MICROPY_ENABLE_MOD_STRUCT (1)
121121
#endif
122122

123+
// Whether to provide "sys" module
124+
#ifndef MICROPY_ENABLE_MOD_SYS
125+
#define MICROPY_ENABLE_MOD_SYS (1)
126+
#endif
127+
128+
#ifndef MICROPY_MOD_SYS_STDFILES
129+
#define MICROPY_MOD_SYS_STDFILES (0)
130+
#endif
131+
123132
// Whether to support slice object and correspondingly
124133
// slice subscript operators
125134
#ifndef MICROPY_ENABLE_SLICE

py/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ machine_int_t mp_obj_tuple_hash(mp_obj_t self_in);
454454
mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
455455

456456
// list
457+
struct _mp_obj_list_t;
458+
void mp_obj_list_init(struct _mp_obj_list_t *o, uint n);
457459
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
458460
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
459461
void mp_obj_list_set_len(mp_obj_t self_in, uint len);

py/objlist.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
#include "obj.h"
99
#include "runtime0.h"
1010
#include "runtime.h"
11-
12-
typedef struct _mp_obj_list_t {
13-
mp_obj_base_t base;
14-
machine_uint_t alloc;
15-
machine_uint_t len;
16-
mp_obj_t *items;
17-
} mp_obj_list_t;
11+
#include "objlist.h"
1812

1913
STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
2014
STATIC mp_obj_list_t *list_new(uint n);
@@ -371,12 +365,16 @@ const mp_obj_type_t mp_type_list = {
371365
.locals_dict = (mp_obj_t)&list_locals_dict,
372366
};
373367

374-
STATIC mp_obj_list_t *list_new(uint n) {
375-
mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
368+
void mp_obj_list_init(mp_obj_list_t *o, uint n) {
376369
o->base.type = &mp_type_list;
377370
o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
378371
o->len = n;
379372
o->items = m_new(mp_obj_t, o->alloc);
373+
}
374+
375+
STATIC mp_obj_list_t *list_new(uint n) {
376+
mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
377+
mp_obj_list_init(o, n);
380378
return o;
381379
}
382380

py/objlist.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
typedef struct _mp_obj_list_t {
2+
mp_obj_base_t base;
3+
machine_uint_t alloc;
4+
machine_uint_t len;
5+
mp_obj_t *items;
6+
} mp_obj_list_t;

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ PY_O_BASENAME = \
8383
modmath.o \
8484
modmicropython.o \
8585
modstruct.o \
86+
modsys.o \
8687
vm.o \
8788
showbc.o \
8889
repl.o \

py/qstrdefs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,10 @@ Q(encode)
270270
Q(decode)
271271
Q(utf-8)
272272
#endif
273+
274+
#if MICROPY_ENABLE_MOD_SYS
275+
Q(argv)
276+
Q(stdin)
277+
Q(stdout)
278+
Q(stderr)
279+
#endif

0 commit comments

Comments
 (0)
0