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.
........
This commit is contained in:
Georg Brandl 2009-05-25 21:10:36 +00:00
parent 0c1829b919
commit 0c31562a91
9 changed files with 181 additions and 61 deletions

View file

@ -347,9 +347,10 @@ This 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.
@ -382,6 +383,21 @@ The execution of the :keyword:`with` statement proceeds as follows:
value 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
.. versionchanged:: 3.1
Support for multiple context expressions.
.. seealso::
:pep:`0343` - The "with" statement