-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpy-traceback.patch
53 lines (47 loc) · 2.14 KB
/
mpy-traceback.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
From e7a799e31c0c01bfe9a2972fa456f80d4dc987b5 Mon Sep 17 00:00:00 2001
From: "David (Pololu)" <[email protected]>
Date: Tue, 11 Apr 2023 16:40:05 -0700
Subject: [PATCH] py/modsys.c: Add sys._exc_traceback.
This makes it easier to provide custom formatting of exception
stack traces, e.g. to make them fit on a tiny display.
This is an experimental function that exposes internal details
of MicroPython and it might change in the future.
Signed-off-by: David (Pololu) <[email protected]>
---
py/modsys.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/py/modsys.c b/py/modsys.c
index 35af761a08f..ab03aff95b7 100644
--- a/py/modsys.c
+++ b/py/modsys.c
@@ -150,6 +150,24 @@ STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
+STATIC mp_obj_t mp_sys_exc_traceback(mp_obj_t exc) {
+ if (!mp_obj_is_exception_instance(exc)) {
+ mp_raise_TypeError(MP_ERROR_TEXT("not an exception"));
+ }
+ size_t n, *values;
+ mp_obj_exception_get_traceback(exc, &n, &values);
+ // Assumption: n is a multiple of 3.
+ mp_obj_t obj = mp_obj_new_list(n, NULL);
+ mp_obj_list_t *list = MP_OBJ_TO_PTR(obj);
+ for (size_t i = 0; i < list->len; i += 3) {
+ list->items[i + 0] = MP_OBJ_NEW_QSTR(values[i + 0]); // filename
+ list->items[i + 1] = MP_OBJ_NEW_SMALL_INT(values[i + 1]); // line
+ list->items[i + 2] = MP_OBJ_NEW_QSTR(values[i + 2]); // block
+ }
+ return obj;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_exc_traceback_obj, mp_sys_exc_traceback);
+
#if MICROPY_PY_SYS_EXC_INFO
STATIC mp_obj_t mp_sys_exc_info(void) {
mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
@@ -277,6 +295,7 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
*/
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
+ { MP_ROM_QSTR(MP_QSTR__exc_traceback), MP_ROM_PTR(&mp_sys_exc_traceback_obj) },
#if MICROPY_PY_SYS_ATEXIT
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
#endif