Skip to content

Commit 84b245f

Browse files
committed
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.
1 parent 7203b58 commit 84b245f

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

esp8266/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "py/runtime0.h"
3333
#include "py/runtime.h"
3434
#include "py/stackctrl.h"
35-
#include "py/frozenmod.h"
3635
#include "py/mphal.h"
3736
#include "py/gc.h"
3837
#include "lib/utils/pyexec.h"
@@ -49,8 +48,7 @@ STATIC void mp_reset(void) {
4948
mp_obj_list_init(mp_sys_path, 0);
5049
mp_obj_list_init(mp_sys_argv, 0);
5150
#if MICROPY_MODULE_FROZEN
52-
mp_lexer_t *lex = mp_find_frozen_module("main", 4);
53-
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
51+
pyexec_frozen_module("main");
5452
#endif
5553
}
5654

lib/utils/pyexec.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
#include <stdlib.h>
2828
#include <stdio.h>
2929
#include <stdint.h>
30+
#include <string.h>
3031

3132
#include "py/nlr.h"
3233
#include "py/compile.h"
3334
#include "py/runtime.h"
3435
#include "py/repl.h"
3536
#include "py/gc.h"
37+
#include "py/frozenmod.h"
3638
#include "py/mphal.h"
3739
#if defined(USE_DEVICE_MODE)
3840
#include "irq.h"
@@ -476,6 +478,19 @@ int pyexec_file(const char *filename) {
476478
return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
477479
}
478480

481+
#if MICROPY_MODULE_FROZEN
482+
int pyexec_frozen_module(const char *name) {
483+
mp_lexer_t *lex = mp_find_frozen_module(name, strlen(name));
484+
485+
if (lex == NULL) {
486+
printf("could not find module '%s'\n", name);
487+
return false;
488+
}
489+
490+
return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
491+
}
492+
#endif
493+
479494
mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
480495
repl_display_debugging_info = mp_obj_get_int(o_value);
481496
return mp_const_none;

lib/utils/pyexec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern pyexec_mode_kind_t pyexec_mode_kind;
3939
int pyexec_raw_repl(void);
4040
int pyexec_friendly_repl(void);
4141
int pyexec_file(const char *filename);
42+
int pyexec_frozen_module(const char *name);
4243
void pyexec_event_repl_init(void);
4344
int pyexec_event_repl_process_char(int c);
4445

teensy/main.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include "uart.h"
2323
#include "pin.h"
2424

25-
#if MICROPY_MODULE_FROZEN
26-
#include "py/compile.h"
27-
#include "py/frozenmod.h"
28-
#endif
29-
3025
extern uint32_t _heap_start;
3126

3227
void flash_error(int n) {
@@ -306,10 +301,7 @@ int main(void) {
306301
#endif
307302

308303
#if MICROPY_MODULE_FROZEN
309-
{
310-
mp_lexer_t *lex = mp_find_frozen_module("boot", 4);
311-
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
312-
}
304+
pyexec_frozen_module("boot");
313305
#else
314306
if (!pyexec_file("/boot.py")) {
315307
flash_error(4);
@@ -321,10 +313,7 @@ int main(void) {
321313

322314
// run main script
323315
#if MICROPY_MODULE_FROZEN
324-
{
325-
mp_lexer_t *lex = mp_find_frozen_module("main", 4);
326-
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
327-
}
316+
pyexec_frozen_module("main");
328317
#else
329318
{
330319
vstr_t *vstr = vstr_new();

0 commit comments

Comments
 (0)