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.
diff --git a/py/compile.c b/py/compile.c
index 141cd2d..12134d4 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -108,16 +108,20 @@
#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 @@
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
}
}