Merge pull request #1167 from notro/cpython_stdlib

Support CPython standard library
This commit is contained in:
Scott Shawcroft 2018-10-24 12:57:41 -07:00 committed by GitHub
commit dc9d338612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 221 additions and 19 deletions

View File

@ -20,8 +20,6 @@
#define MICROPY_COMP_CONST (1)
#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (1)
#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1)
// Turn off for consistency
#define MICROPY_CPYTHON_COMPAT (0)
#define MICROPY_MEM_STATS (0)
#define MICROPY_DEBUG_PRINTERS (0)
#define MICROPY_ENABLE_GC (1)
@ -57,7 +55,6 @@
#define MICROPY_PY_DESCRIPTORS (1)
#define MICROPY_PY_MATH (0)
#define MICROPY_PY_CMATH (0)
#define MICROPY_PY_IO (0)
#define MICROPY_PY_URANDOM (0)
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (0)
#define MICROPY_PY_STRUCT (0)
@ -84,7 +81,6 @@
#define MICROPY_VFS (1)
#define MICROPY_VFS_FAT (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_MODULE_WEAK_LINKS (0)
#define MICROPY_REPL_AUTO_INDENT (1)
#define MICROPY_HW_ENABLE_DAC (1)
#define MICROPY_ENABLE_FINALISER (1)
@ -140,15 +136,33 @@ typedef long mp_off_t;
#include "include/sam.h"
#ifdef SAMD21
#define CIRCUITPY_MCU_FAMILY samd21
#define CIRCUITPY_MCU_FAMILY samd21
#define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21"
#define PORT_HEAP_SIZE (16384 + 4096)
#define PORT_HEAP_SIZE (16384 + 4096)
#define CIRCUITPY_DEFAULT_STACK_SIZE 4096
#define MICROPY_CPYTHON_COMPAT (0)
#define MICROPY_MODULE_WEAK_LINKS (0)
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (0)
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (0)
#define MICROPY_PY_FUNCTION_ATTRS (0)
#define MICROPY_PY_IO (0)
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (0)
#define MICROPY_PY_SYS_EXC_INFO (0)
#endif
#ifdef SAMD51
#define CIRCUITPY_MCU_FAMILY samd51
#define CIRCUITPY_MCU_FAMILY samd51
#define MICROPY_PY_SYS_PLATFORM "MicroChip SAMD51"
#define PORT_HEAP_SIZE (0x20000) // 128KiB
#define PORT_HEAP_SIZE (0x20000) // 128KiB
#define CIRCUITPY_DEFAULT_STACK_SIZE 8192
#define MICROPY_CPYTHON_COMPAT (1)
#define MICROPY_MODULE_WEAK_LINKS (1)
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1)
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
#define MICROPY_PY_FUNCTION_ATTRS (1)
#define MICROPY_PY_IO (1)
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
#define MICROPY_PY_SYS_EXC_INFO (1)
#endif
#ifdef LONGINT_IMPL_NONE
@ -290,6 +304,34 @@ extern const struct _mp_obj_module_t usb_hid_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
#elif MICROPY_MODULE_WEAK_LINKS
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&os_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_rtc), (mp_obj_t)&rtc_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&struct_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR__time), (mp_obj_t)&time_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, \
TOUCHIO_MODULE \
EXTRA_BUILTIN_MODULES
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
{ MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) }, \
{ MP_ROM_QSTR(MP_QSTR_io), MP_ROM_PTR(&mp_module_io) }, \
{ MP_ROM_QSTR(MP_QSTR_os), MP_ROM_PTR(&os_module) }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
#else
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \
@ -360,8 +402,4 @@ void run_background_tasks(void);
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
// TODO(tannewt): Make this 6k+ for any non-express M4 boards because they cache sectors on the
// stack.
#define CIRCUITPY_DEFAULT_STACK_SIZE 4096
#endif // __INCLUDED_MPCONFIGPORT_H

10
py/builtinimport.c Executable file → Normal file
View File

@ -155,11 +155,9 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
#endif
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code, const char *filename) {
#if MICROPY_PY___FILE__
// TODO
//qstr source_name = lex->source_name;
//mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(qstr_from_str(filename)));
#endif
// execute the module in its context
@ -222,7 +220,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// its data) in the list of frozen files, execute it.
#if MICROPY_MODULE_FROZEN_MPY
if (frozen_type == MP_FROZEN_MPY) {
do_execute_raw_code(module_obj, modref);
do_execute_raw_code(module_obj, modref, file_str);
return;
}
#endif
@ -235,7 +233,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
#if MICROPY_PERSISTENT_CODE_LOAD
if (file_str[file->len - 3] == 'm') {
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
do_execute_raw_code(module_obj, raw_code);
do_execute_raw_code(module_obj, raw_code, file_str);
return;
}
#endif

View File

@ -135,7 +135,7 @@ STATIC mp_obj_t mp_sys_exc_info(void) {
t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
t->items[1] = cur_exc;
t->items[2] = mp_const_none;
t->items[2] = mp_obj_exception_get_traceback_obj(cur_exc);
return MP_OBJ_FROM_PTR(t);
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);

View File

@ -717,6 +717,7 @@ bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
mp_obj_t mp_obj_exception_get_traceback_obj(mp_obj_t self_in);
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);

View File

