Index: Lib/test/test_parser.py =================================================================== --- Lib/test/test_parser.py (revision 67354) +++ Lib/test/test_parser.py (working copy) @@ -188,6 +188,10 @@ def test_assert(self): self.check_suite("assert alo < ahi and blo < bhi\n") + + def test_with(self): + self.check_suite("with open('x'): pass\n") + self.check_suite("with open('x') as f: pass\n") def test_position(self): # An absolutely minimal test of position information. Better Index: Modules/parsermodule.c =================================================================== --- Modules/parsermodule.c (revision 67354) +++ Modules/parsermodule.c (working copy) @@ -1438,7 +1438,7 @@ /* compound_stmt: - * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated + * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated */ static int validate_compound_stmt(node *tree) @@ -1456,6 +1456,7 @@ || (ntype == while_stmt) || (ntype == for_stmt) || (ntype == try_stmt) + || (ntype == with_stmt) || (ntype == funcdef) || (ntype == classdef) || (ntype == decorated)) @@ -2399,6 +2400,38 @@ return ok; } +/* with_var +with_var: 'as' expr + */ +static int +validate_with_var(node *tree) +{ + int nch = NCH(tree); + int ok = (validate_ntype(tree, with_var) + && (nch == 2) + && validate_name(CHILD(tree, 0), "as") + && validate_expr(CHILD(tree, 1))); + return ok; +} + +/* with_stmt + * 0 1 2 -2 -1 +with_stmt: 'with' test [ with_var ] ':' suite + */ +static int +validate_with_stmt(node *tree) +{ + int nch = NCH(tree); + int ok = (validate_ntype(tree, with_stmt) + && ((nch == 4) || (nch == 5)) + && validate_name(CHILD(tree, 0), "with") + && validate_test(CHILD(tree, 1)) + && (nch == 4 || validate_with_var(CHILD(tree, 2))) + && validate_name(RCHILD(tree, -2), ":") */ + && validate_suite(RCHILD(tree, -1))); + return ok; +} + /* funcdef: * * -5 -4 -3 -2 -1 @@ -2775,6 +2808,9 @@ case funcdef: res = validate_funcdef(tree); break; + case with_stmt: + res = validate_with_stmt(tree); + break; case classdef: res = validate_class(tree); break;