[pypy-svn] r50250 - in pypy/branch/astcompilertests/pypy/interpreter/astcompiler: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Jan 2 11:08:55 CET 2008
Author: arigo
Date: Wed Jan 2 11:08:53 2008
New Revision: 50250
Modified:
pypy/branch/astcompilertests/pypy/interpreter/astcompiler/opt.py
pypy/branch/astcompilertests/pypy/interpreter/astcompiler/test/test_compiler.py
Log:
BUILD_LIST -> BUILD_TUPLE in the few cases where the difference
does not matter.
Modified: pypy/branch/astcompilertests/pypy/interpreter/astcompiler/opt.py
==============================================================================
--- pypy/branch/astcompilertests/pypy/interpreter/astcompiler/opt.py (original)
+++ pypy/branch/astcompilertests/pypy/interpreter/astcompiler/opt.py Wed Jan 2 11:08:53 2008
@@ -180,7 +180,23 @@
def visitBitxor(self, node):
return self._visitBitOp(node, _spacewrapper2('xor'))
- #def visitCompare(self, node): XXX
+ def _List2Tuple(self, node):
+ if isinstance(node, ast.List):
+ newnode = ast.Tuple(node.nodes)
+ copy_node_fields(node, newnode)
+ # if the resulting tuple contains only constants, we can
+ # completely constant-fold the tuple creation itself
+ return self.visitTuple(newnode)
+ else:
+ return node
+
+ def visitCompare(self, node):
+ # xxx could do some constant-folding too, even if it sounds
+ # a bit unlikely to be useful in practice
+ last_op_name, last_subnode = node.ops[-1]
+ if last_op_name == 'in' or last_op_name == 'not in':
+ node.ops[-1] = last_op_name, self._List2Tuple(last_subnode)
+ return node
def _visitAbstractTest(self, node, is_and):
# Logic for And nodes:
@@ -217,6 +233,18 @@
consts_w.append(subnode.value)
return ast.Const(self.space.newtuple(consts_w))
+ def visitFor(self, node):
+ node.list = self._List2Tuple(node.list)
+ return node
+
+ def visitListCompFor(self, node):
+ node.list = self._List2Tuple(node.list)
+ return node
+
+ def visitGenExprFor(self, node):
+ node.iter = self._List2Tuple(node.iter)
+ return node
+
def _spacewrapper1(name):
"""Make a wrapper around the method: space.<name>(w_x)
@@ -241,6 +269,11 @@
def constant_fold_pow(space, w_x, w_y):
return space.pow(w_x, w_y, space.w_None)
+ def copy_node_fields(src, dst):
+ dst.lineno = src.lineno
+ dst.filename = src.filename
+ dst.parent = src.parent
+
def optimize_ast_tree(space, tree):
return tree.mutate(OptimizerMutator(space))
Modified: pypy/branch/astcompilertests/pypy/interpreter/astcompiler/test/test_compiler.py
==============================================================================
--- pypy/branch/astcompilertests/pypy/interpreter/astcompiler/test/test_compiler.py (original)
+++ pypy/branch/astcompilertests/pypy/interpreter/astcompiler/test/test_compiler.py Wed Jan 2 11:08:53 2008
@@ -279,3 +279,38 @@
''', "doc"),
]:
yield self.simple_test, source, "foo.__doc__", expected
+
+ def test_in(self):
+ yield self.st, "n = 5; x = n in [3,4,5]", 'x', True
+ yield self.st, "n = 5; x = n in [3,4,6]", 'x', False
+ yield self.st, "n = 5; x = n in [3,4,n]", 'x', True
+ yield self.st, "n = 5; x = n in [3,4,n+1]", 'x', False
+ yield self.st, "n = 5; x = n in (3,4,5)", 'x', True
+ yield self.st, "n = 5; x = n in (3,4,6)", 'x', False
+ yield self.st, "n = 5; x = n in (3,4,n)", 'x', True
+ yield self.st, "n = 5; x = n in (3,4,n+1)", 'x', False
+
+ def test_for_loops(self):
+ yield self.st, """
+ total = 0
+ for i in [2, 7, 5]:
+ total += i
+ """, 'total', 2 + 7 + 5
+ yield self.st, """
+ total = 0
+ for i in (2, 7, 5):
+ total += i
+ """, 'total', 2 + 7 + 5
+ yield self.st, """
+ total = 0
+ for i in [2, 7, total+5]:
+ total += i
+ """, 'total', 2 + 7 + 5
+ yield self.st, "x = sum([n+2 for n in [6, 1, 2]])", 'x', 15
+ yield self.st, "x = sum([n+2 for n in (6, 1, 2)])", 'x', 15
+ yield self.st, "k=2; x = sum([n+2 for n in [6, 1, k]])", 'x', 15
+ yield self.st, "k=2; x = sum([n+2 for n in (6, 1, k)])", 'x', 15
+ yield self.st, "x = sum(n+2 for n in [6, 1, 2])", 'x', 15
+ yield self.st, "x = sum(n+2 for n in (6, 1, 2))", 'x', 15
+ yield self.st, "k=2; x = sum(n+2 for n in [6, 1, k])", 'x', 15
+ yield self.st, "k=2; x = sum(n+2 for n in (6, 1, k))", 'x', 15
More information about the Pypy-commit
mailing list