[pypy-svn] r50253 - in pypy/branch/astcompilertests/pypy/interpreter/astcompiler: . test

arigo at codespeak.net arigo at codespeak.net
Wed Jan 2 12:05:36 CET 2008


Author: arigo
Date: Wed Jan  2 12:05:35 2008
New Revision: 50253

Modified:
   pypy/branch/astcompilertests/pypy/interpreter/astcompiler/opt.py
   pypy/branch/astcompilertests/pypy/interpreter/astcompiler/test/test_compiler.py
Log:
not (x in y) ===> x not in y.

That's the last of the bytecode optimizations done by CPython 2.6 (svn),
excluding some optimizations that just remove dead opcodes.


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 12:05:35 2008
@@ -104,12 +104,19 @@
             return self._visitUnaryOp(node, _spacewrapper1('repr'))
         def visitInvert(self, node):
             return self._visitUnaryOp(node, _spacewrapper1('invert'))
-        def visitNot(self, node):
-            return self._visitUnaryOp(node, _spacewrapper1('not_'))
         def visitUnaryAdd(self, node):
             return self._visitUnaryOp(node, _spacewrapper1('pos'))
         def visitUnarySub(self, node):
             return self._visitUnaryOp(node, _spacewrapper1('neg'))
+        def visitNot(self, node):
+            expr = node.expr
+            if isinstance(expr, ast.Compare) and len(expr.ops) == 1:
+                op, subnode = expr.ops[0]
+                if op in opposite_cmp_op:
+                    # not (x in y) ===> x not in y   (resp: not in, is, etc.)
+                    expr.ops[0] = opposite_cmp_op[op], subnode
+                    return expr
+            return self._visitUnaryOp(node, _spacewrapper1('not_'))
 
         def _visitBinaryOp(self, node, constant_fold):
             left = node.left
@@ -274,6 +281,13 @@
         dst.filename = src.filename
         dst.parent = src.parent
 
+    opposite_cmp_op = {
+        'in'     : 'not in',
+        'not in' : 'in',
+        'is'     : 'is not',
+        'is not' : 'is',
+        }
+
 
     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 12:05:35 2008
@@ -218,6 +218,27 @@
         yield self.st, "n = None; x = n is True", "x", False
         yield self.st, "n = None; x = n is False", "x", False
         yield self.st, "n = None; x = n is None", "x", True
+        yield self.st, "t = True; x = t is not True", "x", False
+        yield self.st, "t = True; x = t is not False", "x", True
+        yield self.st, "t = True; x = t is not None", "x", True
+        yield self.st, "n = None; x = n is not True", "x", True
+        yield self.st, "n = None; x = n is not False", "x", True
+        yield self.st, "n = None; x = n is not None", "x", False
+
+        yield self.st, "x = not (3 in {3: 5})", "x", False
+        yield self.st, "x = not (3 not in {3: 5})", "x", True
+        yield self.st, "t = True; x = not (t is True)", "x", False
+        yield self.st, "t = True; x = not (t is False)", "x", True
+        yield self.st, "t = True; x = not (t is None)", "x", True
+        yield self.st, "n = None; x = not (n is True)", "x", True
+        yield self.st, "n = None; x = not (n is False)", "x", True
+        yield self.st, "n = None; x = not (n is None)", "x", False
+        yield self.st, "t = True; x = not (t is not True)", "x", True
+        yield self.st, "t = True; x = not (t is not False)", "x", False
+        yield self.st, "t = True; x = not (t is not None)", "x", False
+        yield self.st, "n = None; x = not (n is not True)", "x", False
+        yield self.st, "n = None; x = not (n is not False)", "x", False
+        yield self.st, "n = None; x = not (n is not None)", "x", True
 
     def test_multiexpr(self):
         yield self.st, "z = 2+3; x = y = z", "x,y,z", (5,5,5)



More information about the Pypy-commit mailing list