8000 py: Add traceback info to syntax errors. · lurch/micropython@4b01de4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b01de4

Browse files
committed
py: Add traceback info to syntax errors.
Should fix issue micropython#463.
1 parent 3d484d9 commit 4b01de4

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

py/builtinevex.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse
2525
// parse the string
2626
mp_parse_error_kind_t parse_error_kind;
2727
mp_parse_node_t pn = mp_parse(lex, parse_input_kind, &parse_error_kind);
28-
mp_lexer_free(lex);
2928

3029
if (pn == MP_PARSE_NODE_NULL) {
3130
// parse error; raise exception
32-
nlr_raise(mp_parse_make_exception(parse_error_kind));
31+
mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind);
32+
mp_lexer_free(lex);
33+
nlr_raise(exc);
3334
}
3435

36+
mp_lexer_free(lex);
37+
3538
// compile the string
3639
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
3740
mp_parse_node_free(pn);

py/builtinimport.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,18 @@ void do_load(mp_obj_t module_obj, vstr_t *file) {
100100
// parse the imported script
101101
mp_parse_error_kind_t parse_error_kind;
102102
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);
103-
mp_lexer_free(lex);
104103

105104
if (pn == MP_PARSE_NODE_NULL) {
106105
// parse error; clean up and raise exception
106+
mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind);
107+
mp_lexer_free(lex);
107108
mp_locals_set(old_locals);
108109
mp_globals_set(old_globals);
109-
nlr_raise(mp_parse_make_exception(parse_error_kind));
110+
nlr_raise(exc);
110111
}
111112

113+
mp_lexer_free(lex);
114+
112115
// compile the imported script
113116
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
114117
mp_parse_node_free(pn);

py/parsehelper.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,30 @@ void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_
3838
}
3939
}
4040

41-
mp_obj_t mp_parse_make_exception(mp_parse_error_kind_t parse_error_kind) {
42-
// TODO add source file and line number to exception?
41+
mp_obj_t mp_parse_make_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
42+
// make exception object
43+
mp_obj_t exc;
4344
switch (parse_error_kind) {
4445
case MP_PARSE_ERROR_MEMORY:
45-
return mp_obj_new_exception_msg(&mp_type_MemoryError, STR_MEMORY);
46+
exc = mp_obj_new_exception_msg(&mp_type_MemoryError, STR_MEMORY);
47+
break;
4648

4749
case MP_PARSE_ERROR_UNEXPECTED_INDENT:
48-
return mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT);
50+
exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT);
51+
break;
4952

5053
case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
51-
return mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT);
54+
exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT);
55+
break;
5256

5357
case MP_PARSE_ERROR_INVALID_SYNTAX:
5458
default:
55-
return mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX);
59+
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX);
60+
break;
5661
}
62+
63+
// add traceback to give info about file name and location
64+
mp_obj_exception_add_traceback(exc, mp_lexer_source_name(lex), mp_lexer_cur(lex)->src_line, mp_lexer_cur(lex)->src_column);
65+
66+
return exc;
5767
}

py/parsehelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind);
2-
mp_obj_t mp_parse_make_exception(mp_parse_error_kind_t parse_error_kind);
2+
mp_obj_t mp_parse_make_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind);

0 commit comments

Comments
 (0)
0