summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-05-25 21:02:56 (GMT)
committerGeorg Brandl <georg@python.org>2009-05-25 21:02:56 (GMT)
commit944f684ce6f439bc868d4b189c45f726dfb9d3b1 (patch)
tree3fd9c596e78b01836158508978ce8eab678bb37b /Doc/reference
parent04516611e7e4ceaef6fef9413719e9cb5b4bb087 (diff)
downloadcpython-944f684ce6f439bc868d4b189c45f726dfb9d3b1.zip
cpython-944f684ce6f439bc868d4b189c45f726dfb9d3b1.tar.gz
cpython-944f684ce6f439bc868d4b189c45f726dfb9d3b1.tar.bz2
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.
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/compound_stmts.rst20
1 files changed, 18 insertions, 2 deletions
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 @@ allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` usage
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 @@ The execution of the :keyword:`with` statement proceeds as follows:
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