Skip to content

Commit 84d59c2

Browse files
committed
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 adafruit#1381.
1 parent d8a7f8b commit 84d59c2

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

py/compile.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,20 @@ typedef struct _compiler_t {
108108
#endif
109109
} compiler_t;
110110

111-
STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
112-
mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
113-
// we don't have a 'block' name, so just pass the NULL qstr to indicate this
111+
STATIC void compile_error_add_traceback(compiler_t *comp, mp_parse_node_t pn) {
112+
mp_uint_t line;
114113
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
115-
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);
114+
line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line;
116115
} else {
117116
// we don't have a line number, so just pass 0
118-
mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name);
117+
line = 0;
119118
}
120-
comp->compile_error = exc;
119+
mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, line, comp->scope_cur->simple_name);
120+
}
121+
122+
STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
123+
comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
124+
compile_error_add_traceback(comp, pn);
121125
}
122126

123127
#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
38183822
if (comp->compile_error == MP_OBJ_NULL) {
38193823
compile_scope(comp, s, MP_PASS_EMIT);
38203824
}
3825+
3826+
#if MICROPY_EMIT_NATIVE
3827+
// if viper had an error then add traceback
3828+
if (comp->compile_error != MP_OBJ_NULL && s->emit_options == MP_EMIT_OPT_VIPER) {
3829+
compile_error_add_traceback(comp, s->pn);
3830+
}
3831+
#endif
38213832
}
38223833
}
38233834

py/emitnative.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
677677

678678
// right now we have a restriction of maximum of 4 arguments
679679
if (scope->num_pos_args >= 5) {
680-
EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments (while compiling '%q')", scope->simple_name); return;
680+
EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments");
681681
return;
682682
}
683683

0 commit comments

Comments
 (0)