traceback: Implement format_exception

This commit is contained in:
Jeff Epler 2021-08-08 11:03:03 -05:00 committed by Jeff Epler
parent c1ffab7476
commit 9a932a5a48
2 changed files with 72 additions and 28 deletions

View File

@ -3453,10 +3453,6 @@ msgstr ""
msgid "lhs and rhs should be compatible"
msgstr ""
#: shared-bindings/traceback/__init__.c
msgid "limit should be an int"
msgstr ""
#: py/emitnative.c
msgid "local '%q' has type '%q' but source is '%q'"
msgstr ""

View File

@ -38,6 +38,75 @@
//| ...
//|
STATIC void traceback_exception_common(mp_print_t *print, mp_obj_t value, mp_obj_t tb_obj, mp_obj_t limit_obj) {
if (!mp_obj_is_exception_instance(value)) {
mp_raise_TypeError(translate("invalid exception"));
}
mp_obj_exception_t exc = *(mp_obj_exception_t *)MP_OBJ_TO_PTR(value);
mp_int_t limit = 0;
bool print_tb = true;
if (limit_obj != mp_const_none) {
limit = mp_obj_get_int(limit_obj);
print_tb = (limit != 0);
}
if (tb_obj != mp_const_none && print_tb) {
if (!mp_obj_is_type(tb_obj, &mp_type_traceback)) {
mp_raise_TypeError(translate("invalid traceback"));
}
exc.traceback = MP_OBJ_TO_PTR(tb_obj);
} else {
exc.traceback = NULL;
}
shared_module_traceback_print_exception(&exc, print, limit);
}
//| def format_exception(etype: Type[BaseException], value: BaseException, tb: TracebackType,
//| limit: Optional[int] = None, chain: Optional[bool] = True) -> None:
//| """Format a stack trace and the exception information.
//|
//| The arguments have the same meaning as the corresponding arguments
//| to print_exception(). The return value is a list of strings, each
//| ending in a newline and some containing internal newlines. When
//| these lines are concatenated and printed, exactly the same text is
//| printed as does print_exception().
//|
//| .. note: Setting `chain` will have no effect as chained exceptions are not yet implemented.
//|
//| :param Type[BaseException] etype: This is ignored and inferred from the type of ``value``.
//| :param BaseException value: The exception. Must be an instance of `BaseException`.
//| :param TracebackType tb: The traceback object. If `None`, the traceback will not be printed.
//| :param int limit: Print up to limit stack trace entries (starting from the callers frame) if limit is positive.
//| Otherwise, print the last ``abs(limit)`` entries. If limit is omitted or None, all entries are printed.
//| :param bool chain: If `True` then chained exceptions will be printed (note: not yet implemented).
//|
//| """
//| ...
//|
STATIC mp_obj_t traceback_format_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_chain };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_etype, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_value, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_tb, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_limit, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_chain, MP_ARG_BOOL, {.u_bool = true} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_print_t print;
vstr_t vstr;
vstr_init_print(&vstr, 0, &print);
traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj);
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(traceback_format_exception_obj, 3, traceback_format_exception);
//| def print_exception(etype: Type[BaseException], value: BaseException, tb: TracebackType,
//| limit: Optional[int] = None, file: Optional[io.FileIO] = None, chain: Optional[bool] = True) -> None:
//|
@ -57,6 +126,7 @@
//| """
//| ...
//|
STATIC mp_obj_t traceback_print_exception(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_etype, ARG_value, ARG_tb, ARG_limit, ARG_file, ARG_chain };
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
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (!mp_obj_is_exception_instance(args[ARG_value].u_obj)) {
mp_raise_TypeError(translate("invalid exception"));
}
mp_obj_exception_t exc = *(mp_obj_exception_t *)MP_OBJ_TO_PTR(args[ARG_value].u_obj);
mp_print_t print = mp_plat_print;
if (args[ARG_file].u_obj != mp_const_none) {
#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
#endif
}
mp_int_t limit = 0;
bool print_tb = true;
if (args[ARG_limit].u_obj != mp_const_none) {
if (!mp_obj_get_int_maybe(args[ARG_limit].u_obj, &limit)) {
mp_raise_TypeError(translate("limit should be an int"));
}
print_tb = (limit != 0);
}
if (args[ARG_tb].u_obj != mp_const_none && print_tb) {
if (!mp_obj_is_type(args[ARG_tb].u_obj, &mp_type_traceback)) {
mp_raise_TypeError(translate("invalid traceback"));
}
exc.traceback = MP_OBJ_TO_PTR(args[ARG_tb].u_obj);
} else {
exc.traceback = NULL;
}
shared_module_traceback_print_exception(&exc, &print, limit);
traceback_exception_common(&print, args[ARG_value].u_obj, args[ARG_tb].u_obj, args[ARG_limit].u_obj);
return mp_const_none;
}
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[] = {
// module name
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_traceback) },
// module functions
{ MP_ROM_QSTR(MP_QSTR_format_exception), MP_ROM_PTR(&traceback_format_exception_obj) },
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&traceback_print_exception_obj) },
};
STATIC MP_DEFINE_CONST_DICT(traceback_module_globals, traceback_module_globals_table);