py, unix: Convert sys module to static representation.

This commit is contained in:
Paul Sokolovsky 2014-04-13 06:43:18 +03:00
parent 18bef25a0c
commit 5500cdeec7
10 changed files with 68 additions and 31 deletions

View File

@ -42,3 +42,4 @@ 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_math;
extern const mp_obj_module_t mp_module_micropython; extern const mp_obj_module_t mp_module_micropython;
extern const mp_obj_module_t mp_module_struct; extern const mp_obj_module_t mp_module_struct;
extern const mp_obj_module_t mp_module_sys;

View File

@ -28,8 +28,6 @@
#define PATH_SEP_CHAR '/' #define PATH_SEP_CHAR '/'
mp_obj_t mp_sys_path;
mp_import_stat_t stat_dir_or_file(vstr_t *path) { mp_import_stat_t stat_dir_or_file(vstr_t *path) {
//printf("stat %s\n", vstr_str(path)); //printf("stat %s\n", vstr_str(path));
mp_import_stat_t stat = mp_import_stat(vstr_str(path)); 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) {
// extract the list of paths // extract the list of paths
uint path_num = 0; uint path_num = 0;
mp_obj_t *path_items; mp_obj_t *path_items;
if (mp_sys_path != MP_OBJ_NULL) {
mp_obj_list_get(mp_sys_path, &path_num, &path_items); mp_obj_list_get(mp_sys_path, &path_num, &path_items);
}
if (path_num == 0) { if (path_num == 0) {
// mp_sys_path is empty, so just use the given file name // mp_sys_path is empty, so just use the given file name

View File

@ -134,6 +134,7 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
#if MICROPY_ENABLE_FLOAT #if MICROPY_ENABLE_FLOAT
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math }, { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },
#endif #endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },
// extra builtin modules as defined by a port // extra builtin modules as defined by a port
MICROPY_EXTRA_BUILTIN_MODULES MICROPY_EXTRA_BUILTIN_MODULES

48
py/modsys.c Normal file
View File

@ -0,0 +1,48 @@
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#include "runtime.h"
#include "objlist.h"
#if MICROPY_ENABLE_MOD_SYS
// These should be implemented by ports, specific types don't matter,
// only addresses.
struct _dummy_t;
extern struct _dummy_t mp_sys_stdin_obj;
extern struct _dummy_t mp_sys_stdout_obj;
extern struct _dummy_t mp_sys_stderr_obj;
mp_obj_list_t mp_sys_path_obj;
mp_obj_list_t mp_sys_argv_obj;
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdin), (mp_obj_t)&mp_sys_stdin_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdout), (mp_obj_t)&mp_sys_stdout_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj },
};
STATIC const mp_obj_dict_t mp_module_sys_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_sys_globals_table,
},
};
const mp_obj_module_t mp_module_sys = {
.base = { &mp_type_module },
.name = MP_QSTR_sys,
.globals = (mp_obj_dict_t*)&mp_module_sys_globals,
};
#endif

View File

@ -120,6 +120,11 @@ typedef double mp_float_t;
#define MICROPY_ENABLE_MOD_STRUCT (1) #define MICROPY_ENABLE_MOD_STRUCT (1)
#endif #endif
// Whether to provide "sys" module
#ifndef MICROPY_ENABLE_MOD_SYS
#define MICROPY_ENABLE_MOD_SYS (1)
#endif
// Whether to support slice object and correspondingly // Whether to support slice object and correspondingly
// slice subscript operators // slice subscript operators
#ifndef MICROPY_ENABLE_SLICE #ifndef MICROPY_ENABLE_SLICE

View File

@ -82,6 +82,7 @@ PY_O_BASENAME = \
modmath.o \ modmath.o \
modmicropython.o \ modmicropython.o \
modstruct.o \ modstruct.o \
modsys.o \
vm.o \ vm.o \
showbc.o \ showbc.o \
repl.o \ repl.o \

View File

