From 8a96ebea75158987c1136f1a618d98cf387445fd Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Jun 2014 20:54:22 +0300 Subject: py: Move stack_ctrl_init() to mp_init(). As stack checking is enabled by default, ports which don't call stack_ctrl_init() are broken now (report RuntimeError on startup). Save them trouble and just init stack control framework in interpreter init. --- py/runtime.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'py') diff --git a/py/runtime.c b/py/runtime.c index b539984c0..f08ff9ff4 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -45,6 +45,7 @@ #include "smallint.h" #include "objgenerator.h" #include "lexer.h" +#include "stackctrl.h" #if 0 // print debugging info #define DEBUG_PRINT (1) @@ -69,6 +70,8 @@ const mp_obj_module_t mp_module___main__ = { }; void mp_init(void) { + stack_ctrl_init(); + // call port specific initialization if any #ifdef MICROPY_PORT_INIT_FUNC MICROPY_PORT_INIT_FUNC; -- cgit v1.2.3 From cb78f862cb595bf1e4f164869fe44a4ee7755a55 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Jun 2014 20:39:09 +0300 Subject: py: Allow to disable array module and bytearray type. array.array and bytearray share big deal of code, so to get real savings, both need to be disabled. --- py/builtintables.c | 4 ++++ py/modarray.c | 4 ++++ py/mpconfig.h | 12 ++++++++++++ py/objarray.c | 4 ++++ 4 files changed, 24 insertions(+) (limited to 'py') diff --git a/py/builtintables.c b/py/builtintables.c index 8b4df9317..c42cdf89b 100644 --- a/py/builtintables.c +++ b/py/builtintables.c @@ -43,7 +43,9 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = { // built-in types { MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool }, { MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes }, +#if MICROPY_PY_BUILTINS_BYTEARRAY { MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray }, +#endif #if MICROPY_PY_BUILTINS_COMPLEX { MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex }, #endif @@ -160,7 +162,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___main__), (mp_obj_t)&mp_module___main__ }, { MP_OBJ_NEW_QSTR(MP_QSTR_micropython), (mp_obj_t)&mp_module_micropython }, +#if MICROPY_PY_ARRAY { MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_module_array }, +#endif #if MICROPY_PY_IO { MP_OBJ_NEW_QSTR(MP_QSTR__io), (mp_obj_t)&mp_module_io }, #endif diff --git a/py/modarray.c b/py/modarray.c index 3ff567f1d..c0fe33164 100644 --- a/py/modarray.c +++ b/py/modarray.c @@ -30,6 +30,8 @@ #include "obj.h" #include "builtin.h" +#if MICROPY_PY_ARRAY + 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 }, @@ -51,3 +53,5 @@ const mp_obj_module_t mp_module_array = { .name = MP_QSTR_array, .globals = (mp_obj_dict_t*)&mp_module_array_globals, }; + +#endif diff --git a/py/mpconfig.h b/py/mpconfig.h index d7504c140..5b27b910e 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -249,6 +249,11 @@ typedef double mp_float_t; /*****************************************************************************/ /* Fine control over Python builtins, classes, modules, etc */ +// Whether to support bytearray object +#ifndef MICROPY_PY_BUILTINS_BYTEARRAY +#define MICROPY_PY_BUILTINS_BYTEARRAY (1) +#endif + // Whether to support set object #ifndef MICROPY_PY_BUILTINS_SET #define MICROPY_PY_BUILTINS_SET (1) @@ -269,6 +274,13 @@ typedef double mp_float_t; #define MICROPY_PY_BUILTINS_PROPERTY (1) #endif +// Whether to provide "array" module. Note that large chunk of the +// underlying code is shared with "bytearray" builtin type, so to +// get real savings, it should be disabled too. +#ifndef MICROPY_PY_ARRAY +#define MICROPY_PY_ARRAY (1) +#endif + // Whether to provide "collections" module #ifndef MICROPY_PY_COLLECTIONS #define MICROPY_PY_COLLECTIONS (1) diff --git a/py/objarray.c b/py/objarray.c index 05821e8de..b13df2bdb 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -37,6 +37,8 @@ #include "runtime.h" #include "binary.h" +#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY + typedef struct _mp_obj_array_t { mp_obj_base_t base; machine_uint_t typecode : 8; @@ -310,3 +312,5 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) { o->cur = 0; return o; } + +#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY -- cgit v1.2.3 From 8993fb6cf0677ce980ab56cbad326e4e6bc47811 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 28 Jun 2014 02:25:04 +0300 Subject: py: Add protection against printing too nested or recursive data structures. With a test which cannot be automatically validated so far. --- py/obj.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'py') diff --git a/py/obj.c b/py/obj.c index a0f55d65d..7e39c5a5c 100644 --- a/py/obj.c +++ b/py/obj.c @@ -35,6 +35,7 @@ #include "obj.h" #include "runtime0.h" #include "runtime.h" +#include "stackctrl.h" mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) { if (MP_OBJ_IS_SMALL_INT(o_in)) { @@ -59,6 +60,8 @@ void printf_wrapper(void *env, const char *fmt, ...) { } void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { + // There can be data structures nested too deep, or just recursive + STACK_CHECK(); #if !NDEBUG if (o_in == NULL) { print(env, "(nil)"); -- cgit v1.2.3