X Tutup
Skip to content

Commit fe879e8

Browse files
committed
#4529: fix parser's validation for try-except-finally statements.
1 parent 3129ea2 commit fe879e8

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

Lib/test/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ def test_with(self):
200200
self.check_suite("with open('x'): pass\n")
201201
self.check_suite("with open('x') as f: pass\n")
202202

203+
def test_try_stmt(self):
204+
self.check_suite("try: pass\nexcept: pass\n")
205+
self.check_suite("try: pass\nfinally: pass\n")
206+
self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
207+
self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
208+
"finally: pass\n")
209+
self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
210+
self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
211+
"finally: pass\n")
212+
203213
def test_position(self):
204214
# An absolutely minimal test of position information. Better
205215
# tests would be a big project.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Core and Builtins
6060
Library
6161
-------
6262

63+
- Issue #4529: fix the parser module's validation of try-except-finally
64+
statements.
65+
6366
- Issue #4458: getopt.gnu_getopt() now recognizes a single "-" as an argument,
6467
not a malformed option.
6568

Modules/parsermodule.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,7 @@ validate_for(node *tree)
20572057

20582058
/* try_stmt:
20592059
* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2060+
['finally' ':' suite]
20602061
* | 'try' ':' suite 'finally' ':' suite
20612062
*
20622063
*/
@@ -2082,35 +2083,34 @@ validate_try(node *tree)
20822083
PyErr_Format(parser_error,
20832084
"Illegal number of children for try/%s node.", name);
20842085
}
2085-
/* Skip past except_clause sections: */
2086+
/* Handle try/finally statement */
2087+
if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2088+
(strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
2089+
res = (validate_numnodes(tree, 6, "try/finally")
2090+
&& validate_colon(CHILD(tree, 4))
2091+
&& validate_suite(CHILD(tree, 5)));
2092+
return (res);
2093+
}
2094+
/* try/except statement: skip past except_clause sections */
20862095
while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
20872096
res = (validate_except_clause(CHILD(tree, pos))
20882097
&& validate_colon(CHILD(tree, pos + 1))
20892098
&& validate_suite(CHILD(tree, pos + 2)));
20902099
pos += 3;
20912100
}
2092-
if (res && (pos < nch)) {
2093-
res = validate_ntype(CHILD(tree, pos), NAME);
2094-
if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2095-
res = (validate_numnodes(tree, 6, "try/finally")
2096-
&& validate_colon(CHILD(tree, 4))
2097-
&& validate_suite(CHILD(tree, 5)));
2098-
else if (res) {
2099-
if (nch == (pos + 3)) {
2100-
res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2101-
|| (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2102-
if (!res)
2103-
err_string("illegal trailing triple in try statement");
2104-
}
2105-
else if (nch == (pos + 6)) {
2106-
res = (validate_name(CHILD(tree, pos), "except")
2107-
&& validate_colon(CHILD(tree, pos + 1))
2108-
&& validate_suite(CHILD(tree, pos + 2))
2109-
&& validate_name(CHILD(tree, pos + 3), "else"));
2110-
}
2111-
else
2112-
res = validate_numnodes(tree, pos + 3, "try/except");
2113-
}
2101+
/* skip else clause */
2102+
if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
2103+
(strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
2104+
res = (validate_colon(CHILD(tree, pos + 1))
2105+
&& validate_suite(CHILD(tree, pos + 2)));
2106+
pos += 3;
2107+
}
2108+
if (res && pos < nch) {
2109+
/* last clause must be a finally */
2110+
res = (validate_name(CHILD(tree, pos), "finally")
2111+
&& validate_numnodes(tree, pos + 3, "try/except/finally")
2112+
&& validate_colon(CHILD(tree, pos + 1))
2113+
&& validate_suite(CHILD(tree, pos + 2)));
21142114
}
21152115
return (res);
21162116
}

0 commit comments

Comments
 (0)
X Tutup