summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-12-26 12:32:33 +0000
committerDamien George <damien.p.george@gmail.com>2015-12-26 12:32:33 +0000
commit84b245f187f9711357b1fd46bebc67266bd028e5 (patch)
tree1df85f4a6a32cd2742ec0ff1690ff4afa919f7d9
parent7203b58e876cffe9bb7246e17c206e7b4280f701 (diff)
downloadmicropython-84b245f187f9711357b1fd46bebc67266bd028e5.tar.gz
micropython-84b245f187f9711357b1fd46bebc67266bd028e5.zip
lib/utils: Add pyexec_frozen_module to load and execute frozen module.
This is a convenience function similar to pyexec_file. It should be used instead of raw mp_parse_compile_execute because the latter does not catch and report exceptions.
-rw-r--r--esp8266/main.c4
-rw-r--r--lib/utils/pyexec.c15
-rw-r--r--lib/utils/pyexec.h1
-rw-r--r--teensy/main.c15
4 files changed, 19 insertions, 16 deletions
diff --git a/esp8266/main.c b/esp8266/main.c
index 178ad8b7c2..051ff6f6e2 100644
--- a/esp8266/main.c
+++ b/esp8266/main.c
@@ -32,7 +32,6 @@
#include "py/runtime0.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
-#include "py/frozenmod.h"
#include "py/mphal.h"
#include "py/gc.h"
#include "lib/utils/pyexec.h"
@@ -49,8 +48,7 @@ STATIC void mp_reset(void) {
mp_obj_list_init(mp_sys_path, 0);
mp_obj_list_init(mp_sys_argv, 0);
#if MICROPY_MODULE_FROZEN
- mp_lexer_t *lex = mp_find_frozen_module("main", 4);
- mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
+ pyexec_frozen_module("main");
#endif
}
diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c
index 295d059433..5e90b77d46 100644
--- a/lib/utils/pyexec.c
+++ b/lib/utils/pyexec.c
@@ -27,12 +27,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
+#include <string.h>
#include "py/nlr.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/repl.h"
#include "py/gc.h"
+#include "py/frozenmod.h"
#include "py/mphal.h"
#if defined(USE_DEVICE_MODE)
#include "irq.h"
@@ -476,6 +478,19 @@ int pyexec_file(const char *filename) {
return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
}
+#if MICROPY_MODULE_FROZEN
+int pyexec_frozen_module(const char *name) {
+ mp_lexer_t *lex = mp_find_frozen_module(name, strlen(name));
+
+ if (lex == NULL) {
+ printf("could not find module '%s'\n", name);
+ return false;
+ }
+
+ return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
+}
+#endif
+
mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
repl_display_debugging_info = mp_obj_get_int(o_value);
return mp_const_none;
diff --git a/lib/utils/pyexec.h b/lib/utils/pyexec.h
index 96bb481a54..cf44530c59 100644
--- a/lib/utils/pyexec.h
+++ b/lib/utils/pyexec.h
@@ -39,6 +39,7 @@ extern pyexec_mode_kind_t pyexec_mode_kind;
int pyexec_raw_repl(void);
int pyexec_friendly_repl(void);
int pyexec_file(const char *filename);
+int pyexec_frozen_module(const char *name);
void pyexec_event_repl_init(void);
int pyexec_event_repl_process_char(int c);
diff --git a/teensy/main.c b/teensy/main.c
index ac73259095..b630e88867 100644
--- a/teensy/main.c
+++ b/teensy/main.c
@@ -22,11 +22,6 @@
#include "uart.h"
#include "pin.h"
-#if MICROPY_MODULE_FROZEN
-#include "py/compile.h"
-#include "py/frozenmod.h"
-#endif
-
extern uint32_t _heap_start;
void flash_error(int n) {
@@ -306,10 +301,7 @@ soft_reset:
#endif
#if MICROPY_MODULE_FROZEN
- {
- mp_lexer_t *lex = mp_find_frozen_module("boot", 4);
- mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
- }
+ pyexec_frozen_module("boot");
#else
if (!pyexec_file("/boot.py")) {
flash_error(4);
@@ -321,10 +313,7 @@ soft_reset:
// run main script
#if MICROPY_MODULE_FROZEN
- {
- mp_lexer_t *lex = mp_find_frozen_module("main", 4);
- mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
- }
+ pyexec_frozen_module("main");
#else
{
vstr_t *vstr = vstr_new();