Merge pull request #5110 from jepler/format-exception

Implement `traceback.format_exception`
This commit is contained in:
microDev 2021-08-10 00:47:18 +05:30 committed by GitHub
commit 3495cc1ff6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 167 additions and 145 deletions

View File

@ -35,11 +35,11 @@ msgid ""
"https://github.com/adafruit/circuitpython/issues\n"
msgstr ""
#: py/obj.c shared-bindings/traceback/__init__.c
#: py/obj.c
msgid " File \"%q\""
msgstr ""
#: py/obj.c shared-bindings/traceback/__init__.c
#: py/obj.c
msgid " File \"%q\", line %d"
msgstr ""
@ -326,7 +326,7 @@ msgstr ""
msgid "*x must be assignment target"
msgstr ""
#: py/obj.c shared-bindings/traceback/__init__.c
#: py/obj.c
msgid ", in %q\n"
msgstr ""
@ -2215,7 +2215,7 @@ msgstr ""
msgid "Touch alarms not available"
msgstr ""
#: py/obj.c shared-bindings/traceback/__init__.c
#: py/obj.c
msgid "Traceback (most recent call last):\n"
msgstr ""
@ -3457,10 +3457,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 ""
@ -3907,6 +3903,10 @@ msgstr ""
#: ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.h
#: ports/esp32s2/boards/franzininho_wifi_wroom/mpconfigboard.h
#: ports/esp32s2/boards/franzininho_wifi_wrover/mpconfigboard.h
#: ports/esp32s2/boards/gravitech_cucumber_m/mpconfigboard.h
#: ports/esp32s2/boards/gravitech_cucumber_ms/mpconfigboard.h
#: ports/esp32s2/boards/gravitech_cucumber_r/mpconfigboard.h
#: ports/esp32s2/boards/gravitech_cucumber_rs/mpconfigboard.h
#: ports/esp32s2/boards/lilygo_ttgo_t8_s2_st7789/mpconfigboard.h
#: ports/esp32s2/boards/microdev_micro_s2/mpconfigboard.h
#: ports/esp32s2/boards/muselab_nanoesp32_s2_wroom/mpconfigboard.h

View File

