[pypy-svn] r66422 - in pypy/branch/parser-compiler/pypy: interpreter/astcompiler module/parser

benjamin at codespeak.net benjamin at codespeak.net
Mon Jul 20 01:25:53 CEST 2009


Author: benjamin
Date: Mon Jul 20 01:25:52 2009
New Revision: 66422

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py
   pypy/branch/parser-compiler/pypy/module/parser/pyparser.py
Log:
make keyword args consistent with cpython

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	Mon Jul 20 01:25:52 2009
@@ -14,38 +14,38 @@
     return TopLevelCodeGenerator(space, module, symbols, info).assemble()
 
 
-name_ops_default = {
+name_ops_default = misc.dict_to_switch({
     ast.Load : ops.LOAD_NAME,
     ast.Store : ops.STORE_NAME,
     ast.Del : ops.DELETE_NAME
-}
+})
 
-name_ops_fast = {
+name_ops_fast = misc.dict_to_switch({
     ast.Load : ops.LOAD_FAST,
     ast.Store : ops.STORE_FAST,
     ast.Del : ops.DELETE_FAST
-}
+})
 
-name_ops_deref = {
+name_ops_deref = misc.dict_to_switch({
     ast.Load : ops.LOAD_DEREF,
     ast.Store : ops.STORE_DEREF,
-}
+})
 
-name_ops_global = {
+name_ops_global = misc.dict_to_switch({
     ast.Load : ops.LOAD_GLOBAL,
     ast.Store : ops.STORE_GLOBAL,
     ast.Del : ops.DELETE_GLOBAL
-}
+})
 
 
-unary_operations = {
+unary_operations = misc.dict_to_switch({
     ast.Invert : ops.UNARY_INVERT,
     ast.Not : ops.UNARY_NOT,
     ast.UAdd : ops.UNARY_POSITIVE,
     ast.USub : ops.UNARY_NEGATIVE
-}
+})
 
-binary_operations = {
+binary_operations = misc.dict_to_switch({
     ast.Add : ops.BINARY_ADD,
     ast.Sub : ops.BINARY_SUBTRACT,
     ast.Mult : ops.BINARY_MULTIPLY,
@@ -57,9 +57,9 @@
     ast.BitAnd : ops.BINARY_AND,
     ast.BitXor : ops.BINARY_XOR,
     ast.FloorDiv : ops.BINARY_FLOOR_DIVIDE
-}
+})
 
-inplace_operations = {
+inplace_operations = misc.dict_to_switch({
     ast.Add : ops.INPLACE_ADD,
     ast.Sub : ops.INPLACE_SUBTRACT,
     ast.Mult : ops.INPLACE_MULTIPLY,
@@ -71,9 +71,9 @@
     ast.BitAnd : ops.INPLACE_AND,
     ast.BitXor : ops.INPLACE_XOR,
     ast.FloorDiv : ops.INPLACE_FLOOR_DIVIDE
-}
+})
 
-compare_operations = {
+compare_operations = misc.dict_to_switch({
     ast.Eq : 2,
     ast.NotEq : 3,
     ast.Lt : 0,
@@ -84,23 +84,23 @@
     ast.NotIn : 7,
     ast.Is : 8,
     ast.IsNot : 9
-}
+})
 
-subscr_operations = {
+subscr_operations = misc.dict_to_switch({
     ast.AugLoad : ops.BINARY_SUBSCR,
     ast.Load : ops.BINARY_SUBSCR,
     ast.AugStore : ops.STORE_SUBSCR,
     ast.Store : ops.STORE_SUBSCR,
     ast.Del : ops.DELETE_SUBSCR
-}
+})
 
-slice_operations = {
+slice_operations = misc.dict_to_switch({
     ast.AugLoad : ops.SLICE,
     ast.Load : ops.SLICE,
     ast.AugStore : ops.STORE_SLICE,
     ast.Store : ops.STORE_SLICE,
     ast.Del : ops.DELETE_SLICE
-}
+})
 
 
 F_BLOCK_LOOP = 0
@@ -168,7 +168,7 @@
         elif scope == symtable.SCOPE_GLOBAL_EXPLICIT:
             kind = name_ops_global
         try:
