Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf360b9

Browse files
committed
Issue #14701: Add missing support for 'raise ... from' in parser module.
1 parent 640335c commit cf360b9

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

Lib/test/test_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ def test_extended_unpacking(self):
297297
self.check_suite("[*a, *b] = y")
298298
self.check_suite("for [*x, b] in x: pass")
299299

300+
def test_raise_statement(self):
301+
self.check_suite("raise\n")
302+
self.check_suite("raise e\n")
303+
self.check_suite("try:\n"
304+
" suite\n"
305+
"except Exception as e:\n"
306+
" raise ValueError from e\n")
307+
300308

301309
#
302310
# Second, we take *invalid* trees and make sure we get ParserError

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Core and Builtins
6161
Library
6262
-------
6363

64+
- Issue #14701: Fix missing support for 'raise ... from' in parser module.
65+
6466
- Issue #13183: Fix pdb skipping frames after hitting a breakpoint and running
6567
step. Patch by Xavier de Gaye.
6668

Modules/parsermodule.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,31 +1608,30 @@ validate_return_stmt(node *tree)
16081608
}
16091609

16101610

1611+
/*
1612+
* raise_stmt:
1613+
*
1614+
* 'raise' [test ['from' test]]
1615+
*/
16111616
static int
16121617
validate_raise_stmt(node *tree)
16131618
{
16141619
int nch = NCH(tree);
16151620
int res = (validate_ntype(tree, raise_stmt)
1616-
&& ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
1621+
&& ((nch == 1) || (nch == 2) || (nch == 4)));
1622+
1623+
if (!res && !PyErr_Occurred())
1624+
(void) validate_numnodes(tree, 2, "raise");
16171625

16181626
if (res) {
16191627
res = validate_name(CHILD(tree, 0), "raise");
16201628
if (res && (nch >= 2))
16211629
res = validate_test(CHILD(tree, 1));
1622-
if (res && nch > 2) {
1623-
res = (validate_comma(CHILD(tree, 2))
1630+
if (res && (nch == 4)) {
1631+
res = (validate_name(CHILD(tree, 2), "from")
16241632
&& validate_test(CHILD(tree, 3)));
1625-
if (res && (nch > 4))
1626-
res = (validate_comma(CHILD(tree, 4))
1627-
&& validate_test(CHILD(tree, 5)));
16281633
}
16291634
}
1630-
else
1631-
(void) validate_numnodes(tree, 2, "raise");
1632-
if (res && (nch == 4))
1633-
res = (validate_comma(CHILD(tree, 2))
1634-
&& validate_test(CHILD(tree, 3)));
1635-
16361635
return (res);
16371636
}
16381637

0 commit comments

Comments
 (0)