Skip to content

[3.7] bpo-36256: Fix bug in parsermodule when parsing if statements (GH-12477) #12488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ def test_try_stmt(self):
self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
"finally: pass\n")

def test_if_stmt(self):
self.check_suite("if True:\n pass\nelse:\n pass\n")
self.check_suite("if True:\n pass\nelif True:\n pass\nelse:\n pass\n")

def test_position(self):
# An absolutely minimal test of position information. Better
# tests would be a big project.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug in parsermodule when parsing a state in a DFA that has two or more
arcs with labels of the same type. Patch by Pablo Galindo.
22 changes: 17 additions & 5 deletions Modules/parsermodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,12 @@ validate_node(node *tree)
for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
short a_label = dfa_state->s_arc[arc].a_lbl;
assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {

const char *label_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
if ((_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type)
&& ((ch->n_str == NULL) || (label_str == NULL)
|| (strcmp(ch->n_str, label_str) == 0))
) {
/* The child is acceptable; if non-terminal, validate it recursively. */
if (ISNONTERMINAL(ch_type) && !validate_node(ch))
return 0;
Expand All @@ -679,17 +684,24 @@ validate_node(node *tree)
/* What would this state have accepted? */
{
short a_label = dfa_state->s_arc->a_lbl;
int next_type;
if (!a_label) /* Wouldn't accept any more children */
goto illegal_num_children;

next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
if (ISNONTERMINAL(next_type))
int next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;

if (ISNONTERMINAL(next_type)) {
PyErr_Format(parser_error, "Expected node type %d, got %d.",
next_type, ch_type);
else
}
else if (expected_str != NULL) {
PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",
expected_str);
}
else {
PyErr_Format(parser_error, "Illegal terminal: expected %s.",
_PyParser_TokenNames[next_type]);
}
return 0;
}

Expand Down