Skip to content

Commit 96f0dd3

Browse files
committed
py/parse: Fix handling of empty input so it raises an exception.
1 parent fa7c61d commit 96f0dd3

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

py/grammar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
// eval_input: testlist NEWLINE* ENDMARKER
3939

4040
DEF_RULE(single_input, nc, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound_stmt))
41-
DEF_RULE(file_input, nc, and(1), opt_rule(file_input_2))
41+
DEF_RULE(file_input, c(generic_all_nodes), and(1), opt_rule(file_input_2))
4242
DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3))
4343
DEF_RULE(file_input_3, nc, or(2), tok(NEWLINE), rule(stmt))
4444
DEF_RULE(eval_input, nc, and(2), rule(testlist), opt_rule(eval_input_2))

py/parse.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,11 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
720720
goto syntax_error;
721721
}
722722

723+
// check that parsing resulted in a parse node (can fail on empty input)
724+
if (parser.result_stack_top == 0) {
725+
goto syntax_error;
726+
}
727+
723728
//result_stack_show(parser);
724729
//printf("rule stack alloc: %d\n", parser.rule_stack_alloc);
725730
//printf("result stack alloc: %d\n", parser.result_stack_alloc);

tests/basics/parser.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# parser tests
2+
3+
# completely empty string
4+
# uPy and CPy differ for this case
5+
#try:
6+
# compile("", "stdin", "single")
7+
#except SyntaxError:
8+
# print("SyntaxError")
9+
try:
10+
compile("", "stdin", "eval")
11+
except SyntaxError:
12+
print("SyntaxError")
13+
compile("", "stdin", "exec")
14+
15+
# empty continued line
16+
try:
17+
compile("\\\n", "stdin", "single")
18+
except SyntaxError:
19+
print("SyntaxError")
20+
try:
21+
compile("\\\n", "stdin", "eval")
22+
except SyntaxError:
23+
print("SyntaxError")
24+
compile("\\\n", "stdin", "exec")

0 commit comments

Comments
 (0)