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.
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst
index afb7ebc..354ed1f 100644
--- a/Doc/reference/compound_stmts.rst
+++ b/Doc/reference/compound_stmts.rst
@@ -333,9 +333,10 @@
patterns to be encapsulated for convenient reuse.
.. productionlist::
- with_stmt: "with" `expression` ["as" `target`] ":" `suite`
+ with_stmt: "with" with_item ("," with_item)* ":" `suite`
+ with_item: `expression` ["as" `target`]
-The execution of the :keyword:`with` statement proceeds as follows:
+The execution of the :keyword:`with` statement with one "item" proceeds as follows:
#. The context expression is evaluated to obtain a context manager.
@@ -369,12 +370,27 @@
from :meth:`__exit__` is ignored, and execution proceeds at the normal location
for the kind of exit that was taken.
+With more than one item, the context managers are processed as if multiple
+:keyword:`with` statements were nested::
+
+ with A() as a, B() as b:
+ suite
+
+is equivalent to ::
+
+ with A() as a:
+ with B() as b:
+ suite
+
.. note::
In Python 2.5, the :keyword:`with` statement is only allowed when the
``with_statement`` feature has been enabled. It is always enabled in
Python 2.6.
+.. versionchanged:: 2.7
+ Support for multiple context expressions.
+
.. seealso::
:pep:`0343` - The "with" statement