Commits

Ronan Lamy committed 697a17c

Turn make_sc() into a method of SpaceOperator

Comments (0)

Files changed (2)

rpython/flowspace/operation.py

 import operator
 from rpython.tool.sourcetools import compile2
 from rpython.rlib.rarithmetic import ovfcheck
+from rpython.flowspace.model import Constant
 
 class _OpHolder(object): pass
 op = _OpHolder()
         self.pure = pure
         self.can_overflow = can_overflow
 
+    def make_sc(self):
+        def sc_operator(space, args_w):
+            if len(args_w) != self.arity:
+                if self is op.pow and len(args_w) == 2:
+                    args_w = args_w + [Constant(None)]
+                elif self is op.getattr and len(args_w) == 3:
+                    return space.frame.do_operation('simple_call', Constant(getattr), *args_w)
+                else:
+                    raise Exception("should call %r with exactly %d arguments" % (
+                        self.name, self.arity))
+            # completely replace the call with the underlying
+            # operation and its limited implicit exceptions semantic
+            return getattr(space, self.name)(*args_w)
+        return sc_operator
+
+
 def add_operator(name, arity, symbol, pyfunc=None, pure=False, ovf=False):
     operator_func = getattr(operator, name, None)
     oper = SpaceOperator(name, arity, symbol, pyfunc, pure, can_overflow=ovf)

rpython/flowspace/specialcase.py

     args = [space.unwrap(arg) for arg in args_w]
     return space.import_name(*args)
 
-def make_sc(oper):
-    def sc_operator(space, args_w):
-        if len(args_w) != oper.arity:
-            if oper is op.pow and len(args_w) == 2:
-                args_w = args_w + [Constant(None)]
-            elif oper is op.getattr and len(args_w) == 3:
-                return space.frame.do_operation('simple_call', Constant(getattr), *args_w)
-            else:
-                raise Exception("should call %r with exactly %d arguments" % (
-                    oper.name, oper.arity))
-        # completely replace the call with the underlying
-        # operation and its limited implicit exceptions semantic
-        return getattr(space, oper.name)(*args_w)
-    return sc_operator
-
 # _________________________________________________________________________
 # a simplified version of the basic printing routines, for RPython programs
 class StdOutBuffer:
         we_are_translated: sc_we_are_translated,
         locals: sc_locals}
 for fn, oper in func2op.items():
-    SPECIAL_CASES[fn] = make_sc(oper)
+    SPECIAL_CASES[fn] = oper.make_sc()