py: Add full traceback to exception printing.

This commit is contained in:
Damien George 2014-01-19 12:38:49 +00:00
parent 49f6a99c65
commit 136b149e41
6 changed files with 41 additions and 36 deletions

View File

@ -58,6 +58,22 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_print_helper(printf_wrapper, NULL, o_in, kind); mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
} }
// helper function to print an exception with traceback
void mp_obj_print_exception(mp_obj_t exc) {
if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
machine_uint_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values);
if (n > 0) {
printf("Traceback (most recent call last):\n");
for (int i = n - 3; i >= 0; i -= 3) {
printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2]));
}
}
}
mp_obj_print(exc, PRINT_REPR);
printf("\n");
}
bool mp_obj_is_callable(mp_obj_t o_in) { bool mp_obj_is_callable(mp_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) { if (MP_OBJ_IS_SMALL_INT(o_in)) {
return false; return false;

View File

@ -236,6 +236,7 @@ const char *mp_obj_get_type_str(mp_obj_t o_in);
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind); void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, 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(mp_obj_t o, mp_print_kind_t kind);
void mp_obj_print_exception(mp_obj_t exc);
bool mp_obj_is_callable(mp_obj_t o_in); bool mp_obj_is_callable(mp_obj_t o_in);
machine_int_t mp_obj_hash(mp_obj_t o_in); machine_int_t mp_obj_hash(mp_obj_t o_in);
@ -273,8 +274,8 @@ machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
// exception // exception
extern const mp_obj_type_t exception_type; extern const mp_obj_type_t exception_type;
qstr mp_obj_exception_get_type(mp_obj_t self_in); qstr mp_obj_exception_get_type(mp_obj_t self_in);
void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block); void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
void mp_obj_exception_get_source_info(mp_obj_t self_in, qstr *file, machine_uint_t *line, qstr *block); void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
// str // str
extern const mp_obj_type_t str_type; extern const mp_obj_type_t str_type;

View File

@ -17,9 +17,7 @@
// have args tuple (or otherwise have it as NULL). // have args tuple (or otherwise have it as NULL).
typedef struct mp_obj_exception_t { typedef struct mp_obj_exception_t {
mp_obj_base_t base; mp_obj_base_t base;
qstr source_file; mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
machine_uint_t source_line;
qstr source_block;
qstr id; qstr id;
qstr msg; qstr msg;
mp_obj_tuple_t args; mp_obj_tuple_t args;
@ -90,8 +88,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(qstr id, const char *fmt, ...) {
// make exception object // make exception object
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, 0); mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, 0);
o->base.type = &exception_type; o->base.type = &exception_type;
o->source_file = 0; o->traceback = MP_OBJ_NULL;
o->source_line = 0;
o->id = id; o->id = id;
o->args.len = 0; o->args.len = 0;
if (fmt == NULL) { if (fmt == NULL) {
@ -115,26 +112,27 @@ qstr mp_obj_exception_get_type(mp_obj_t self_in) {
return self->id; return self->id;
} }
void mp_obj_exception_set_source_info(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
assert(MP_OBJ_IS_TYPE(self_in, &exception_type)); assert(MP_OBJ_IS_TYPE(self_in, &exception_type));
mp_obj_exception_t *self = self_in; mp_obj_exception_t *self = self_in;
// TODO make a list of file/line pairs for the traceback // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
// for now, just keep the first one if (self->traceback == MP_OBJ_NULL) {
if (file != 0 && self->source_file == 0) { self->traceback = mp_obj_new_list(0, NULL);
self->source_file = file;
}
if (line != 0 && self->source_line == 0) {
self->source_line = line;
}
if (block != 0 && self->source_block == 0) {
self->source_block = block;
} }
mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
} }
void mp_obj_exception_get_source_info(mp_obj_t self_in, qstr *file, machine_uint_t *line, qstr *block) { void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) {
assert(MP_OBJ_IS_TYPE(self_in, &exception_type)); assert(MP_OBJ_IS_TYPE(self_in, &exception_type));
mp_obj_exception_t *self = self_in; mp_obj_exception_t *self = self_in;
*file = self->source_file; if (self->traceback == MP_OBJ_NULL) {
*line = self->source_line; *n = 0;
*block = self->source_block; *values = NULL;
} else {
uint n2;
mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
*n = n2;
}
} }

View File

@ -544,7 +544,7 @@ bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_ob
break; break;
} }
} }
mp_obj_exception_set_source_info(nlr.ret_val, source_file, source_line, block_name); mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
} }
while (currently_in_except_block) { while (currently_in_except_block) {

View File

@ -445,8 +445,7 @@ void do_repl(void) {
} }
} else { } else {
// uncaught exception // uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR); mp_obj_print_exception((mp_obj_t)nlr.ret_val);
printf("\n");
} }
} }
} }
@ -490,8 +489,7 @@ bool do_file(const char *filename) {
return true; return true;
} else { } else {
// uncaught exception // uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR); mp_obj_print_exception((mp_obj_t)nlr.ret_val);
printf("\n");
return false; return false;
} }
} }

View File

@ -73,15 +73,7 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
nlr_pop(); nlr_pop();
} else { } else {
// uncaught exception // uncaught exception
mp_obj_t exc = (mp_obj_t)nlr.ret_val; mp_obj_print_exception((mp_obj_t)nlr.ret_val);
if (MP_OBJ_IS_TYPE(exc, &exception_type)) {
qstr file, block;
machine_uint_t line;
mp_obj_exception_get_source_info(exc, &file, &line, &block);
printf("File \"%s\", line %d, in %s\n", qstr_str(file), (int)line, qstr_str(block));
}
mp_obj_print(exc, PRINT_REPR);
printf("\n");
} }
} }