Proper support for registering builtin modules in ROM.

Comes with some refactoring of code and renaming of files.  All modules
are now named mod*.[ch].
pull/379/head
Damien George 2014-03-25 14:18:18 +00:00
rodzic 1dfde891e3
commit caac542b23
26 zmienionych plików z 297 dodań i 196 usunięć

Wyświetl plik

@ -32,5 +32,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
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_math;
extern const mp_obj_module_t mp_module_micropython;

Wyświetl plik

@ -12,6 +12,7 @@
#include "lexerunix.h"
#include "parse.h"
#include "obj.h"
#include "objmodule.h"
#include "parsehelper.h"
#include "compile.h"
#include "runtime0.h"
@ -156,7 +157,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len);
// check if module already exists
mp_obj_t module_obj = mp_obj_module_get(mp_obj_str_get_qstr(args[0]));
mp_obj_t module_obj = mp_module_get(mp_obj_str_get_qstr(args[0]));
if (module_obj != MP_OBJ_NULL) {
// If it's not a package, return module right away
char *p = strchr(mod_str, '.');
@ -169,7 +170,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
}
// Otherwise, we need to return top-level package
qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
return mp_obj_module_get(pkg_name);
return mp_module_get(pkg_name);
}
uint last = 0;
@ -200,7 +201,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", qstr_str(mod_name)));
}
module_obj = mp_obj_module_get(mod_name);
module_obj = mp_module_get(mod_name);
if (module_obj == MP_OBJ_NULL) {
// module not already loaded, so load it!

154
py/builtintables.c 100644
Wyświetl plik

@ -0,0 +1,154 @@
#include <stdlib.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#include "builtintables.h"
#include "objarray.h"
// builtins
// we put these tables in ROM because they're always needed and take up quite a bit of room in RAM
// in fact, it uses less ROM here in table form than the equivalent in code form initialising a dynamic mp_map_t object in RAM
// at the moment it's a linear table, but we could convert it to a const mp_map_t table with a simple preprocessing script
typedef struct _mp_builtin_elem_t {
qstr qstr;
mp_obj_t elem;
} mp_builtin_elem_t;
STATIC const mp_builtin_elem_t builtin_object_table[] = {
// built-in core functions
{ MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
{ MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
{ MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
// built-in types
{ MP_QSTR_bool, (mp_obj_t)&bool_type },
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
#if MICROPY_ENABLE_FLOAT
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
#endif
{ MP_QSTR_dict, (mp_obj_t)&dict_type },
{ MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
{ MP_QSTR_filter, (mp_obj_t)&filter_type },
#if MICROPY_ENABLE_FLOAT
{ MP_QSTR_float, (mp_obj_t)&mp_type_float },
#endif
{ MP_QSTR_int, (mp_obj_t)&int_type },
{ MP_QSTR_list, (mp_obj_t)&list_type },
{ MP_QSTR_map, (mp_obj_t)&map_type },
{ MP_QSTR_object, (mp_obj_t)&mp_type_object },
{ MP_QSTR_set, (mp_obj_t)&set_type },
{ MP_QSTR_str, (mp_obj_t)&str_type },
{ MP_QSTR_super, (mp_obj_t)&super_type },
{ MP_QSTR_tuple, (mp_obj_t)&tuple_type },
{ MP_QSTR_type, (mp_obj_t)&mp_type_type },
{ MP_QSTR_zip, (mp_obj_t)&zip_type },
{ MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
{ MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
// built-in user functions
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
{ MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
{ MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
{ MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
{ MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
{ MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
{ MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
{ MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
{ MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
{ MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
{ MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
{ MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
{ MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
{ MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
{ MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
{ MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
{ MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
{ MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
{ MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
{ MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
{ MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
{ MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
{ MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
{ MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
{ MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
// built-in exceptions
{ MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
{ MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError },
{ MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
{ MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
{ MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError },
{ MP_QSTR_EOFError, (mp_obj_t)&mp_type_EOFError },
{ MP_QSTR_EnvironmentError, (mp_obj_t)&mp_type_EnvironmentError },
{ MP_QSTR_Exception, (mp_obj_t)&mp_type_Exception },
{ MP_QSTR_FloatingPointError, (mp_obj_t)&mp_type_FloatingPointError },
{ MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit },
{ MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError },
{ MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
{ MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
{ MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
{ MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
{ MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError },
{ MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError },
{ MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
{ MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
{ MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
{ MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
{ MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError },
{ MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError },
{ MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
{ MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError },
{ MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit },
{ MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError },
{ MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
{ MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError },
{ MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
{ MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
{ MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
// Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
// TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
// Extra builtins as defined by a port
MICROPY_EXTRA_BUILTINS
{ MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
};
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 },
{ MP_QSTR_collections, (mp_obj_t)&mp_module_collections },
#if MICROPY_ENABLE_FLOAT
{ MP_QSTR_math, (mp_obj_t)&mp_module_math },
#endif
// extra builtin modules as defined by a port
MICROPY_EXTRA_BUILTIN_MODULES
{ MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
};
STATIC mp_obj_t mp_builtin_tables_lookup(const mp_builtin_elem_t *table, qstr q) {
for (; table->qstr != MP_QSTR_; table++) {
if (table->qstr == q) {
return table->elem;
}
}
return MP_OBJ_NULL;
}
mp_obj_t mp_builtin_tables_lookup_object(qstr q) {
return mp_builtin_tables_lookup(&builtin_object_table[0], q);
}
mp_obj_t mp_builtin_tables_lookup_module(qstr q) {
return mp_builtin_tables_lookup(&builtin_module_table[0], q);
}

Wyświetl plik

@ -0,0 +1,2 @@
mp_obj_t mp_builtin_tables_lookup_object(qstr q);
mp_obj_t mp_builtin_tables_lookup_module(qstr q);

25
py/modarray.c 100644
Wyświetl plik

@ -0,0 +1,25 @@
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "map.h"
#include "builtin.h"
STATIC const mp_map_elem_t mp_module_array_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_array) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_type_array },
};
STATIC const mp_map_t mp_module_array_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_array_globals_table,
};
const mp_obj_module_t mp_module_array = {
.base = { &mp_type_module },
.name = MP_QSTR_array,
.globals = (mp_map_t*)&mp_module_array_globals,
};

Wyświetl plik

@ -0,0 +1,25 @@
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "map.h"
#include "builtin.h"
STATIC const mp_map_elem_t mp_module_collections_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_collections) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_namedtuple), (mp_obj_t)&mp_namedtuple_obj },
};
STATIC const mp_map_t mp_module_collections_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
};
const mp_obj_module_t mp_module_collections = {
.base = { &mp_type_module },
.name = MP_QSTR_collections,
.globals = (mp_map_t*)&mp_module_collections_globals,
};

Wyświetl plik

@ -1,4 +1,3 @@
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"

Wyświetl plik

@ -125,10 +125,16 @@ typedef double mp_float_t;
#define MICROPY_PATH_MAX (512)
#endif
// Additional builtin function definitions - see runtime.c:builtin_table for format.
// Additional builtin function definitions - see builtintables.c:builtin_object_table for format.
#ifndef MICROPY_EXTRA_BUILTINS
#define MICROPY_EXTRA_BUILTINS
#endif
// Additional builtin module definitions - see builtintables.c:builtin_module_table for format.
#ifndef MICROPY_EXTRA_BUILTIN_MODULES
#define MICROPY_EXTRA_BUILTIN_MODULES
#endif
/*****************************************************************************/
/* Miscellaneous settings */

Wyświetl plik

@ -417,7 +417,7 @@ void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *sto
extern const mp_obj_type_t zip_type;
// array
extern const mp_obj_type_t array_type;
extern const mp_obj_type_t mp_type_array;
uint mp_obj_array_len(mp_obj_t self_in);
mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items);
@ -454,9 +454,6 @@ typedef struct _mp_obj_module_t {
struct _mp_map_t *globals;
} mp_obj_module_t;
extern const mp_obj_type_t mp_type_module;
mp_obj_t mp_obj_new_module(qstr module_name);
mp_obj_t mp_obj_module_get(qstr module_name);
void mp_obj_module_register(qstr qstr, mp_obj_t module); //use for loading statically allocated modules
struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);
// staticmethod and classmethod types; defined here so we can make const versions

Wyświetl plik

@ -121,7 +121,7 @@ STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
assert(MP_OBJ_IS_TYPE(self_in, &array_type));
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array));
mp_obj_array_t *self = self_in;
if (self->free == 0) {
int item_sz = mp_binary_get_size(self->typecode);
@ -154,7 +154,7 @@ STATIC const mp_method_t array_type_methods[] = {
{ NULL, NULL },
};
const mp_obj_type_t array_type = {
const mp_obj_type_t mp_type_array = {
{ &mp_type_type },
.name = MP_QSTR_array,
.print = array_print,
@ -169,7 +169,7 @@ const mp_obj_type_t array_type = {
STATIC mp_obj_array_t *array_new(char typecode, uint n) {
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
o->base.type = &array_type;
o->base.type = &mp_type_array;
o->typecode = typecode;
o->free = 0;
o->len = n;
@ -190,7 +190,7 @@ mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
// Create bytearray which references specified memory area
mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
o->base.type = &array_type;
o->base.type = &mp_type_array;
o->typecode = BYTEARRAY_TYPECODE;
o->free = 0;
o->len = n;
@ -207,7 +207,7 @@ typedef struct _mp_obj_array_it_t {
machine_uint_t cur;
} mp_obj_array_it_t;
mp_obj_t array_it_iternext(mp_obj_t self_in) {
STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
mp_obj_array_it_t *self = self_in;
if (self->cur < self->array->len) {
return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
@ -222,7 +222,7 @@ STATIC const mp_obj_type_t array_it_type = {
.iternext = array_it_iternext,
};
mp_obj_t array_iterator_new(mp_obj_t array_in) {
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
mp_obj_array_t *array = array_in;
mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
o->base.type = &array_it_type;

Wyświetl plik

@ -6,9 +6,12 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objmodule.h"
#include "runtime.h"
#include "map.h"
#include "builtin.h"
#include "builtintables.h"
STATIC mp_map_t mp_loaded_modules_map; // TODO: expose as sys.modules
STATIC void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_module_t *self = self_in;
@ -39,7 +42,7 @@ const mp_obj_type_t mp_type_module = {
};
mp_obj_t mp_obj_new_module(qstr module_name) {
mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
mp_map_elem_t *el = mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
// We could error out if module already exists, but let C extensions
// add new members to existing modules.
if (el->value != MP_OBJ_NULL) {
@ -62,9 +65,27 @@ mp_obj_t mp_obj_new_module(qstr module_name) {
return o;
}
mp_obj_t mp_obj_module_get(qstr module_name) {
mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
mp_obj_module_t *self = self_in;
return self->globals;
}
/******************************************************************************/
// Global module table and related functions
void mp_module_init(void) {
mp_map_init(&mp_loaded_modules_map, 3);
}
void mp_module_deinit(void) {
mp_map_deinit(&mp_loaded_modules_map);
}
// returns MP_OBJ_NULL if not found
mp_obj_t mp_module_get(qstr module_name) {
// lookup module
mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
mp_map_elem_t *el = mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
// module found, return it
if (el != NULL) {
@ -72,23 +93,10 @@ mp_obj_t mp_obj_module_get(qstr module_name) {
}
// module not found, look for builtin module names
#if MICROPY_ENABLE_FLOAT
if (module_name == MP_QSTR_math) {
return (mp_obj_t)&mp_module_math;
}
#endif
// no module found, return NULL object
return MP_OBJ_NULL;
// it will return MP_OBJ_NULL if nothing found
return mp_builtin_tables_lookup_module(module_name);
}
void mp_obj_module_register(qstr qstr, mp_obj_t module)
{
mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
}
mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
mp_obj_module_t *self = self_in;
return self->globals;
void mp_module_register(qstr qstr, mp_obj_t module) {
mp_map_lookup(&mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
}

4
py/objmodule.h 100644
Wyświetl plik

@ -0,0 +1,4 @@
void mp_module_init(void);
void mp_module_deinit(void);
mp_obj_t mp_module_get(qstr module_name);
void mp_module_register(qstr qstr, mp_obj_t module);

Wyświetl plik

@ -74,8 +74,11 @@ PY_O_BASENAME = \
builtin.o \
builtinimport.o \
builtinevex.o \
builtinmp.o \
builtinmath.o \
builtintables.o \
modarray.o \
modcollections.o \
modmath.o \
modmicropython.o \
vm.o \
showbc.o \
repl.o \

Wyświetl plik

@ -12,12 +12,13 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objmodule.h"
#include "parsenum.h"
#include "runtime0.h"
#include "runtime.h"
#include "map.h"
#include "builtin.h"
#include "objarray.h"
#include "builtintables.h"
#include "bc.h"
#include "intdivmod.h"
@ -35,7 +36,6 @@
STATIC mp_map_t *map_locals;
STATIC mp_map_t *map_globals;
STATIC mp_map_t map_builtins;
STATIC mp_map_t map_loaded_modules; // TODO: expose as sys.modules
typedef enum {
MP_CODE_NONE,
@ -72,119 +72,6 @@ STATIC mp_code_t *unique_codes = NULL;
FILE *fp_write_code = NULL;
#endif
// builtins
// we put this table in ROM because it's always needed and takes up quite a bit of room in RAM
// in fact, it uses less ROM here in table form than the equivalent in code form initialising a dynamic mp_map_t object in RAM
// at the moment it's a linear table, but we could convert it to a const mp_map_t table with a simple preprocessing script
// if we wanted to allow dynamic modification of the builtins, we could provide an mp_map_t object which is searched before this one
typedef struct _mp_builtin_elem_t {
qstr qstr;
mp_obj_t fun;
} mp_builtin_elem_t;
STATIC const mp_builtin_elem_t builtin_table[] = {
// built-in core functions
{ MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
{ MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
{ MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
// built-in types
{ MP_QSTR_bool, (mp_obj_t)&bool_type },
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
#if MICROPY_ENABLE_FLOAT
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
#endif
{ MP_QSTR_dict, (mp_obj_t)&dict_type },
{ MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
{ MP_QSTR_filter, (mp_obj_t)&filter_type },
#if MICROPY_ENABLE_FLOAT
{ MP_QSTR_float, (mp_obj_t)&mp_type_float },
#endif
{ MP_QSTR_int, (mp_obj_t)&int_type },
{ MP_QSTR_list, (mp_obj_t)&list_type },
{ MP_QSTR_map, (mp_obj_t)&map_type },
{ MP_QSTR_object, (mp_obj_t)&mp_type_object },
{ MP_QSTR_set, (mp_obj_t)&set_type },
{ MP_QSTR_str, (mp_obj_t)&str_type },
{ MP_QSTR_super, (mp_obj_t)&super_type },
{ MP_QSTR_tuple, (mp_obj_t)&tuple_type },
{ MP_QSTR_type, (mp_obj_t)&mp_type_type },
{ MP_QSTR_zip, (mp_obj_t)&zip_type },
{ MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
{ MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
// built-in user functions
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
{ MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
{ MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
{ MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
{ MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
{ MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
{ MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
{ MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
{ MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
{ MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
{ MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
{ MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
{ MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
{ MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
{ MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
{ MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
{ MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
{ MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
{ MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
{ MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
{ MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
{ MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
{ MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
{ MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
{ MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
// built-in exceptions
{ MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
{ MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError },
{ MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
{ MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
{ MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError },
{ MP_QSTR_EOFError, (mp_obj_t)&mp_type_EOFError },
{ MP_QSTR_EnvironmentError, (mp_obj_t)&mp_type_EnvironmentError },
{ MP_QSTR_Exception, (mp_obj_t)&mp_type_Exception },
{ MP_QSTR_FloatingPointError, (mp_obj_t)&mp_type_FloatingPointError },
{ MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit },
{ MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError },
{ MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
{ MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
{ MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
{ MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
{ MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError },
{ MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError },
{ MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
{ MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
{ MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
{ MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
{ MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError },
{ MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError },
{ MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
{ MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError },
{ MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit },
{ MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError },
{ MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
{ MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError },
{ MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
{ MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
{ MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
// Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
// TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
// Extra builtins as defined by a port
MICROPY_EXTRA_BUILTINS
{ MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
};
// a good optimising compiler will inline this if necessary
STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
@ -193,23 +80,17 @@ STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
void rt_init(void) {
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
map_locals = map_globals = mp_map_new(1);
mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
// init built-in hash table
mp_map_init(&map_builtins, 3);
// init loaded modules table
mp_map_init(&map_loaded_modules, 3);
// init global module stuff
mp_module_init();
// built-in objects
// add some builtins that can't be done in ROM
mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
mp_obj_t m_collections = mp_obj_new_module(MP_QSTR_collections);
rt_store_attr(m_collections, MP_QSTR_namedtuple, (mp_obj_t)&mp_namedtuple_obj);
#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);
@ -221,10 +102,6 @@ void rt_init(void) {
//sys_path = mp_obj_new_list(0, NULL);
//rt_store_attr(m_sys, MP_QSTR_path, sys_path);
// we pre-import the micropython module
// probably shouldn't do this, so we are compatible with CPython
rt_store_name(MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython);
// TODO: wastes one mp_code_t structure in mem
next_unique_code_id = 1; // 0 indicates "no code"
unique_codes_alloc = 0;
@ -238,8 +115,8 @@ void rt_init(void) {
void rt_deinit(void) {
m_del(mp_code_t, unique_codes, unique_codes_alloc);
mp_map_free(map_globals);
mp_map_deinit(&map_loaded_modules);
mp_map_deinit(&map_builtins);
mp_module_deinit();
#ifdef WRITE_CODE
if (fp_write_code != NULL) {
fclose(fp_write_code);
@ -428,10 +305,9 @@ mp_obj_t rt_load_global(qstr qstr) {
if (elem == NULL) {
elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
if (elem == NULL) {
for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
if (e->qstr == qstr) {
return e->fun;
}
mp_obj_t o = mp_builtin_tables_lookup_object(qstr);
if (o != MP_OBJ_NULL) {
return o;
}
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
}
@ -441,10 +317,13 @@ mp_obj_t rt_load_global(qstr qstr) {
mp_obj_t rt_load_build_class(void) {
DEBUG_OP_printf("load_build_class\n");
// lookup __build_class__ in dynamic table of builtins first
mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
if (elem != NULL) {
// found user-defined __build_class__, return it
return elem->value;
} else {
// no user-defined __build_class__, return builtin one
return (mp_obj_t)&mp_builtin___build_class___obj;
}
}
@ -1141,10 +1020,6 @@ void rt_globals_set(mp_map_t *m) {
map_globals = m;
}
mp_map_t *rt_loaded_modules_get(void) {
return &map_loaded_modules;
}
// these must correspond to the respective enum
void *const rt_fun_table[RT_F_NUMBER_OF] = {
rt_load_const_dec,

Wyświetl plik

@ -24,6 +24,7 @@
#include "lexerfatfs.h"
#include "parse.h"
#include "obj.h"
#include "objmodule.h"
#include "parsehelper.h"
#include "compile.h"
#include "runtime0.h"
@ -273,7 +274,7 @@ soft_reset:
rt_store_name(MP_QSTR_open, rt_make_function_n(2, pyb_io_open));
// load the pyb module
mp_obj_module_register(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
mp_module_register(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
// check if user switch held (initiates reset of filesystem)
bool reset_filesystem = false;

Wyświetl plik

@ -181,7 +181,8 @@ STATIC mp_obj_t pyb_standby(void) {
MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
mp_obj_t *items;
mp_obj_get_array_fixed_n(arg, 4, &items);
uint8_t data[4];
data[0] = mp_obj_get_int(items[0]);
data[1] = mp_obj_get_int(items[1]);

Wyświetl plik

@ -74,9 +74,9 @@ SRC_C = \
pyexec.c \
help.c \
input.c \
pybmodule.c \
osmodule.c \
timemodule.c \
modpyb.c \
modos.c \
modtime.c \
import.c \
lexerfatfs.c \
gpio.c \

Wyświetl plik

@ -21,9 +21,6 @@
#include "gc.h"
#include "gccollect.h"
#include "pyexec.h"
#include "pybmodule.h"
#include "osmodule.h"
#include "timemodule.h"
#include "usart.h"
#include "led.h"
#include "exti.h"
@ -110,6 +107,7 @@ static const char fresh_boot_py[] =
"# boot.py -- run on boot-up\n"
"# can run arbitrary Python, but best to keep it minimal\n"
"\n"
"import pyb\n"
"pyb.source_dir('/src')\n"
"pyb.main('main.py')\n"
"#pyb.usb_usr('VCP')\n"
@ -246,15 +244,6 @@ soft_reset:
pin_map_init();
// we pre-import the pyb module
// probably shouldn't do this, so we are compatible with CPython
rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
// pre-import the os and time modules
// TODO don't do this! (need a way of registering builtin modules...)
rt_store_name(MP_QSTR_os, (mp_obj_t)&os_module);
rt_store_name(MP_QSTR_time, (mp_obj_t)&time_module);
// check if user switch held (initiates reset of filesystem)
bool reset_filesystem = false;
#if MICROPY_HW_HAS_SWITCH

Wyświetl plik

@ -10,7 +10,7 @@
#include "rng.h"
#include "storage.h"
#include "ff.h"
#include "osmodule.h"
#include "modos.h"
#if _USE_LFN
static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */

Wyświetl plik

@ -30,7 +30,7 @@
#include "usb.h"
#include "i2c.h"
#endif
#include "pybmodule.h"
#include "modpyb.h"
#include "ff.h"
// get lots of info about the board

Wyświetl plik

@ -6,7 +6,7 @@
#include "qstr.h"
#include "obj.h"
#include "map.h"
#include "timemodule.h"
#include "modtime.h"
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
#if MICROPY_ENABLE_FLOAT

Wyświetl plik

@ -27,6 +27,15 @@ extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
{ MP_QSTR_input, (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj },
// extra built in modules to add to the list of known ones
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t pyb_module;
extern const struct _mp_obj_module_t time_module;
#define MICROPY_EXTRA_BUILTIN_MODULES \
{ MP_QSTR_os, (mp_obj_t)&os_module }, \
{ MP_QSTR_pyb, (mp_obj_t)&pyb_module }, \
{ MP_QSTR_time, (mp_obj_t)&time_module }, \
// type definitions for the specific machine
#define BYTES_PER_WORD (4)