Commits

Mark Dickinson committed b7e491b

Issue #14696: Fix parser module to understand 'nonlocal' declarations.

  • Participants
  • Parent commits 90b9781
  • Branches 3.2

Comments (0)

Files changed (3)

File Lib/test/test_parser.py

                          "    if (yield):\n"
                          "        yield x\n")
 
+    def test_nonlocal_statement(self):
+        self.check_suite("def f():\n"
+                         "    x = 0\n"
+                         "    def g():\n"
+                         "        nonlocal x\n")
+        self.check_suite("def f():\n"
+                         "    x = y = 0\n"
+                         "    def g():\n"
+                         "        nonlocal x, y\n")
+
     def test_expressions(self):
         self.check_expr("foo(1)")
         self.check_expr("[1, 2, 3]")
 Library
 -------
 
+- Issue #14696: Fix parser module to understand 'nonlocal' declarations.
+
 - Issue #10941: Fix imaplib.Internaldate2tuple to produce correct result near
   the DST transition.  Patch by Joe Peterson.
 

File Modules/parsermodule.c

 VALIDATER(return_stmt);         VALIDATER(raise_stmt);
 VALIDATER(import_stmt);         VALIDATER(import_stmt);
 VALIDATER(import_name);         VALIDATER(yield_stmt);
-VALIDATER(global_stmt);         VALIDATER(assert_stmt);
+VALIDATER(global_stmt);         VALIDATER(nonlocal_stmt);
+VALIDATER(assert_stmt);
 VALIDATER(compound_stmt);       VALIDATER(test_or_star_expr);
 VALIDATER(while);               VALIDATER(for);
 VALIDATER(try);                 VALIDATER(except_clause);
               || (ntype == flow_stmt)
               || (ntype == import_stmt)
               || (ntype == global_stmt)
+              || (ntype == nonlocal_stmt)
               || (ntype == assert_stmt))
             res = validate_node(CHILD(tree, 0));
         else {
 }
 
 
-
-
+/*  global_stmt:
+ *
+ *  'global' NAME (',' NAME)*
+ */
 static int
 validate_global_stmt(node *tree)
 {
     return (res);
 }
 
+/*  nonlocal_stmt:
+ *
+ *  'nonlocal' NAME (',' NAME)*
+ */
+static int
+validate_nonlocal_stmt(node *tree)
+{
+    int j;
+    int nch = NCH(tree);
+    int res = (validate_ntype(tree, nonlocal_stmt)
+               && is_even(nch) && (nch >= 2));
+
+    if (!res && !PyErr_Occurred())
+        err_string("illegal nonlocal statement");
+
+    if (res)
+        res = (validate_name(CHILD(tree, 0), "nonlocal")
+               && validate_ntype(CHILD(tree, 1), NAME));
+    for (j = 2; res && (j < nch); j += 2)
+        res = (validate_comma(CHILD(tree, j))
+               && validate_ntype(CHILD(tree, j + 1), NAME));
+
+    return res;
+}
 
 /*  assert_stmt:
  *
             break;
           case small_stmt:
             /*
-             *  expr_stmt | del_stmt | pass_stmt | flow_stmt
-             *  | import_stmt | global_stmt | assert_stmt
+             *  expr_stmt | del_stmt | pass_stmt | flow_stmt |
+             *  import_stmt | global_stmt | nonlocal_stmt | assert_stmt
              */
             res = validate_small_stmt(tree);
             break;
           case global_stmt:
             res = validate_global_stmt(tree);
             break;
+          case nonlocal_stmt:
+            res = validate_nonlocal_stmt(tree);
+            break;
           case assert_stmt:
             res = validate_assert_stmt(tree);
             break;