[pypy-svn] r66569 - pypy/branch/parser-compiler/pypy/interpreter/astcompiler

benjamin at codespeak.net benjamin at codespeak.net
Fri Jul 24 03:31:54 CEST 2009


Author: benjamin
Date: Fri Jul 24 03:31:54 2009
New Revision: 66569

Added:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/asthelpers.py   (contents, props changed)
Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
Log:
move a few global helpers to methods of the ast

Added: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/asthelpers.py
==============================================================================
--- (empty file)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/asthelpers.py	Fri Jul 24 03:31:54 2009
@@ -0,0 +1,46 @@
+from pypy.interpreter.astcompiler import ast2 as ast
+from pypy.interpreter.error import OperationError
+
+
+class __extend__(ast.expr):
+
+    constant = False
+
+    def as_node_list(self, space):
+        return None
+
+
+class __extend__(ast.List):
+
+    def as_node_list(self, space):
+        return self.elts
+
+
+class __extend__(ast.Tuple):
+
+    def as_list_w(self, space):
+        return self.elts
+
+
+class __extend__(ast.Const):
+
+    constant = True
+
+    def as_node_list(self, space):
+        try:
+            values_w = space.unpackiterable(self.value)
+        except OperationError:
+            return None
+        line = self.lineno
+        column = self.col_offset
+        return [ast.Const(w_obj, line, column) for w_obj in values_w]
+
+
+class __extend__(ast.Str):
+
+    constant = True
+
+
+class __extend__(ast.Num):
+
+    constant = True

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	Fri Jul 24 03:31:54 2009
@@ -4,7 +4,7 @@
 
 from pypy.interpreter.astcompiler import (ast2 as ast, assemble, symtable,
                                           consts, misc)
-from pypy.interpreter.astcompiler import optimize # For side effects
+from pypy.interpreter.astcompiler import optimize, asthelpers # For side effects
 from pypy.interpreter.pyparser.error import SyntaxError
 from pypy.tool import stdlib_opcode as ops
 from pypy.interpreter.pyparser import future
@@ -632,10 +632,10 @@
     def _optimize_unpacking(self, assign):
         if len(assign.targets) != 1:
             return False
-        targets = self._list_from_sequence_node(assign.targets[0], False)
+        targets = assign.targets[0].as_node_list(self.space)
         if targets is None:
             return False
-        values = self._list_from_sequence_node(assign.value, True)
+        values = assign.value.as_node_list(self.space)
         if values is None:
             return False
         targets_count = len(targets)
@@ -668,23 +668,6 @@
         self.visit_sequence(targets)
         return True
 
-    def _list_from_sequence_node(self, node, const_possible):
-        if isinstance(node, ast.Tuple):
-            nodes = node.elts
-        elif isinstance(node, ast.List):
-            nodes = node.elts
-        elif const_possible and isinstance(node, ast.Const):
-            try:
-                values_w = self.space.unpackiterable(node.value)
-            except OperationError:
-                return None
-            line = node.lineno
-            column = node.col_offset
-            nodes = [ast.Const(obj, line, column) for obj in values_w]
-        else:
-            nodes = None
-        return nodes
-
     def visit_With(self, wih):
         self.update_position(wih.lineno, True)
         body_block = self.new_block()
@@ -762,8 +745,7 @@
         if self.interactive:
             expr.value.walkabout(self)
             self.emit_op(ops.PRINT_EXPR)
-        elif not isinstance(expr.value, ast.Num) and \
-                not isinstance(expr.value, ast.Str):
+        elif not expr.value.constant:
             expr.value.walkabout(self)
             self.emit_op(ops.POP_TOP)
 



More information about the Pypy-commit mailing list