Skip to content

Commit 5500cde

Browse files
committed
py, unix: Convert sys module to static representation.
1 parent 18bef25 commit 5500cde

10 files changed

Lines changed: 68 additions & 31 deletions

File tree

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ extern const mp_obj_module_t mp_module_io;
4242
extern const mp_obj_module_t mp_module_math;
4343
extern const mp_obj_module_t mp_module_micropython;
4444
extern const mp_obj_module_t mp_module_struct;
45+
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdin), (mp_obj_t)&mp_sys_stdin_obj },
27+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdout), (mp_obj_t)&mp_sys_stdout_obj },
28+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj },
29+
};
30+
31+
STATIC const mp_obj_dict_t mp_module_sys_globals = {
32+
.base = {&mp_type_dict},
33+
.map = {
34+
.all_keys_are_qstrs = 1,
35+
.table_is_fixed_array = 1,
36+
.used = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
37+
.alloc = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
38+
.table = (mp_map_elem_t*)mp_module_sys_globals_table,
39+
},
40+
};
41+
42+
const mp_obj_module_t mp_module_sys = {
43+
.base = { &mp_type_module },
44+
.name = MP_QSTR_sys,
45+
.globals = (mp_obj_dict_t*)&mp_module_sys_globals,
46+
};
47+
48+
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ 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+
123128
// Whether to support slice object and correspondingly
124129
// slice subscript operators
125130
#ifndef MICROPY_ENABLE_SLICE

py/py.mk

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

py/runtime.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ void mp_init(void) {
5353

5454
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
5555
dict_locals = dict_globals = &dict_main;
56-
57-
#if MICROPY_CPYTHON_COMPAT
58-
// Precreate sys module, so "import sys" didn't throw exceptions.
59-
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
60-
// Avoid warning of unused var
61-
(void)m_sys;
62-
#endif
63-
// init sys.path
64-
// for efficiency, left to platform-specific startup code
65-
//mp_sys_path = mp_obj_new_list(0, NULL);
66-
//mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
6756
}
6857

6958
void mp_deinit(void) {

py/runtime.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t th
6060

6161
mp_obj_t mp_make_raise_obj(mp_obj_t o);
6262

63-
extern mp_obj_t mp_sys_path;
6463
mp_map_t *mp_loaded_modules_get(void);
6564
mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
6665
mp_obj_t mp_import_from(mp_obj_t module, qstr name);
6766
void mp_import_all(mp_obj_t module);
67+
68+
extern struct _mp_obj_list_t mp_sys_path_obj;
69+
extern struct _mp_obj_list_t mp_sys_argv_obj;
70+
#define mp_sys_path ((mp_obj_t)&mp_sys_path_obj)
71+
#define mp_sys_argv ((mp_obj_t)&mp_sys_argv_obj)

unix/file.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) {
146146
}
147147
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open);
148148

149-
void file_init() {
150-
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
151-
mp_store_attr(m_sys, MP_QSTR_stdin, fdfile_new(STDIN_FILENO));
152-
mp_store_attr(m_sys, MP_QSTR_stdout, fdfile_new(STDOUT_FILENO));
153-
mp_store_attr(m_sys, MP_QSTR_stderr, fdfile_new(STDERR_FILENO));
154-
}
149+
const mp_obj_fdfile_t mp_sys_stdin_obj = { .base = {&rawfile_type}, .fd = STDIN_FILENO };
150+
const mp_obj_fdfile_t mp_sys_stdout_obj = { .base = {&rawfile_type}, .fd = STDOUT_FILENO };
151+
const mp_obj_fdfile_t mp_sys_stderr_obj = { .base = {&rawfile_type}, .fd = STDERR_FILENO };

unix/main.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
4242
// Stack top at the start of program
4343
void *stack_top;
4444

45-
void file_init();
4645
void microsocket_init();
4746
void time_init();
4847
void ffi_init();
@@ -326,7 +325,7 @@ int main(int argc, char **argv) {
326325
p++;
327326
}
328327
}
329-
mp_sys_path = mp_obj_new_list(path_num, NULL);
328+
mp_obj_list_init(mp_sys_path, path_num);
330329
mp_obj_t *path_items;
331330
mp_obj_list_get(mp_sys_path, &path_num, &path_items);
332331
path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
@@ -348,10 +347,7 @@ int main(int argc, char **argv) {
348347
p = p1 + 1;
349348
}
350349

351-
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
352-
mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
353-
mp_obj_t py_argv = mp_obj_new_list(0, NULL);
354-
mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
350+
mp_obj_list_init(mp_sys_argv, 0);
355351

356352
mp_store_name(qstr_from_str("test"), test_obj_new(42));
357353
mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
@@ -360,7 +356,6 @@ int main(int argc, char **argv) {
360356
mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
361357
#endif
362358

363-
file_init();
364359
microsocket_init();
365360
#if MICROPY_MOD_TIME
366361
time_init();
@@ -418,7 +413,7 @@ int main(int argc, char **argv) {
418413
free(basedir);
419414

420415
for (int i = a; i < argc; i++) {
421-
mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
416+
mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
422417
}
423418
do_file(argv[a]);
424419
executed = true;

0 commit comments

Comments
 (0)