@ -53,17 +53,6 @@ void mp_init(void) {
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New()) // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
dict_locals = dict_globals = &dict_main; dict_locals = dict_globals = &dict_main;
#if MICROPY_CPYTHON_COMPAT
// Precreate sys module, so "import sys" didn't throw exceptions.
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
// Avoid warning of unused var
(void)m_sys;
#endif
// init sys.path
// for efficiency, left to platform-specific startup code
//mp_sys_path = mp_obj_new_list(0, NULL);
//mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
} }
void mp_deinit(void) { void mp_deinit(void) {

View File

@ -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
mp_obj_t mp_make_raise_obj(mp_obj_t o); mp_obj_t mp_make_raise_obj(mp_obj_t o);
extern mp_obj_t mp_sys_path;
mp_map_t *mp_loaded_modules_get(void); mp_map_t *mp_loaded_modules_get(void);
mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
mp_obj_t mp_import_from(mp_obj_t module, qstr name); mp_obj_t mp_import_from(mp_obj_t module, qstr name);
void mp_import_all(mp_obj_t module); void mp_import_all(mp_obj_t module);
extern struct _mp_obj_list_t mp_sys_path_obj;
extern struct _mp_obj_list_t mp_sys_argv_obj;
#define mp_sys_path ((mp_obj_t)&mp_sys_path_obj)
#define mp_sys_argv ((mp_obj_t)&mp_sys_argv_obj)

View File

@ -146,9 +146,6 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) {
} }
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open);
void file_init() { const mp_obj_fdfile_t mp_sys_stdin_obj = { .base = {&rawfile_type}, .fd = STDIN_FILENO };
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys); const mp_obj_fdfile_t mp_sys_stdout_obj = { .base = {&rawfile_type}, .fd = STDOUT_FILENO };
mp_store_attr(m_sys, MP_QSTR_stdin, fdfile_new(STDIN_FILENO)); const mp_obj_fdfile_t mp_sys_stderr_obj = { .base = {&rawfile_type}, .fd = STDERR_FILENO };
mp_store_attr(m_sys, MP_QSTR_stdout, fdfile_new(STDOUT_FILENO));
mp_store_attr(m_sys, MP_QSTR_stderr, fdfile_new(STDERR_FILENO));
}

View File

@ -42,7 +42,6 @@ long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
// Stack top at the start of program // Stack top at the start of program
void *stack_top; void *stack_top;
void file_init();
void microsocket_init(); void microsocket_init();
void time_init(); void time_init();
void ffi_init(); void ffi_init();
@ -326,7 +325,7 @@ int main(int argc, char **argv) {
p++; p++;
} }
} }
mp_sys_path = mp_obj_new_list(path_num, NULL); mp_obj_list_init(mp_sys_path, path_num);
mp_obj_t *path_items; mp_obj_t *path_items;
mp_obj_list_get(mp_sys_path, &path_num, &path_items); mp_obj_list_get(mp_sys_path, &path_num, &path_items);
path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_); path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
@ -348,10 +347,7 @@ int main(int argc, char **argv) {
p = p1 + 1; p = p1 + 1;
} }
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys); mp_obj_list_init(mp_sys_argv, 0);
mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
mp_obj_t py_argv = mp_obj_new_list(0, NULL);
mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
mp_store_name(qstr_from_str("test"), test_obj_new(42)); mp_store_name(qstr_from_str("test"), test_obj_new(42));
mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info)); 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) {
mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj); mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
#endif #endif
file_init();
microsocket_init(); microsocket_init();
#if MICROPY_MOD_TIME #if MICROPY_MOD_TIME
time_init(); time_init();
@ -418,7 +413,7 @@ int main(int argc, char **argv) {
free(basedir); free(basedir);
for (int i = a; i < argc; i++) { for (int i = a; i < argc; i++) {
mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i]))); mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
} }
do_file(argv[a]); do_file(argv[a]);
executed = true; executed = true;