summaryrefslogtreecommitdiffstatshomepage
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
m---------lib/axtls0
-rw-r--r--lib/timeutils/timeutils.c2
-rw-r--r--lib/utils/printf.c11
-rw-r--r--lib/utils/pyexec.c49
-rw-r--r--lib/utils/pyhelp.c85
-rw-r--r--lib/utils/pyhelp.h33
6 files changed, 159 insertions, 21 deletions
diff --git a/lib/axtls b/lib/axtls
-Subproject 532d2302b8f32a35c6ade41563127f61291361f
+Subproject c2690dc100f272cb01375dcf2f2be7105d0fb7c
diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c
index 94bdada980..518f570090 100644
--- a/lib/timeutils/timeutils.c
+++ b/lib/timeutils/timeutils.c
@@ -32,7 +32,7 @@
// LEAPOCH corresponds to 2000-03-01, which is a mod-400 year, immediately
// after Feb 29. We calculate seconds as a signed integer relative to that.
//
-// Our timebase is is relative to 2000-01-01.
+// Our timebase is relative to 2000-01-01.
#define LEAPOCH ((31 + 29) * 86400)
diff --git a/lib/utils/printf.c b/lib/utils/printf.c
index 136056a3e2..308525b6e8 100644
--- a/lib/utils/printf.c
+++ b/lib/utils/printf.c
@@ -59,14 +59,11 @@ int vprintf(const char *fmt, va_list ap) {
int DEBUG_printf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
- #if defined(MICROPY_DEBUG_STDERR) && MICROPY_DEBUG_STDERR
- // Printing debug to stderr may give a chance tests which
- // check stdout to pass, etc.
- extern const mp_print_t mp_stderr_print;
- int ret = mp_vprintf(&mp_stderr_print, fmt, ap);
- #else
- int ret = mp_vprintf(&mp_plat_print, fmt, ap);
+ #ifndef MICROPY_DEBUG_PRINTER_DEST
+ #define MICROPY_DEBUG_PRINTER_DEST mp_plat_print
#endif
+ extern const mp_print_t MICROPY_DEBUG_PRINTER_DEST;
+ int ret = mp_vprintf(&MICROPY_DEBUG_PRINTER_DEST, fmt, ap);
va_end(ap);
return ret;
}
diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c
index 8afa3813cd..ddecb6a8d8 100644
--- a/lib/utils/pyexec.c
+++ b/lib/utils/pyexec.c
@@ -50,22 +50,33 @@ STATIC bool repl_display_debugging_info = 0;
#define EXEC_FLAG_PRINT_EOF (1)
#define EXEC_FLAG_ALLOW_DEBUGGING (2)
#define EXEC_FLAG_IS_REPL (4)
+#define EXEC_FLAG_SOURCE_IS_RAW_CODE (8)
// parses, compiles and executes the code in the lexer
// frees the lexer before returning
// EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
// EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
// EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
-STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, int exec_flags) {
+STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, int exec_flags) {
int ret = 0;
uint32_t start = 0;
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
- // parse and compile the script
- qstr source_name = lex->source_name;
- mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
- mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
+ mp_obj_t module_fun;
+ #if MICROPY_MODULE_FROZEN_MPY
+ if (exec_flags & EXEC_FLAG_SOURCE_IS_RAW_CODE) {
+ // source is a raw_code object, create the function
+ module_fun = mp_make_function_from_raw_code(source, MP_OBJ_NULL, MP_OBJ_NULL);
+ } else
+ #endif
+ {
+ // source is a lexer, parse and compile the script
+ mp_lexer_t *lex = source;
+ qstr source_name = lex->source_name;
+ mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
+ module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
+ }
// execute code
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
@@ -99,7 +110,6 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki
if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) {
mp_uint_t ticks = mp_hal_ticks_ms() - start; // TODO implement a function that does this properly
printf("took " UINT_FMT " ms\n", ticks);
- gc_collect();
// qstr info
{
mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
@@ -107,8 +117,11 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki
printf("qstr:\n n_pool=" UINT_FMT "\n n_qstr=" UINT_FMT "\n n_str_data_bytes=" UINT_FMT "\n n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
}
- // GC info
+ #if MICROPY_ENABLE_GC
+ // run collection and print GC info
+ gc_collect();
gc_dump_info();
+ #endif
}
if (exec_flags & EXEC_FLAG_PRINT_EOF) {
@@ -488,14 +501,24 @@ int pyexec_file(const char *filename) {
#if MICROPY_MODULE_FROZEN
int pyexec_frozen_module(const char *name) {
- mp_lexer_t *lex = mp_find_frozen_module(name, strlen(name));
+ void *frozen_data;
+ int frozen_type = mp_find_frozen_module(name, strlen(name), &frozen_data);
- if (lex == NULL) {
- printf("could not find module '%s'\n", name);
- return false;
- }
+ switch (frozen_type) {
+ #if MICROPY_MODULE_FROZEN_STR
+ case MP_FROZEN_STR:
+ return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, 0);
+ #endif
- return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
+ #if MICROPY_MODULE_FROZEN_MPY
+ case MP_FROZEN_MPY:
+ return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_RAW_CODE);
+ #endif
+
+ default:
+ printf("could not find module '%s'\n", name);
+ return false;
+ }
}
#endif
diff --git a/lib/utils/pyhelp.c b/lib/utils/pyhelp.c
new file mode 100644
index 0000000000..5c0b2c57b7
--- /dev/null
+++ b/lib/utils/pyhelp.c
@@ -0,0 +1,85 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013-2016 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+
+#include "lib/utils/pyhelp.h"
+
+STATIC void pyhelp_print_info_about_object(mp_obj_t name_o, mp_obj_t value) {
+ printf(" ");
+ mp_obj_print(name_o, PRINT_STR);
+ printf(" -- ");
+ mp_obj_print(value, PRINT_STR);
+ printf("\n");
+}
+
+// Helper for 1-argument form of builtin help
+//
+// Typically a port will define a help function thus:
+//
+// STATIC const char *const myport_help_text =
+// "Welcome to MicroPython!\n"
+// "\n"
+// "...information specific to this port e.g. modules available...";
+//
+// STATIC mp_obj_t myport_help(mp_uint_t n_args, const mp_obj_t *args) {
+// if (n_args == 0) {
+// printf("%s", myport_help_text);
+// } else {
+// pyhelp_print_obj(args[0]);
+// }
+// return mp_const_none;
+// }
+// MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, myport_help);
+//
+void pyhelp_print_obj(const mp_obj_t obj) {
+ // try to print something sensible about the given object
+ printf("object ");
+ mp_obj_print(obj, PRINT_STR);
+ printf(" is of type %s\n", mp_obj_get_type_str(obj));
+
+ mp_map_t *map = NULL;
+ if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
+ map = mp_obj_dict_get_map(mp_obj_module_get_globals(obj));
+ } else {
+ mp_obj_type_t *type;
+ if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
+ type = obj;
+ } else {
+ type = mp_obj_get_type(obj);
+ }
+ if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
+ map = mp_obj_dict_get_map(type->locals_dict);
+ }
+ }
+ if (map != NULL) {
+ for (uint i = 0; i < map->alloc; i++) {
+ if (map->table[i].key != MP_OBJ_NULL) {
+ pyhelp_print_info_about_object(map->table[i].key, map->table[i].value);
+ }
+ }
+ }
+}
diff --git a/lib/utils/pyhelp.h b/lib/utils/pyhelp.h
new file mode 100644
index 0000000000..ee313ee37a
--- /dev/null
+++ b/lib/utils/pyhelp.h
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013-2016 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef __MICROPY_INCLUDED_LIB_UTILS_PYHELP_H__
+#define __MICROPY_INCLUDED_LIB_UTILS_PYHELP_H__
+
+#include "py/obj.h"
+
+void pyhelp_print_obj(const mp_obj_t obj);
+
+#endif // __MICROPY_INCLUDED_LIB_UTILS_PYHELP_H__