summaryrefslogtreecommitdiff
path: root/Doc/reference/compound_stmts.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/reference/compound_stmts.rst')
-rw-r--r--Doc/reference/compound_stmts.rst27
1 files changed, 23 insertions, 4 deletions
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst
index 3c716b286f..9dfa31f1a6 100644
--- a/Doc/reference/compound_stmts.rst
+++ b/Doc/reference/compound_stmts.rst
@@ -333,11 +333,15 @@ 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.
+#. The context expression (the expression given in the :token:`with_item`) is
+ evaluated to obtain a context manager.
+
+#. The context manager's :meth:`__exit__` is loaded for later use.
#. The context manager's :meth:`__enter__` method is invoked.
@@ -349,7 +353,7 @@ The execution of the :keyword:`with` statement proceeds as follows:
The :keyword:`with` statement guarantees that if the :meth:`__enter__` method
returns without an error, then :meth:`__exit__` will always be called. Thus, if
an error occurs during the assignment to the target list, it will be treated the
- same as an error occurring within the suite would be. See step 5 below.
+ same as an error occurring within the suite would be. See step 6 below.
#. The suite is executed.
@@ -367,12 +371,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