Skip to content
This repository was archived by the owner on Apr 25, 2021. It is now read-only.

Commit bdb8897

Browse files
committed
pretty traceback
1 parent 2a940c7 commit bdb8897

3 files changed

Lines changed: 48 additions & 24 deletions

File tree

src/machine.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <string.h>
2+
13
#include "py/nlr.h"
24
#include "py/runtime.h"
35
#include "py/objexcept.h"
@@ -102,7 +104,7 @@ void nlr_jump_fail(void *val) {
102104
}
103105

104106
void NORETURN __fatal_error(const char *msg) {
105-
__syscall2(SYS_CONTROL, SYS_CONTROL_CRASH, (int) msg);
107+
__syscall3(SYS_CONTROL, SYS_CONTROL_CRASH, (int) msg, (int) strlen(msg));
106108
for (;;);
107109
}
108110

src/main.c

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "py/repl.h"
99
#include "py/runtime.h"
1010
#include "py/stackctrl.h"
11+
#include "py/frozenmod.h"
1112
#include "lib/utils/pyexec.h"
1213
#include "gccollect.h"
1314
#include "machine.h"
@@ -36,9 +37,18 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
3637
}
3738
}
3839

40+
void do_frozen(const char *name) {
41+
void *frozen_data;
42+
int frozen_type = mp_find_frozen_module(name, strlen(name), &frozen_data);
43+
if (frozen_type != MP_FROZEN_MPY)
44+
__fatal_error("frozen_type != MP_FROZEN_MPY");
45+
46+
mp_obj_t module_fun = mp_make_function_from_raw_code(frozen_data, MP_OBJ_NULL, MP_OBJ_NULL);
47+
mp_call_function_0(module_fun);
48+
}
49+
3950
int main(int argc, char **argv) {
4051
nlr_buf_t nlr;
41-
int code;
4252

4353
if (nlr_push(&nlr) == 0) {
4454
mp_stack_set_top((uint8_t * ) & _estack);
@@ -57,28 +67,42 @@ int main(int argc, char **argv) {
5767
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
5868
mp_obj_list_init(mp_sys_argv, 0);
5969

60-
code = pyexec_frozen_module("bios.py");
61-
if (code != 1) {
62-
// error or SystemExit
63-
return 1;
64-
} else {
65-
// done, give interpreter for now
66-
for (;;) {
67-
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
68-
if (pyexec_raw_repl() != 0) {
69-
break;
70-
}
71-
} else {
72-
if (pyexec_friendly_repl() != 0) {
73-
break;
74-
}
70+
do_frozen("bios.py");
71+
72+
for (;;) {
73+
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
74+
if (pyexec_raw_repl() != 0) {
75+
break;
76+
}
77+
} else {
78+
if (pyexec_friendly_repl() != 0) {
79+
break;
7580
}
7681
}
7782
}
7883

7984
nlr_pop();
8085
} else {
81-
mp_obj_print_exception(&debug_print, (mp_obj_t) nlr.ret_val);
86+
nlr_buf_t nlr2;
87+
if (nlr_push(&nlr2) == 0) {
88+
vstr_t vstr;
89+
mp_print_t print;
90+
vstr_init_print(&vstr, 256, &print);
91+
92+
mp_obj_print_exception(&print, (mp_obj_t) nlr.ret_val);
93+
char *message = vstr_null_terminated_str(&vstr);
94+
__fatal_error(message);
95+
nlr_pop();
96+
} else {
97+
vstr_t vstr;
98+
mp_print_t print;
99+
vstr_init_print(&vstr, 256, &print);
100+
101+
mp_obj_print_helper(&print, (mp_obj_t) nlr.ret_val, PRINT_EXC);
102+
char *message = vstr_null_terminated_str(&vstr);
103+
__fatal_error(message);
104+
}
105+
82106
mp_deinit();
83107
}
84108

src/modules/bios.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def main():
5151
5252
content = load(address)
5353
context = {'__name__': '__main__', '__path__': address}
54-
func = compile(content, "<init>", "exec")
55-
exec(content, context)
54+
func = compile(content, init, "exec")
55+
exec(func, context)
5656
5757
5858
if __name__ == '__main__':
@@ -62,10 +62,8 @@ def main():
6262
data = invoke(eeprom, 'get')
6363
context = {'__name__': '__main__', '__path__': eeprom}
6464

65-
try:
66-
exec(data, context)
67-
except BaseException as e:
68-
print(type(e), e)
65+
func = compile(data, "<EEPROM>", "exec")
66+
exec(func, context)
6967

7068

7169
bios()

0 commit comments

Comments
 (0)