summaryrefslogtreecommitdiffstats
path: root/Lib/compiler
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 /Lib/compiler
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 'Lib/compiler')
-rw-r--r--Lib/compiler/transformer.py24
1 files changed, 14 insertions, 10 deletions
diff --git a/Lib/compiler/transformer.py b/Lib/compiler/transformer.py
index f5fe582..2a156d3 100644
--- a/Lib/compiler/transformer.py
+++ b/Lib/compiler/transformer.py
@@ -965,18 +965,22 @@ class Transformer:
return try_except
def com_with(self, nodelist):
- # with_stmt: 'with' expr [with_var] ':' suite
- expr = self.com_node(nodelist[1])
+ # with_stmt: 'with' with_item (',' with_item)* ':' suite
body = self.com_node(nodelist[-1])
- if nodelist[2][0] == token.COLON:
- var = None
+ for i in range(len(nodelist) - 3, 0, -2):
+ ret = self.com_with_item(nodelist[i], body, nodelist[0][2])
+ if i == 1:
+ return ret
+ body = ret
+
+ def com_with_item(self, nodelist, body, lineno):
+ # with_item: test ['as' expr]
+ if len(nodelist) == 4:
+ var = self.com_assign(nodelist[3], OP_ASSIGN)
else:
- var = self.com_assign(nodelist[2][2], OP_ASSIGN)
- return With(expr, var, body, lineno=nodelist[0][2])
-
- def com_with_var(self, nodelist):
- # with_var: 'as' expr
- return self.com_node(nodelist[1])
+ var = None
+ expr = self.com_node(nodelist[1])
+ return With(expr, var, body, lineno=lineno)
def com_augassign_op(self, node):
assert node[0] == symbol.augassign