Skip to content

Commit 9a932a5

Browse files
committed
traceback: Implement format_exception
1 parent c1ffab7 commit 9a932a5

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

locale/circuitpython.pot

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,10 +3453,6 @@ msgstr ""
34533453
msgid "lhs and rhs should be compatible"
34543454
msgstr ""
34553455

3456-
#: shared-bindings/traceback/__init__.c
3457-
msgid "limit should be an int"
3458-
msgstr ""
3459-
34603456
#: py/emitnative.c
34613457
msgid "local '%q' has type '%q' but source is '%q'"
34623458
msgstr ""

shared-bindings/traceback/__init__.c

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,75 @@
3838
//| ...
3939
//|
4040

41+
STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj_t tb_obj, mp_obj_t limit_obj) {
42+
if (!mp_obj_is_exception_instance(value)) {
43+
mp_raise_TypeError(translate("invalid exception"));
44+
}
45+
mp_obj_exception_t exc = *(mp_obj_exception_t *)MP_OBJ_TO_PTR(value);
46+
47+
mp_int_t limit = 0;
48+
bool print_tb = true;
49+
if (limit_obj != mp_const_none) {
50+
limit = mp_obj_get_int(limit_obj);
51+
print_tb = (limit != 0);
52+
}
53+
54+
if (tb_obj != mp_const_none && print_tb) {
55+
if (!mp_obj_is_type(tb_obj, &mp_type_traceback)) {
56+
mp_raise_TypeError(translate("invalid traceback"));
57+
}
58+
exc.traceback = MP_OBJ_TO_PTR(tb_obj);
59+
} else {
60+
exc.traceback = NULL;
61+
}
62+
63+
shared_module_traceback_print_exception(&exc, print, limit);
64+
}
65+
66+
//| def format_exception(etype: Type[BaseException], value: BaseException, tb: TracebackType,
67+
//| limit: Optional[int] = None, chain: Optional[bool] = True) -> None:
68+
//| """Format a stack trace and the exception information.
69+
//|
70+
//| The arguments have the same meaning as the corresponding arguments
71+
//| to print_exception(). The return value is a list of strings, each
72+
//| ending in a newline and some containing internal newlines. When
73+
//| these lines are concatenated and printed, exactly the same text is
74+
//| printed as does print_exception().
75+
//|
76+
//| .. note: Setting `chain` will have no effect as chained exceptions are not yet implemented.
77+
//|
78+
//| :param Type[BaseException] etype: This is ignored and inferred from the type of ``value``.
79+
//| :param BaseException value: The exception. Must be an instance of `BaseException`.
80+
//| :param TracebackType tb: The traceback object. If `None`, the traceback will not be printed.
81+
//| :param int limit: Print up to limit stack trace entries (starting from the caller’s frame) if limit is positive.
82+
//| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed.
83+
//| :param bool chain: If `True` then chained exceptions will be printed (note: not yet implemented).
84+
//|
85+
//| """
86+
//| ...
87+
//|
88+
STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
89+
enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_chain };
90+
static const mp_arg_t allowed_args[] = {
91+
{ MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED },
92+
{ MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED },
93+
{ MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED },
94+
{ MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} },
95+
{ MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} },
96+
};
97+
98+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
99+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
100+
101+
mp_print_t print;
102+
vstr_t vstr;
103+
vstr_init_print(&vstr, 0, &print);
104+
traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj);
105+
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
106+
}
107+
108+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 3, traceback_format_exception);
109+
41110
//| def print_exception(etype: Type[BaseException], value: BaseException, tb: TracebackType,
42111
//| limit: Optional[int] = None, file: Optional[io.FileIO] = None, chain: Optional[bool] = True) -> None:
43112
//|
@@ -57,6 +126,7 @@
57126
//| """
58127
//| ...
59128
//|
129+
60130
STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
61131
enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain };
62132
static const mp_arg_t allowed_args[] = {
@@ -71,11 +141,6 @@ STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_arg
71141
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
72142
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
73143

74-
if (!mp_obj_is_exception_instance(args[ARG_value].u_obj)) {
75-
mp_raise_TypeError(translate("invalid exception"));
76-
}
77-
mp_obj_exception_t exc = *(mp_obj_exception_t *)MP_OBJ_TO_PTR(args[ARG_value].u_obj);
78-
79144
mp_print_t print = mp_plat_print;
80145
if (args[ARG_file].u_obj != mp_const_none) {
81146
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
@@ -87,25 +152,7 @@ STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_arg
87152
#endif
88153
}
89154

90-
mp_int_t limit = 0;
91-
bool print_tb = true;
92-
if (args[ARG_limit].u_obj != mp_const_none) {
93-
if (!mp_obj_get_int_maybe(args[ARG_limit].u_obj, &limit)) {
94-
mp_raise_TypeError(translate("limit should be an int"));
95-
}
96-
print_tb = (limit != 0);
97-
}
98-
99-
if (args[ARG_tb].u_obj != mp_const_none && print_tb) {
100-
if (!mp_obj_is_type(args[ARG_tb].u_obj, &mp_type_traceback)) {
101-
mp_raise_TypeError(translate("invalid traceback"));
102-
}
103-
exc.traceback = MP_OBJ_TO_PTR(args[ARG_tb].u_obj);
104-
} else {
105-
exc.traceback = NULL;
106-
}
107-
108-
shared_module_traceback_print_exception(&exc, &print, limit);
155+
traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj);
109156
return mp_const_none;
110157
}
111158
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_print_exception_obj, 3, traceback_print_exception);
@@ -114,6 +161,7 @@ STATIC const mp_rom_map_elem_t traceback_module_globals_table[] = {
114161
// module name
115162
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_traceback) },
116163
// module functions
164+
{ MP_ROM_QSTR(MP_QSTR_format_exception), MP_ROM_PTR(&traceback_format_exception_obj) },
117165
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&traceback_print_exception_obj) },
118166
};
119167
STATIC MP_DEFINE_CONST_DICT(traceback_module_globals, traceback_module_globals_table);

0 commit comments

Comments
 (0)