py: For viper compile errors, add traceback with function and filename.

ViperTypeError now includes filename and function name where the error
occurred.  The line number is the line number of the start of the
function definition, which is the best that can be done without a lot
more work.

Partially addresses issue #1381.
This commit is contained in:
Damien George 2015-07-27 22:20:00 +01:00
parent d8a7f8bff2
commit 84d59c2873
2 changed files with 18 additions and 7 deletions

View File

@ -108,16 +108,20 @@ typedef struct _compiler_t {
#endif
} compiler_t;
STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
// we don't have a 'block' name, so just pass the NULL qstr to indicate this
STATIC void compile_error_add_traceback(compiler_t *comp, mp_parse_node_t pn) {
mp_uint_t line;
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, comp->scope_cur->simple_name);
line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
} else {
// we don't have a line number, so just pass 0
mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
line = 0;
}
comp->compile_error = exc;
mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, line, comp->scope_cur->simple_name);
}
STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
compile_error_add_traceback(comp, pn);
}
#if MICROPY_COMP_MODULE_CONST
@ -3818,6 +3822,13 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
if (comp->compile_error == MP_OBJ_NULL) {
compile_scope(comp, s, MP_PASS_EMIT);
}
#if MICROPY_EMIT_NATIVE
// if viper had an error then add traceback
if (comp->compile_error != MP_OBJ_NULL && s->emit_options == MP_EMIT_OPT_VIPER) {
compile_error_add_traceback(comp, s->pn);
}
#endif
}
}

View File

@ -677,7 +677,7 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
// right now we have a restriction of maximum of 4 arguments
if (scope->num_pos_args >= 5) {
EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments (while compiling '%q')", scope->simple_name); return;
EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments");
return;
}