From 46c3ab20049af16e97863e90e5761fae329f872a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 6 Dec 2014 14:29:09 +0200 Subject: modsys: Add sys.print_exception(exc, file=sys.stdout) function. The function is modeled after traceback.print_exception(), but unbloated, and put into existing module to save overhead on adding another module. Compliant traceback.print_exception() is intended to be implemented in micropython-lib in terms of sys.print_exception(). This change required refactoring mp_obj_print_exception() to take pfenv_t interface arguments. Addresses #751. --- py/obj.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'py/obj.c') diff --git a/py/obj.c b/py/obj.c index c869a6546..d2801a396 100644 --- a/py/obj.c +++ b/py/obj.c @@ -85,31 +85,31 @@ 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(mp_obj_t exc) { +void mp_obj_print_exception(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t exc) { if (mp_obj_is_exception_instance(exc)) { mp_uint_t n, *values; mp_obj_exception_get_traceback(exc, &n, &values); if (n > 0) { assert(n % 3 == 0); - printf("Traceback (most recent call last):\n"); + print(env, "Traceback (most recent call last):\n"); for (int i = n - 3; i >= 0; i -= 3) { #if MICROPY_ENABLE_SOURCE_LINE - printf(" File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]); + print(env, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]); #else - printf(" File \"%s\"", qstr_str(values[i])); + print(env, " File \"%s\"", qstr_str(values[i])); #endif // the block name can be NULL if it's unknown qstr block = values[i + 2]; if (block == MP_QSTR_NULL) { - printf("\n"); + print(env, "\n"); } else { - printf(", in %s\n", qstr_str(block)); + print(env, ", in %s\n", qstr_str(block)); } } } } - mp_obj_print(exc, PRINT_EXC); - printf("\n"); + mp_obj_print_helper(print, env, exc, PRINT_EXC); + print(env, "\n"); } bool mp_obj_is_true(mp_obj_t arg) { -- cgit v1.2.3