@ -92,6 +92,9 @@ STATIC void bound_meth_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
if (attr == MP_QSTR___name__) {
mp_obj_bound_meth_t *o = MP_OBJ_TO_PTR(self_in);
dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(o->meth));
} else if (attr == MP_QSTR___func__) {
mp_obj_bound_meth_t *o = MP_OBJ_TO_PTR(self_in);
dest[0] = o->meth;
}
}
#endif

View File

@ -30,6 +30,7 @@
#include <stdio.h>
#include "py/objlist.h"
#include "py/objnamedtuple.h"
#include "py/objstr.h"
#include "py/objtuple.h"
#include "py/objtype.h"
@ -531,3 +532,164 @@ void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values
*values = self->traceback_data;
}
}
#if MICROPY_PY_SYS_EXC_INFO
STATIC const mp_obj_namedtuple_type_t code_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.name = MP_QSTR_code,
.print = namedtuple_print,
.make_new = namedtuple_make_new,
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.attr = namedtuple_attr,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
.parent = &mp_type_tuple,
},
.n_fields = 15,
.fields = {
MP_QSTR_co_argcount,
MP_QSTR_co_kwonlyargcount,
MP_QSTR_co_nlocals,
MP_QSTR_co_stacksize,
MP_QSTR_co_flags,
MP_QSTR_co_code,
MP_QSTR_co_consts,
MP_QSTR_co_names,
MP_QSTR_co_varnames,
MP_QSTR_co_freevars,
MP_QSTR_co_cellvars,
MP_QSTR_co_filename,
MP_QSTR_co_name,
MP_QSTR_co_firstlineno,
MP_QSTR_co_lnotab,
},
};
STATIC mp_obj_t code_make_new(qstr file, qstr block) {
mp_obj_t elems[15] = {
mp_obj_new_int(0), // co_argcount
mp_obj_new_int(0), // co_kwonlyargcount
mp_obj_new_int(0), // co_nlocals
mp_obj_new_int(0), // co_stacksize
mp_obj_new_int(0), // co_flags
mp_obj_new_bytearray(0, NULL), // co_code
mp_obj_new_tuple(0, NULL), // co_consts
mp_obj_new_tuple(0, NULL), // co_names
mp_obj_new_tuple(0, NULL), // co_varnames
mp_obj_new_tuple(0, NULL), // co_freevars
mp_obj_new_tuple(0, NULL), // co_cellvars
MP_OBJ_NEW_QSTR(file), // co_filename
MP_OBJ_NEW_QSTR(block), // co_name
mp_obj_new_int(1), // co_firstlineno
mp_obj_new_bytearray(0, NULL), // co_lnotab
};
return namedtuple_make_new((const mp_obj_type_t*)&code_type_obj, 15, 0, elems);
}
STATIC const mp_obj_namedtuple_type_t frame_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.name = MP_QSTR_frame,
.print = namedtuple_print,
.make_new = namedtuple_make_new,
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.attr = namedtuple_attr,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
.parent = &mp_type_tuple,
},
.n_fields = 8,
.fields = {
MP_QSTR_f_back,
MP_QSTR_f_builtins,
MP_QSTR_f_code,
MP_QSTR_f_globals,
MP_QSTR_f_lasti,
MP_QSTR_f_lineno,
MP_QSTR_f_locals,
MP_QSTR_f_trace,
},
};
STATIC mp_obj_t frame_make_new(mp_obj_t f_code, int f_lineno) {
mp_obj_t elems[8] = {
mp_const_none, // f_back
mp_obj_new_dict(0), // f_builtins
f_code, // f_code
mp_obj_new_dict(0), // f_globals
mp_obj_new_int(0), // f_lasti
mp_obj_new_int(f_lineno), // f_lineno
mp_obj_new_dict(0), // f_locals
mp_const_none, // f_trace
};
return namedtuple_make_new((const mp_obj_type_t*)&frame_type_obj, 8, 0, elems);
}
STATIC const mp_obj_namedtuple_type_t traceback_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.name = MP_QSTR_traceback,
.print = namedtuple_print,
.make_new = namedtuple_make_new,
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.attr = namedtuple_attr,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
.parent = &mp_type_tuple,
},
.n_fields = 4,
.fields = {
MP_QSTR_tb_frame,
MP_QSTR_tb_lasti,
MP_QSTR_tb_lineno,
MP_QSTR_tb_next,
},
};
STATIC mp_obj_t traceback_from_values(size_t *values, mp_obj_t tb_next) {
int lineno = values[1];
mp_obj_t elems[4] = {
frame_make_new(code_make_new(values[0], values[2]), lineno),
mp_obj_new_int(0),
mp_obj_new_int(lineno),
tb_next,
};
return namedtuple_make_new((const mp_obj_type_t*)&traceback_type_obj, 4, 0, elems);
};
mp_obj_t mp_obj_exception_get_traceback_obj(mp_obj_t self_in) {
mp_obj_exception_t *self = MP_OBJ_TO_PTR(self_in);
if (!mp_obj_is_exception_instance(self)) {
return mp_const_none;
}
size_t n, *values;
mp_obj_exception_get_traceback(self, &n, &values);
if (n == 0) {
return mp_const_none;
}
mp_obj_t tb_next = mp_const_none;
for (size_t i = 0; i < n; i += 3) {
tb_next = traceback_from_values(&values[i], tb_next);
}
return tb_next;
}
#endif