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

Skip to content

Commit 407b3bd

Browse files
committed
Issue #14696: Fix parser module to understand 'nonlocal' declarations.
1 parent 2420d83 commit 407b3bd

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

Lib/test/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ def test_yield_statement(self):
5757
" if (yield):\n"
5858
" yield x\n")
5959

60+
def test_nonlocal_statement(self):
61+
self.check_suite("def f():\n"
62+
" x = 0\n"
63+
" def g():\n"
64+
" nonlocal x\n")
65+
self.check_suite("def f():\n"
66+
" x = y = 0\n"
67+
" def g():\n"
68+
" nonlocal x, y\n")
69+
6070
def test_expressions(self):
6171
self.check_expr("foo(1)")
6272
self.check_expr("[1, 2, 3]")

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Core and Builtins
5656
Library
5757
-------
5858

59+
- Issue #14696: Fix parser module to understand 'nonlocal' declarations.
60+
5961
- Issue #10941: Fix imaplib.Internaldate2tuple to produce correct result near
6062
the DST transition. Patch by Joe Peterson.
6163

Modules/parsermodule.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ VALIDATER(del_stmt);
954954
VALIDATER(return_stmt); VALIDATER(raise_stmt);
955955
VALIDATER(import_stmt); VALIDATER(import_stmt);
956956
VALIDATER(import_name); VALIDATER(yield_stmt);
957-
VALIDATER(global_stmt); VALIDATER(assert_stmt);
957+
VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
958+
VALIDATER(assert_stmt);
958959
VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
959960
VALIDATER(while); VALIDATER(for);
960961
VALIDATER(try); VALIDATER(except_clause);
@@ -1477,6 +1478,7 @@ validate_small_stmt(node *tree)
14771478
|| (ntype == flow_stmt)
14781479
|| (ntype == import_stmt)
14791480
|| (ntype == global_stmt)
1481+
|| (ntype == nonlocal_stmt)
14801482
|| (ntype == assert_stmt))
14811483
res = validate_node(CHILD(tree, 0));
14821484
else {
@@ -1834,8 +1836,10 @@ validate_import_stmt(node *tree)
18341836
}
18351837

18361838

1837-
1838-
1839+
/* global_stmt:
1840+
*
1841+
* 'global' NAME (',' NAME)*
1842+
*/
18391843
static int
18401844
validate_global_stmt(node *tree)
18411845
{
@@ -1857,6 +1861,30 @@ validate_global_stmt(node *tree)
18571861
return (res);
18581862
}
18591863

1864+
/* nonlocal_stmt:
1865+
*
1866+
* 'nonlocal' NAME (',' NAME)*
1867+
*/
1868+
static int
1869+
validate_nonlocal_stmt(node *tree)
1870+
{
1871+
int j;
1872+
int nch = NCH(tree);
1873+
int res = (validate_ntype(tree, nonlocal_stmt)
1874+
&& is_even(nch) && (nch >= 2));
1875+
1876+
if (!res && !PyErr_Occurred())
1877+
err_string("illegal nonlocal statement");
1878+
1879+
if (res)
1880+
res = (validate_name(CHILD(tree, 0), "nonlocal")
1881+
&& validate_ntype(CHILD(tree, 1), NAME));
1882+
for (j = 2; res && (j < nch); j += 2)
1883+
res = (validate_comma(CHILD(tree, j))
1884+
&& validate_ntype(CHILD(tree, j + 1), NAME));
1885+
1886+
return res;
1887+
}
18601888

18611889
/* assert_stmt:
18621890
*
@@ -2921,8 +2949,8 @@ validate_node(node *tree)
29212949
break;
29222950
case small_stmt:
29232951
/*
2924-
* expr_stmt | del_stmt | pass_stmt | flow_stmt
2925-
* | import_stmt | global_stmt | assert_stmt
2952+
* expr_stmt | del_stmt | pass_stmt | flow_stmt |
2953+
* import_stmt | global_stmt | nonlocal_stmt | assert_stmt
29262954
*/
29272955
res = validate_small_stmt(tree);
29282956
break;
@@ -2989,6 +3017,9 @@ validate_node(node *tree)
29893017
case global_stmt:
29903018
res = validate_global_stmt(tree);
29913019
break;
3020+
case nonlocal_stmt:
3021+
res = validate_nonlocal_stmt(tree);
3022+
break;
29923023
case assert_stmt:
29933024
res = validate_assert_stmt(tree);
29943025
break;

0 commit comments

Comments
 (0)