Skip to content

Commit 0c31562

Browse files
committed
Merged revisions 72924 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r72924 | georg.brandl | 2009-05-25 23:02:56 +0200 (Mo, 25 Mai 2009) | 6 lines Allow multiple context managers in one with statement, as proposed in http://codereview.appspot.com/53094 and accepted by Guido. The construct is transformed into multiple With AST nodes so that there should be no problems with the semantics. ........
1 parent 0c1829b commit 0c31562

File tree

9 files changed

+180
-60
lines changed

9 files changed

+180
-60
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,10 @@ This allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally`
347347
usage patterns to be encapsulated for convenient reuse.
348348

349349
.. productionlist::
350-
with_stmt: "with" `expression` ["as" `target`] ":" `suite`
350+
with_stmt: "with" with_item ("," with_item)* ":" `suite`
351+
with_item: `expression` ["as" `target`]
351352

352-
The execution of the :keyword:`with` statement proceeds as follows:
353+
The execution of the :keyword:`with` statement with one "item" proceeds as follows:
353354

354355
#. The context expression is evaluated to obtain a context manager.
355356

@@ -382,6 +383,21 @@ The execution of the :keyword:`with` statement proceeds as follows:
382383
value from :meth:`__exit__` is ignored, and execution proceeds at the normal
383384
location for the kind of exit that was taken.
384385

386+
With more than one item, the context managers are processed as if multiple
387+
:keyword:`with` statements were nested::
388+
389+
with A() as a, B() as b:
390+
suite
391+
392+
is equivalent to ::
393+
394+
with A() as a:
395+
with B() as b:
396+
suite
397+
398+
.. versionchanged:: 3.1
399+
Support for multiple context expressions.
400+
385401
.. seealso::
386402

387403
:pep:`0343` - The "with" statement

Grammar/Grammar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ try_stmt: ('try' ':' suite
7373
['else' ':' suite]
7474
['finally' ':' suite] |
7575
'finally' ':' suite))
76-
with_stmt: 'with' test [ with_var ] ':' suite
77-
with_var: 'as' expr
76+
with_stmt: 'with' with_item (',' with_item)* ':' suite
77+
with_item: test ['as' expr]
7878
# NB compile.c makes sure that the default except clause is last
7979
except_clause: 'except' [test ['as' NAME]]
8080
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

Include/graminit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#define for_stmt 295
4343
#define try_stmt 296
4444
#define with_stmt 297
45-
#define with_var 298
45+
#define with_item 298
4646
#define except_clause 299
4747
#define suite 300
4848
#define test 301

Lib/test/test_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def test_assert(self):
193193
def test_with(self):
194194
self.check_suite("with open('x'): pass\n")
195195
self.check_suite("with open('x') as f: pass\n")
196+
self.check_suite("with open('x') as f, open('y') as g: pass\n")
196197

197198
def test_try_stmt(self):
198199
self.check_suite("try: pass\nexcept: pass\n")

Lib/test/test_with.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,88 @@ def __exit__(self, t, v, tb): return False
656656
self.fail("ZeroDivisionError should have been raised")
657657

658658

659+
class NestedWith(unittest.TestCase):
660+
661+
class Dummy(object):
662+
def __init__(self, value=None, gobble=False):
663+
if value is None:
664+
value = self
665+
self.value = value
666+
self.gobble = gobble
667+
self.enter_called = False
668+
self.exit_called = False
669+
670+
def __enter__(self):
671+
self.enter_called = True
672+
return self.value
673+
674+
def __exit__(self, *exc_info):
675+
self.exit_called = True
676+
self.exc_info = exc_info
677+
if self.gobble:
678+
return True
679+
680+
class CtorRaises(object):
681+
def __init__(self): raise RuntimeError()
682+
683+
class EnterRaises(object):
684+
def __enter__(self): raise RuntimeError()
685+
def __exit__(self, *exc_info): pass
686+
687+
class ExitRaises(object):
688+
def __enter__(self): pass
689+
def __exit__(self, *exc_info): raise RuntimeError()
690+
691+
def testNoExceptions(self):
692+
with self.Dummy() as a, self.Dummy() as b:
693+
self.assertTrue(a.enter_called)
694+
self.assertTrue(b.enter_called)
695+
self.assertTrue(a.exit_called)
696+
self.assertTrue(b.exit_called)
697+
698+
def testExceptionInExprList(self):
699+
try:
700+
with self.Dummy() as a, self.CtorRaises():
701+
pass
702+
except:
703+
pass
704+
self.assertTrue(a.enter_called)
705+
self.assertTrue(a.exit_called)
706+
707+
def testExceptionInEnter(self):
708+
try:
709+
with self.Dummy() as a, self.EnterRaises():
710+
self.fail('body of bad with executed')
711+
except RuntimeError:
712+
pass
713+
else:
714+
self.fail('RuntimeError not reraised')
715+
self.assertTrue(a.enter_called)
716+
self.assertTrue(a.exit_called)
717+
718+
def testExceptionInExit(self):
719+
body_executed = False
720+
with self.Dummy(gobble=True) as a, self.ExitRaises():
721+
body_executed = True
722+
self.assertTrue(a.enter_called)
723+
self.assertTrue(a.exit_called)
724+
self.assertNotEqual(a.exc_info[0], None)
725+
726+
def testEnterReturnsTuple(self):
727+
with self.Dummy(value=(1,2)) as (a1, a2), \
728+
self.Dummy(value=(10, 20)) as (b1, b2):
729+
self.assertEquals(1, a1)
730+
self.assertEquals(2, a2)
731+
self.assertEquals(10, b1)
732+
self.assertEquals(20, b2)
733+
659734
def test_main():
660735
run_unittest(FailureTestCase, NonexceptionalTestCase,
661736
NestedNonexceptionalTestCase, ExceptionalTestCase,
662737
NonLocalFlowControlTestCase,
663738
AssignmentTargetTestCase,
664-
ExitSwallowsExceptionTestCase)
739+
ExitSwallowsExceptionTestCase,
740+
NestedWith)
665741

666742

667743
if __name__ == '__main__':

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Core and Builtins
1515
- Issue #6089: Fixed str.format with certain invalid field specifiers
1616
that would raise SystemError.
1717

18+
- Added support for multiple context managers in the same with statement.
19+
1820
- Issue #5829: complex("1e500") no longer raises OverflowError. This
1921
makes it consistent with float("1e500") and interpretation of real
2022
and imaginary literals.

Modules/parsermodule.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,36 +2446,39 @@ validate_decorators(node *tree)
24462446
return ok;
24472447
}
24482448

2449-
/* with_var
2450-
with_var: 'as' expr
2449+
/* with_item:
2450+
* test ['as' expr]
24512451
*/
24522452
static int
2453-
validate_with_var(node *tree)
2453+
validate_with_item(node *tree)
24542454
{
24552455
int nch = NCH(tree);
2456-
int ok = (validate_ntype(tree, with_var)
2457-
&& (nch == 2)
2458-
&& validate_name(CHILD(tree, 0), "as")
2459-
&& validate_expr(CHILD(tree, 1)));
2460-
return ok;
2456+
int ok = (validate_ntype(tree, with_item)
2457+
&& (nch == 1 || nch == 3)
2458+
&& validate_test(CHILD(tree, 0)));
2459+
if (ok && nch == 3)
2460+
ok = (validate_name(CHILD(tree, 1), "as")
2461+
&& validate_expr(CHILD(tree, 2)));
2462+
return ok;
24612463
}
24622464

2463-
/* with_stmt
2464-
* 0 1 2 -2 -1
2465-
with_stmt: 'with' test [ with_var ] ':' suite
2465+
/* with_stmt:
2466+
* 0 1 ... -2 -1
2467+
* 'with' with_item (',' with_item)* ':' suite
24662468
*/
24672469
static int
24682470
validate_with_stmt(node *tree)
24692471
{
2472+
int i;
24702473
int nch = NCH(tree);
24712474
int ok = (validate_ntype(tree, with_stmt)
2472-
&& ((nch == 4) || (nch == 5))
2475+
&& (nch % 2 == 0)
24732476
&& validate_name(CHILD(tree, 0), "with")
2474-
&& validate_test(CHILD(tree, 1))
2475-
&& (nch == 4 || validate_with_var(CHILD(tree, 2)))
24762477
&& validate_colon(RCHILD(tree, -2))
24772478
&& validate_suite(RCHILD(tree, -1)));
2478-
return ok;
2479+
for (i = 1; ok && i < nch - 2; i += 2)
2480+
ok = validate_with_item(CHILD(tree, i));
2481+
return ok;
24792482
}
24802483

24812484
/* funcdef:

Python/ast.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,41 +2959,62 @@ ast_for_try_stmt(struct compiling *c, const node *n)
29592959
return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
29602960
}
29612961

2962-
static expr_ty
2963-
ast_for_with_var(struct compiling *c, const node *n)
2964-
{
2965-
REQ(n, with_var);
2966-
return ast_for_expr(c, CHILD(n, 1));
2967-
}
2968-
2969-
/* with_stmt: 'with' test [ with_var ] ':' suite */
2962+
/* with_item: test ['as' expr] */
29702963
static stmt_ty
2971-
ast_for_with_stmt(struct compiling *c, const node *n)
2964+
ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
29722965
{
29732966
expr_ty context_expr, optional_vars = NULL;
2974-
int suite_index = 3; /* skip 'with', test, and ':' */
2975-
asdl_seq *suite_seq;
29762967

2977-
assert(TYPE(n) == with_stmt);
2978-
context_expr = ast_for_expr(c, CHILD(n, 1));
2979-
if (TYPE(CHILD(n, 2)) == with_var) {
2980-
optional_vars = ast_for_with_var(c, CHILD(n, 2));
2968+
REQ(n, with_item);
2969+
context_expr = ast_for_expr(c, CHILD(n, 0));
2970+
if (NCH(n) == 3) {
2971+
optional_vars = ast_for_expr(c, CHILD(n, 2));
29812972

29822973
if (!optional_vars) {
29832974
return NULL;
29842975
}
29852976
if (!set_context(c, optional_vars, Store, n)) {
29862977
return NULL;
29872978
}
2988-
suite_index = 4;
29892979
}
29902980

2991-
suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2992-
if (!suite_seq) {
2981+
return With(context_expr, optional_vars, content, LINENO(n),
2982+
n->n_col_offset, c->c_arena);
2983+
}
2984+
2985+
/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
2986+
static stmt_ty
2987+
ast_for_with_stmt(struct compiling *c, const node *n)
2988+
{
2989+
int i;
2990+
stmt_ty ret;
2991+
asdl_seq *inner;
2992+
2993+
REQ(n, with_stmt);
2994+
2995+
/* process the with items inside-out */
2996+
i = NCH(n) - 1;
2997+
/* the suite of the innermost with item is the suite of the with stmt */
2998+
inner = ast_for_suite(c, CHILD(n, i));
2999+
if (!inner)
29933000
return NULL;
3001+
3002+
for (;;) {
3003+
i -= 2;
3004+
ret = ast_for_with_item(c, CHILD(n, i), inner);
3005+
if (!ret)
3006+
return NULL;
3007+
/* was this the last item? */
3008+
if (i == 1)
3009+
break;
3010+
/* if not, wrap the result so far in a new sequence */
3011+
inner = asdl_seq_new(1, c->c_arena);
3012+
if (!inner)
3013+
return NULL;
3014+
asdl_seq_SET(inner, 0, ret);
29943015
}
2995-
return With(context_expr, optional_vars, suite_seq, LINENO(n),
2996-
n->n_col_offset, c->c_arena);
3016+
3017+
return ret;
29973018
}
29983019

29993020
static stmt_ty

Python/graminit.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -911,42 +911,43 @@ static arc arcs_41_0[1] = {
911911
{99, 1},
912912
};
913913
static arc arcs_41_1[1] = {
914-
{24, 2},
914+
{100, 2},
915915
};
916916
static arc arcs_41_2[2] = {
917-
{100, 3},
918-
{25, 4},
917+
{30, 1},
918+
{25, 3},
919919
};
920920
static arc arcs_41_3[1] = {
921-
{25, 4},
921+
{26, 4},
922922
};
923923
static arc arcs_41_4[1] = {
924-
{26, 5},
925-
};
926-
static arc arcs_41_5[1] = {
927-
{0, 5},
924+
{0, 4},
928925
};
929-
static state states_41[6] = {
926+
static state states_41[5] = {
930927
{1, arcs_41_0},
931928
{1, arcs_41_1},
932929
{2, arcs_41_2},
933930
{1, arcs_41_3},
934931
{1, arcs_41_4},
935-
{1, arcs_41_5},
936932
};
937933
static arc arcs_42_0[1] = {
938-
{80, 1},
934+
{24, 1},
939935
};
940-
static arc arcs_42_1[1] = {
941-
{101, 2},
936+
static arc arcs_42_1[2] = {
937+
{80, 2},
938+
{0, 1},
942939
};
943940
static arc arcs_42_2[1] = {
944-
{0, 2},
941+
{101, 3},
942+
};
943+
static arc arcs_42_3[1] = {
944+
{0, 3},
945945
};
946-
static state states_42[3] = {
946+
static state states_42[4] = {
947947
{1, arcs_42_0},
948-
{1, arcs_42_1},
948+
{2, arcs_42_1},
949949
{1, arcs_42_2},
950+
{1, arcs_42_3},
950951
};
951952
static arc arcs_43_0[1] = {
952953
{102, 1},
@@ -1810,10 +1811,10 @@ static dfa dfas[81] = {
18101811
"\000\000\000\000\000\000\000\000\000\000\000\100\000\000\000\000\000\000\000\000\000"},
18111812
{296, "try_stmt", 0, 13, states_40,
18121813
"\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\000"},
1813-
{297, "with_stmt", 0, 6, states_41,
1814+
{297, "with_stmt", 0, 5, states_41,
18141815
"\000\000\000\000\000\000\000\000\000\000\000\000\010\000\000\000\000\000\000\000\000"},
1815-
{298, "with_var", 0, 3, states_42,
1816-
"\000\000\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\000\000\000"},
1816+
{298, "with_item", 0, 4, states_42,
1817+
"\000\040\040\200\000\000\000\000\000\040\000\000\000\040\004\000\000\103\050\037\000"},
18171818
{299, "except_clause", 0, 5, states_43,
18181819
"\000\000\000\000\000\000\000\000\000\000\000\000\100\000\000\000\000\000\000\000\000"},
18191820
{300, "suite", 0, 5, states_44,

0 commit comments

Comments
 (0)