Commit 136b149e authored by Damien George's avatar Damien George
Browse files

py: Add full traceback to exception printing.

parent 49f6a99c
......@@ -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);
}
// 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) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
return false;
......
......@@ -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(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);
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
extern const mp_obj_type_t exception_type;
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_get_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_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
// str
extern const mp_obj_type_t str_type;
......
......@@ -17,9 +17,7 @@
// have args tuple (or otherwise have it as NULL).
typedef struct mp_obj_exception_t {
mp_obj_base_t base;
qstr source_file;
machine_uint_t source_line;
qstr source_block;
mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
qstr id;
qstr msg;
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
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, 0);
o->base.type = &exception_type;
o->source_file = 0;
o->source_line = 0;
o->traceback = MP_OBJ_NULL;
o->id = id;
o->args.len = 0;
if (fmt == NULL) {
......@@ -115,26 +112,27 @@ qstr mp_obj_exception_get_type(mp_obj_t self_in) {
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));
mp_obj_exception_t *self = self_in;
// TODO make a list of file/line pairs for the traceback
// for now, just keep the first one
if (file != 0 && self->source_file == 0) {
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;
// for traceback, we are just using the list object for convenience, it's not really a list of Python objects
if (self->traceback == MP_OBJ_NULL) {
self->traceback = mp_obj_new_list(0, NULL);
}
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));
mp_obj_exception_t *self = self_in;
*file = self->source_file;
*line = self->source_line;
*block = self->source_block;
if (self->traceback == MP_OBJ_NULL) {
*n = 0;
*values = NULL;
} else {
uint n2;
mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
*n = n2;
}
}
......@@ -544,7 +544,7 @@ bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_ob
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) {
......
......@@ -445,8 +445,7 @@ void do_repl(void) {
}
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
printf("\n");
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
}
}
}
......@@ -490,8 +489,7 @@ bool do_file(const char *filename) {
return true;
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
printf("\n");
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
return false;
}
}
......
......@@ -73,15 +73,7 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
nlr_pop();
} else {
// uncaught exception
mp_obj_t exc = (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");
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment