Skip to content

Commit 5740489

Browse files
committed
Issue #14696: Merge from 3.2
2 parents 53c6651 + 407b3bd commit 5740489

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
@@ -61,6 +61,16 @@ def test_yield_statement(self):
6161
" if (yield):\n"
6262
" yield x\n")
6363

64+
def test_nonlocal_statement(self):
65+
self.check_suite("def f():\n"
66+
" x = 0\n"
67+
" def g():\n"
68+
" nonlocal x\n")
69+
self.check_suite("def f():\n"
70+
" x = y = 0\n"
71+
" def g():\n"
72+
" nonlocal x, y\n")
73+
6474
def test_expressions(self):
6575
self.check_expr("foo(1)")
6676
self.check_expr("[1, 2, 3]")

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ Core and Builtins
8484
Library
8585
-------
8686

87+
- Issue #14696: Fix parser module to understand 'nonlocal' declarations.
88+
8789
- Issue #10941: Fix imaplib.Internaldate2tuple to produce correct result near
8890
the DST transition. Patch by Joe Peterson.
8991

Modules/parsermodule.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@ VALIDATER(del_stmt);
956956
VALIDATER(return_stmt); VALIDATER(raise_stmt);
957957
VALIDATER(import_stmt); VALIDATER(import_stmt);
958958
VALIDATER(import_name); VALIDATER(yield_stmt);
959-
VALIDATER(global_stmt); VALIDATER(assert_stmt);
959+
VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
960+
VALIDATER(assert_stmt);
960961
VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
961962
VALIDATER(while); VALIDATER(for);
962963
VALIDATER(try); VALIDATER(except_clause);
@@ -1480,6 +1481,7 @@ validate_small_stmt(node *tree)
14801481
|| (ntype == flow_stmt)
14811482
|| (ntype == import_stmt)
14821483
|| (ntype == global_stmt)
1484+
|| (ntype == nonlocal_stmt)
14831485
|| (ntype == assert_stmt))
14841486
res = validate_node(CHILD(tree, 0));
14851487
else {
@@ -1864,8 +1866,10 @@ validate_import_stmt(node *tree)
18641866
}
18651867

18661868

1867-
1868-
1869+
/* global_stmt:
1870+
*
1871+
* 'global' NAME (',' NAME)*
1872+
*/
18691873
static int
18701874
validate_global_stmt(node *tree)
18711875
{
@@ -1887,6 +1891,30 @@ validate_global_stmt(node *tree)
18871891
return (res);
18881892
}
18891893

1894+
/* nonlocal_stmt:
1895+
*
1896+
* 'nonlocal' NAME (',' NAME)*
1897+
*/
1898+
static int
1899+
validate_nonlocal_stmt(node *tree)
1900+
{
1901+
int j;
1902+
int nch = NCH(tree);
1903+
int res = (validate_ntype(tree, nonlocal_stmt)
1904+
&& is_even(nch) && (nch >= 2));
1905+
1906+
if (!res && !PyErr_Occurred())
1907+
err_string("illegal nonlocal statement");
1908+
1909+
if (res)
1910+
res = (validate_name(CHILD(tree, 0), "nonlocal")
1911+
&& validate_ntype(CHILD(tree, 1), NAME));
1912+
for (j = 2; res && (j < nch); j += 2)
1913+
res = (validate_comma(CHILD(tree, j))
1914+
&& validate_ntype(CHILD(tree, j + 1), NAME));
1915+
1916+
return res;
1917+
}
18901918

18911919
/* assert_stmt:
18921920
*
@@ -2951,8 +2979,8 @@ validate_node(node *tree)
29512979
break;
29522980
case small_stmt:
29532981
/*
2954-
* expr_stmt | del_stmt | pass_stmt | flow_stmt
2955-
* | import_stmt | global_stmt | assert_stmt
2982+
* expr_stmt | del_stmt | pass_stmt | flow_stmt |
2983+
* import_stmt | global_stmt | nonlocal_stmt | assert_stmt
29562984
*/
29572985
res = validate_small_stmt(tree);
29582986
break;
@@ -3019,6 +3047,9 @@ validate_node(node *tree)
30193047
case global_stmt:
30203048
res = validate_global_stmt(tree);
30213049
break;
3050+
case nonlocal_stmt:
3051+
res = validate_nonlocal_stmt(tree);
3052+
break;
30223053
case assert_stmt:
30233054
res = validate_assert_stmt(tree);
30243055
break;

0 commit comments

Comments
 (0)