@ -131,10 +131,7 @@ STATIC void mp_help_print_modules(void) {
#if MICROPY_ENABLE_EXTERNAL_IMPORT
// let the user know there may be other modules available from the filesystem
const compressed_string_t *compressed = translate("Plus any modules on the filesystem\n");
char decompressed[decompress_length(compressed)];
decompress(compressed, decompressed);
mp_print_str(MP_PYTHON_PRINTER, decompressed);
mp_printf(MP_PYTHON_PRINTER, "%S", translate("Plus any modules on the filesystem\n"));
#endif
}
#endif
@ -150,18 +147,10 @@ STATIC void mp_help_print_obj(const mp_obj_t obj) {
const mp_obj_type_t *type = mp_obj_get_type(obj);
// try to print something sensible about the given object
const compressed_string_t *compressed = translate("object ");
char decompressed_object[decompress_length(compressed)];
decompress(compressed, decompressed_object);
mp_print_str(MP_PYTHON_PRINTER, decompressed_object);
mp_cprintf(MP_PYTHON_PRINTER, translate("object "));
mp_obj_print(obj, PRINT_STR);
compressed = translate(" is of type %q\n");
char decompressed_typestring[decompress_length(compressed)];
decompress(compressed, decompressed_typestring);
mp_printf(MP_PYTHON_PRINTER, decompressed_typestring, type->name);
mp_cprintf(MP_PYTHON_PRINTER, translate(" is of type %q\n"), type->name);
mp_map_t *map = NULL;
if (type == &mp_type_module) {
@ -186,11 +175,9 @@ STATIC void mp_help_print_obj(const mp_obj_t obj) {
STATIC mp_obj_t mp_builtin_help(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
// print a general help message. Translate only works on single strings on one line.
const compressed_string_t *compressed =
translate("Welcome to Adafruit CircuitPython %s!\n\nPlease visit learn.adafruit.com/category/circuitpython for project guides.\n\nTo list built-in modules please do `help(\"modules\")`.\n");
char decompressed[decompress_length(compressed)];
decompress(compressed, decompressed);
mp_printf(MP_PYTHON_PRINTER, decompressed, MICROPY_GIT_TAG);
mp_cprintf(MP_PYTHON_PRINTER,
translate("Welcome to Adafruit CircuitPython %s!\n\nPlease visit learn.adafruit.com/category/circuitpython for project guides.\n\nTo list built-in modules please do `help(\"modules\")`.\n"),
MICROPY_GIT_TAG);
} else {
// try to print something sensible about the given object
mp_help_print_obj(args[0]);

View File

@ -376,6 +376,13 @@ int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, c
}
#endif
static int print_str_common(const mp_print_t *print, const char *str, int prec, size_t len, int flags, int fill, int width) {
if (prec >= 0 && (size_t)prec < len) {
len = prec;
}
return mp_print_strn(print, str, len, flags, fill, width);
}
int mp_printf(const mp_print_t *print, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
@ -484,19 +491,24 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
qstr qst = va_arg(args, qstr);
size_t len;
const char *str = (const char *)qstr_data(qst, &len);
if (prec >= 0 && (size_t)prec < len) {
len = prec;
}
chrs += mp_print_strn(print, str, len, flags, fill, width);
chrs += print_str_common(print, str, prec, len, flags, fill, width);
break;
}
case 'S': {
compressed_string_t *arg = va_arg(args, compressed_string_t *);
size_t len_with_nul = decompress_length(arg);
size_t len = len_with_nul - 1;
char str[len_with_nul];
decompress(arg, str);
chrs += print_str_common(print, str, prec, len, flags, fill, width);
break;
}
case 's': {
const char *str = va_arg(args, const char *);
#ifndef NDEBUG
// With debugging enabled, catch printing of null string pointers
if (prec != 0 && str == NULL) {
chrs += mp_print_strn(print, "(null)", 6, flags, fill, width);
break;
if (str == NULL) {
str = "(null)";
}
#endif
size_t len = strlen(str);
@ -574,3 +586,19 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
}
return chrs;
}
int mp_cprintf(const mp_print_t *print, const compressed_string_t *compressed_fmt, ...) {
va_list ap;
va_start(ap, compressed_fmt);
int ret = mp_vcprintf(print, compressed_fmt, ap);
va_end(ap);
return ret;
}
int mp_vcprintf(const mp_print_t *print, const compressed_string_t *compressed_fmt, va_list args) {
char fmt[decompress_length(compressed_fmt)];
// TODO: Optimise this to format-while-decompressing (and not require the temp stack space).
decompress(compressed_fmt, fmt);
return mp_vprintf(print, fmt, args);
}

View File

@ -71,4 +71,10 @@ int mp_printf(const mp_print_t *print, const char *fmt, ...);
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
#endif
struct compressed_string;
int mp_cprintf(const mp_print_t *print, const struct compressed_string *compressed_fmt, ...);
#ifdef va_start
int mp_vcprintf(const mp_print_t *print, const struct compressed_string *compressed_fmt, va_list args);
#endif
#endif // MICROPY_INCLUDED_PY_MPPRINT_H

View File

@ -143,41 +143,55 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
}
// helper function to print an exception with traceback
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
void mp_obj_print_exception_with_limit(const mp_print_t *print, mp_obj_t exc, mp_int_t limit) {
if (mp_obj_is_exception_instance(exc) && stack_ok()) {
size_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values);
if (n > 0) {
assert(n % 3 == 0);
// Decompress the format strings
const compressed_string_t *traceback = MP_ERROR_TEXT("Traceback (most recent call last):\n");
char decompressed[decompress_length(traceback)];
decompress(traceback, decompressed);
#if MICROPY_ENABLE_SOURCE_LINE
const compressed_string_t *frame = MP_ERROR_TEXT(" File \"%q\", line %d");
#else
const compressed_string_t *frame = MP_ERROR_TEXT(" File \"%q\"");
#endif
char decompressed_frame[decompress_length(frame)];
decompress(frame, decompressed_frame);
const compressed_string_t *block_fmt = MP_ERROR_TEXT(", in %q\n");
char decompressed_block[decompress_length(block_fmt)];
decompress(block_fmt, decompressed_block);
// Set traceback formatting
// Default: Print full traceback
limit = limit * 3;
mp_int_t i = n - 3, j;
if (limit > 0) {
// Print upto limit traceback
// entries from caller's frame
if ((unsigned)limit > n) {
limit = n;
}
limit = n - limit;
} else if (limit < 0) {
// Print upto limit traceback
// entries from last
if ((unsigned)-limit > n) {
limit = -n;
}
i = 0, limit = limit + 3;
}
// Print the traceback
mp_print_str(print, decompressed);
for (int i = n - 3; i >= 0; i -= 3) {
mp_cprintf(print, MP_ERROR_TEXT("Traceback (most recent call last):\n"));
for (; i >= limit; i -= 3) {
j = (i < 0) ? -i : i;
#if MICROPY_ENABLE_SOURCE_LINE
mp_printf(print, decompressed_frame, values[i], (int)values[i + 1]);
mp_cprintf(print, frame, values[j], (int)values[j + 1]);
#else
mp_printf(print, decompressed_frame, values[i]);
mp_cprintf(print, frame, values[j]);
#endif
// the block name can be NULL if it's unknown
qstr block = values[i + 2];
// The block name can be NULL if it's unknown
qstr block = values[j + 2];
if (block == MP_QSTRnull) {
mp_print_str(print, "\n");
} else {
mp_printf(print, decompressed_block, block);
mp_cprintf(print, block_fmt, block);
}
}
}
@ -186,6 +200,10 @@ void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
mp_print_str(print, "\n");
}
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
mp_obj_print_exception_with_limit(print, exc, 0);
}
bool PLACE_IN_ITCM(mp_obj_is_true)(mp_obj_t arg) {
if (arg == mp_const_false) {
return 0;

View File

@ -888,6 +888,7 @@ mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
void mp_obj_print_exception_with_limit(const mp_print_t *print, mp_obj_t exc, mp_int_t limit);
bool mp_obj_is_true(mp_obj_t arg);
bool mp_obj_is_callable(mp_obj_t o_in);

View File

@ -465,12 +465,9 @@ mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, const com
o_str->data = NULL;
} else {
// We have some memory to format the string.
// TODO: Optimise this to format-while-decompressing (and not require the temp stack space).
struct _exc_printer_t exc_pr = {!used_emg_buf, o_str_alloc, 0, o_str_buf};
mp_print_t print = {&exc_pr, exc_add_strn};
char fmt_decompressed[decompress_length(fmt)];
decompress(fmt, fmt_decompressed);
mp_vprintf(&print, fmt_decompressed, ap);
mp_vcprintf(&print, fmt, ap);
exc_pr.buf[exc_pr.len] = '\0';
o_str->len = exc_pr.len;
o_str->data = exc_pr.buf;

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 = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
}
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);

View File

@ -27,67 +27,5 @@
#include "shared-module/traceback/__init__.h"
void shared_module_traceback_print_exception(mp_obj_exception_t *exc, mp_print_t *print, mp_int_t limit) {
// Print traceback
if (exc->traceback != NULL) {
size_t n = exc->traceback->len;
size_t *values = exc->traceback->data;
if (n > 0) {
assert(n % 3 == 0);
// Decompress the format strings
const compressed_string_t *traceback = MP_ERROR_TEXT("Traceback (most recent call last):\n");
char decompressed[decompress_length(traceback)];
decompress(traceback, decompressed);
#if MICROPY_ENABLE_SOURCE_LINE
const compressed_string_t *frame = MP_ERROR_TEXT(" File \"%q\", line %d");
#else
const compressed_string_t *frame = MP_ERROR_TEXT(" File \"%q\"");
#endif
char decompressed_frame[decompress_length(frame)];
decompress(frame, decompressed_frame);
const compressed_string_t *block_fmt = MP_ERROR_TEXT(", in %q\n");
char decompressed_block[decompress_length(block_fmt)];
decompress(block_fmt, decompressed_block);
// Set traceback formatting
// Default: Print full traceback
limit = limit * 3;
mp_int_t i = n - 3, j;
if (limit > 0) {
// Print upto limit traceback
// entries from caller's frame
if ((unsigned)limit > n) {
limit = n;
}
limit = n - limit;
} else if (limit < 0) {
// Print upto limit traceback
// entries from last
if ((unsigned)-limit > n) {
limit = -n;
}
i = 0, limit = limit + 3;
}
// Print the traceback
mp_print_str(print, decompressed);
for (; i >= limit; i -= 3) {
j = (i < 0) ? -i : i;
#if MICROPY_ENABLE_SOURCE_LINE
mp_printf(print, decompressed_frame, values[j], (int)values[j + 1]);
#else
mp_printf(print, decompressed_frame, values[j]);
#endif
// The block name can be NULL if it's unknown
qstr block = values[j + 2];
if (block == MP_QSTRnull) {
mp_print_str(print, "\n");
} else {
mp_printf(print, decompressed_block, block);
}
}
}
}
// Print exception
mp_obj_print_helper(print, exc, PRINT_EXC);
mp_print_str(print, "\n");
mp_obj_print_exception_with_limit(print, exc, limit);
}

View File

@ -35,12 +35,11 @@
#endif
#include "py/misc.h"
#include "py/mpprint.h"
#include "supervisor/serial.h"
void serial_write_compressed(const compressed_string_t *compressed) {
char decompressed[decompress_length(compressed)];
decompress(compressed, decompressed);
serial_write(decompressed);
mp_printf(MP_PYTHON_PRINTER, "%S", compressed);
}
STATIC void get_word(int n, const mchar_t **pos, const mchar_t **end) {

View File

@ -69,7 +69,7 @@
// flexible array}, but is also future-proofed against strings with
// UTF-8 length above 256, with a savings of about 1.375 bytes per
// string.
typedef struct {
typedef struct compressed_string {
uint8_t data;
const uint8_t tail[];
} compressed_string_t;