-            op = kind[ctx]
+            op = kind(ctx)
         except KeyError:
             if kind is name_ops_deref and ctx == ast.Del:
                 raise SyntaxError("Can't delete variable used in "
@@ -275,7 +275,7 @@
                 return ops.INPLACE_TRUE_DIVIDE
             else:
                 return ops.INPLACE_DIVIDE
-        return inplace_operations[op]
+        return inplace_operations(op)
 
     def visit_AugAssign(self, assign):
         self.update_position(assign.lineno)
@@ -325,7 +325,7 @@
                 return ops.BINARY_TRUE_DIVIDE
             else:
                 return ops.BINARY_DIVIDE
-        return binary_operations[op]
+        return binary_operations(op)
 
     def visit_BinOp(self, binop):
         self.update_position(binop.lineno)
@@ -716,7 +716,7 @@
     def visit_UnaryOp(self, op):
         self.update_position(op.lineno)
         op.operand.walkabout(self)
-        self.emit_op(unary_operations[op.op])
+        self.emit_op(unary_operations(op.op))
 
     def visit_BoolOp(self, op):
         self.update_position(op.lineno)
@@ -743,14 +743,14 @@
         for i in range(1, ops_count):
             self.emit_op(ops.DUP_TOP)
             self.emit_op(ops.ROT_THREE)
-            op_kind = compare_operations[comp.ops[i - 1]]
+            op_kind = compare_operations(comp.ops[i - 1])
             self.emit_op_arg(ops.COMPARE_OP, op_kind)
             self.emit_jump(ops.JUMP_IF_FALSE, cleanup)
             self.emit_op(ops.POP_TOP)
             if i < (ops_count - 1):
                 comp.comparators[i].walkabout(self)
         comp.comparators[-1].walkabout(self)
-        last_kind = compare_operations[comp.ops[-1]]
+        last_kind = compare_operations(comp.ops[-1])
         self.emit_op_arg(ops.COMPARE_OP, last_kind)
         if ops_count > 1:
             end = self.new_block()
@@ -1047,7 +1047,7 @@
                 self.emit_op(ops.ROT_THREE)
             elif stack_count == 2:
                 self.emit_op(ops.ROT_FOUR)
-        self.emit_op(slice_operations[ctx] + slice_offset)
+        self.emit_op(slice_operations(ctx) + slice_offset)
 
     def _complex_slice(self, slc, ctx):
         if slc.lower:
@@ -1102,7 +1102,7 @@
             self.emit_op_arg(ops.DUP_TOPX, 2)
         elif ctx == ast.AugStore:
             self.emit_op(ops.ROT_THREE)
-        self.emit_op(subscr_operations[ctx])
+        self.emit_op(subscr_operations(ctx))
 
     def visit_Subscript(self, sub):
         self.update_position(sub.lineno)

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py	Mon Jul 20 01:25:52 2009
@@ -1,5 +1,7 @@
 from pypy.interpreter import gateway
 from pypy.interpreter.astcompiler import ast2 as ast
+from pypy.rlib.objectmodel import we_are_translated
+from pypy.rlib.unroll import unrolling_iterable
 
 
 app = gateway.applevel("""
@@ -38,6 +40,21 @@
     return False
 
 
+def dict_to_switch(d):
+    def lookup(query):
+        if we_are_translated():
+            for key, value in unrolling_iteritems:
+                if key == query:
+                    return value
+            else:
+                raise KeyError
+        else:
+            return d[query]
+    lookup._always_inline_ = True
+    unrolling_iteritems = unrolling_iterable(d.iteritems())
+    return lookup
+
+
 def flatten(tup):
     elts = []
     for elt in tup:

Modified: pypy/branch/parser-compiler/pypy/module/parser/pyparser.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/module/parser/pyparser.py	(original)
+++ pypy/branch/parser-compiler/pypy/module/parser/pyparser.py	Mon Jul 20 01:25:52 2009
@@ -42,14 +42,14 @@
         return space.wrap(self.tree.type == pygram.syms.eval_input)
     descr_isexpr.unwrap_spec = ["self", ObjSpace]
 
-    def descr_totuple(self, space, with_lineno=False, with_offset=False):
+    def descr_totuple(self, space, line_info=False, col_info=False):
         return self._build_app_tree(space, self.tree, space.newtuple,
-                                    with_lineno, with_offset)
+                                    line_info, col_info)
     descr_totuple.unwrap_spec = ["self", ObjSpace, bool, bool]
 
-    def descr_tolist(self, space, with_lineno=False, with_offset=False):
+    def descr_tolist(self, space, line_info=False, col_info=False):
         return self._build_app_tree(space, self.tree, space.newlist,
-                                    with_lineno, with_offset)
+                                    line_info, col_info)
     descr_tolist.unwrap_spec = ["self", ObjSpace, bool, bool]
 
     def descr_compile(self, space, filename="<syntax-tree>"):



More information about the Pypy-commit mailing list