summaryrefslogtreecommitdiffstats
path: root/Source/CursesDialog/form/fld_opts.c
diff options
context:
space:
mode:
Diffstat (limited to 'Source/CursesDialog/form/fld_opts.c')
0 files changed, 0 insertions, 0 deletions
309c7c4b5357af51f4d5'>Grammar/Grammar3
-rw-r--r--Include/Python-ast.h10
-rw-r--r--Include/graminit.h101
-rw-r--r--Include/symtable.h21
-rw-r--r--Lib/test/test_scope.py84
-rw-r--r--Lib/test/test_syntax.py40
-rw-r--r--Parser/Python.asdl1
-rw-r--r--Python/Python-ast.c31
-rw-r--r--Python/ast.c27
-rw-r--r--Python/compile.c1
-rw-r--r--Python/graminit.c1598
-rw-r--r--Python/symtable.c77
12 files changed, 1130 insertions, 864 deletions
diff --git a/Grammar/Grammar b/Grammar/Grammar
index 7606d6e..0277799 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -39,7 +39,7 @@ vfplist: vfpdef (',' vfpdef)* [',']
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
- import_stmt | global_stmt | assert_stmt)
+ import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
expr_stmt: testlist (augassign (yield_expr|testlist) |
('=' (yield_expr|testlist))*)
augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
@@ -63,6 +63,7 @@ import_as_names: import_as_name (',' import_as_name)* [',']
dotted_as_names: dotted_as_name (',' dotted_as_name)*
dotted_name: NAME ('.' NAME)*
global_stmt: 'global' NAME (',' NAME)*
+nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
assert_stmt: 'assert' test [',' test]
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
diff --git a/Include/Python-ast.h b/Include/Python-ast.h
index 66d7b52..233a576 100644
--- a/Include/Python-ast.h
+++ b/Include/Python-ast.h
@@ -66,7 +66,8 @@ enum _stmt_kind {FunctionDef_kind=1, ClassDef_kind=2, Return_kind=3,
While_kind=8, If_kind=9, With_kind=10, Raise_kind=11,
TryExcept_kind=12, TryFinally_kind=13, Assert_kind=14,
Import_kind=15, ImportFrom_kind=16, Global_kind=17,
- Expr_kind=18, Pass_kind=19, Break_kind=20, Continue_kind=21};
+ Nonlocal_kind=18, Expr_kind=19, Pass_kind=20, Break_kind=21,
+ Continue_kind=22};
struct _stmt {
enum _stmt_kind kind;
union {
@@ -165,6 +166,10 @@ struct _stmt {
} Global;
struct {
+ asdl_seq *names;
+ } Nonlocal;
+
+ struct {
expr_ty value;
} Expr;
@@ -422,6 +427,9 @@ stmt_ty _Py_ImportFrom(identifier module, asdl_seq * names, int level, int
#define Global(a0, a1, a2, a3) _Py_Global(a0, a1, a2, a3)
stmt_ty _Py_Global(asdl_seq * names, int lineno, int col_offset, PyArena
*arena);
+#define Nonlocal(a0, a1, a2, a3) _Py_Nonlocal(a0, a1, a2, a3)
+stmt_ty _Py_Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena
+ *arena);
#define Expr(a0, a1, a2, a3) _Py_Expr(a0, a1, a2, a3)
stmt_ty _Py_Expr(expr_ty value, int lineno, int col_offset, PyArena *arena);
#define Pass(a0, a1, a2) _Py_Pass(a0, a1, a2)
diff --git a/Include/graminit.h b/Include/graminit.h
index 7da9004..79e40c4 100644
--- a/Include/graminit.h
+++ b/Include/graminit.h
@@ -35,53 +35,54 @@
#define dotted_as_names 290
#define dotted_name 291
#define global_stmt 292
-#define assert_stmt 293
-#define compound_stmt 294
-#define if_stmt 295
-#define while_stmt 296
-#define for_stmt 297
-#define try_stmt 298
-#define with_stmt 299
-#define with_var 300
-#define except_clause 301
-#define suite 302
-#define testlist_safe 303
-#define old_test 304
-#define old_lambdef 305
-#define test 306
-#define or_test 307
-#define and_test 308
-#define not_test 309
-#define comparison 310
-#define comp_op 311
-#define expr 312
-#define xor_expr 313
-#define and_expr 314
-#define shift_expr 315
-#define arith_expr 316
-#define term 317
-#define factor 318
-#define power 319
-#define atom 320
-#define listmaker 321
-#define testlist_gexp 322
-#define lambdef 323
-#define trailer 324
-#define subscriptlist 325
-#define subscript 326
-#define sliceop 327
-#define exprlist 328
-#define testlist 329
-#define dictsetmaker 330
-#define classdef 331
-#define arglist 332
-#define argument 333
-#define list_iter 334
-#define list_for 335
-#define list_if 336
-#define gen_iter 337
-#define gen_for 338
-#define gen_if 339
-#define testlist1 340
-#define encoding_decl 341
-#define yield_expr 342
+#define nonlocal_stmt 293
+#define assert_stmt 294
+#define compound_stmt 295
+#define if_stmt 296
+#define while_stmt 297
+#define for_stmt 298
+#define try_stmt 299
+#define with_stmt 300
+#define with_var 301
+#define except_clause 302
+#define suite 303
+#define testlist_safe 304
+#define old_test 305
+#define old_lambdef 306
+#define test 307
+#define or_test 308
+#define and_test 309
+#define not_test 310
+#define comparison 311
+#define comp_op 312
+#define expr 313
+#define xor_expr 314
+#define and_expr 315
+#define shift_expr 316
+#define arith_expr 317
+#define term 318
+#define factor 319
+#define power 320
+#define atom 321
+#define listmaker 322
+#define testlist_gexp 323
+#define lambdef 324
+#define trailer 325
+#define subscriptlist 326
+#define subscript 327
+#define sliceop 328
+#define exprlist 329
+#define testlist 330
+#define dictsetmaker 331
+#define classdef 332
+#define arglist 333
+#define argument 334
+#define list_iter 335
+#define list_for 336
+#define list_if 337
+#define gen_iter 338
+#define gen_for 339
+#define gen_if 340
+#define testlist1 341
+#define encoding_decl 342
+#define yield_expr 343
diff --git a/Include/symtable.h b/Include/symtable.h
index f40bfa4..5f50105 100644
--- a/Include/symtable.h
+++ b/Include/symtable.h
@@ -64,23 +64,24 @@ PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
#define DEF_GLOBAL 1 /* global stmt */
#define DEF_LOCAL 2 /* assignment in code block */
#define DEF_PARAM 2<<1 /* formal parameter */
-#define USE 2<<2 /* name is used */
-#define DEF_STAR 2<<3 /* parameter is star arg */
-#define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
-#define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
-#define DEF_FREE 2<<6 /* name used but not defined in nested block */
-#define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
-#define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
-#define DEF_IMPORT 2<<9 /* assignment occurred via import */
+#define DEF_NONLOCAL 2<<2 /* nonlocal stmt */
+#define USE 2<<3 /* name is used */
+#define DEF_STAR 2<<4 /* parameter is star arg */
+#define DEF_DOUBLESTAR 2<<5 /* parameter is star-star arg */
+#define DEF_INTUPLE 2<<6 /* name defined in tuple in parameters */
+#define DEF_FREE 2<<7 /* name used but not defined in nested block */
+#define DEF_FREE_GLOBAL 2<<8 /* free variable is actually implicit global */
+#define DEF_FREE_CLASS 2<<9 /* free variable from class's method */
+#define DEF_IMPORT 2<<10 /* assignment occurred via import */
#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
table. GLOBAL is returned from PyST_GetScope() for either of them.
- It is stored in ste_symbols at bits 12-14.
+ It is stored in ste_symbols at bits 12-15.
*/
#define SCOPE_OFF 11
-#define SCOPE_MASK 7
+#define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
#define LOCAL 1
#define GLOBAL_EXPLICIT 2
diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py
index b9dc711..5fe1bc7 100644
--- a/Lib/test/test_scope.py
+++ b/Lib/test/test_scope.py
@@ -555,6 +555,90 @@ self.assert_(X.passed)
f(4)()
+ def testNonLocalFunction(self):
+
+ def f(x):
+ def inc():
+ nonlocal x
+ x += 1
+ return x
+ def dec():
+ nonlocal x
+ x -= 1
+ return x
+ return inc, dec
+
+ inc, dec = f(0)
+ self.assertEqual(inc(), 1)
+ self.assertEqual(inc(), 2)
+ self.assertEqual(dec(), 1)
+ self.assertEqual(dec(), 0)
+
+ def testNonLocalMethod(self):
+
+ def f(x):
+ class c:
+ def inc(self):
+ nonlocal x
+ x += 1
+ return x
+ def dec(self):
+ nonlocal x
+ x -= 1
+ return x
+ return c()
+
+ c = f(0)
+ self.assertEqual(c.inc(), 1)
+ self.assertEqual(c.inc(), 2)
+ self.assertEqual(c.dec(), 1)
+ self.assertEqual(c.dec(), 0)
+
+ def testNonLocalClass(self):
+
+ def f(x):
+ class c:
+ nonlocal x
+ x += 1
+ def get(self):
+ return x
+ return c()
+
+ c = f(0)
+ self.assertEqual(c.get(), 1)
+ self.assert_("x" not in c.__class__.__dict__)
+
+
+ def testNonLocalGenerator(self):
+
+ def f(x):
+ def g(y):
+ nonlocal x
+ for i in range(y):
+ x += 1
+ yield x
+ return g
+
+ g = f(0)
+ self.assertEqual(list(g(5)), [1, 2, 3, 4, 5])
+
+ def testNestedNonLocal(self):
+
+ def f(x):
+ def g():
+ nonlocal x
+ x -= 2
+ def h():
+ nonlocal x
+ x += 4
+ return x
+ return h
+ return g
+
+ g = f(1)
+ h = g()
+ self.assertEqual(h(), 3)
+
def test_main():
run_unittest(ScopeTests)
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py