[pypy-commit] pypy flowoperators: Kill operation.FunctionByName

rlamy noreply at buildbot.pypy.org
Fri Jul 5 19:42:17 CEST 2013


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: flowoperators
Changeset: r65218:560b115996b4
Date: 2013-05-05 17:26 +0100
http://bitbucket.org/pypy/pypy/changeset/560b115996b4/

Log:	Kill operation.FunctionByName

diff --git a/rpython/flowspace/objspace.py b/rpython/flowspace/objspace.py
--- a/rpython/flowspace/objspace.py
+++ b/rpython/flowspace/objspace.py
@@ -412,44 +412,46 @@
                 raise FlowingError(self.frame, self.wrap(message))
         return self.wrap(value)
 
-def make_impure_op(name, arity):
+def make_impure_op(oper):
     def generic_operator(self, *args_w):
-        assert len(args_w) == arity, name + " got the wrong number of arguments"
-        w_result = self.frame.do_operation_with_implicit_exceptions(name, *args_w)
+        if len(args_w) != oper.arity:
+            raise TypeError(oper.name + " got the wrong number of arguments")
+        w_result = self.frame.do_operation_with_implicit_exceptions(oper.name, *args_w)
         return w_result
     return generic_operator
 
-def make_op(name, arity):
+def make_op(oper):
     """Add function operation to the flow space."""
     op = None
     skip = False
     arithmetic = False
+    name = oper.name
 
     if (name.startswith('del') or
         name.startswith('set') or
         name.startswith('inplace_')):
-        return make_impure_op(name, arity)
+        return make_impure_op(oper)
     elif name in ('id', 'hash', 'iter', 'userdel'):
-        return make_impure_op(name, arity)
+        return make_impure_op(oper)
     elif name in ('repr', 'str'):
         rep = getattr(__builtin__, name)
-        def op(obj):
+        def func(obj):
             s = rep(obj)
             if "at 0x" in s:
                 print >>sys.stderr, "Warning: captured address may be awkward"
             return s
     else:
-        op = operation.FunctionByName[name]
-        arithmetic = (name + '_ovf') in operation.FunctionByName
+        func = oper.pyfunc
+        arithmetic = hasattr(operation.op, name + '_ovf')
 
     def generic_operator(self, *args_w):
-        assert len(args_w) == arity, name + " got the wrong number of arguments"
+        assert len(args_w) == oper.arity, name + " got the wrong number of arguments"
         args = []
         if all(w_arg.foldable() for w_arg in args_w):
             args = [w_arg.value for w_arg in args_w]
             # All arguments are constants: call the operator now
             try:
-                result = op(*args)
+                result = func(*args)
             except Exception, e:
                 etype = e.__class__
                 msg = "%s%r always raises %s: %s" % (
@@ -479,7 +481,7 @@
 
 for oper in operation.op.__dict__.values():
     if getattr(FlowObjSpace, oper.name, None) is None:
-        setattr(FlowObjSpace, oper.name, make_op(oper.name, oper.arity))
+        setattr(FlowObjSpace, oper.name, make_op(oper))
 
 
 def build_flow(func, space=FlowObjSpace()):
diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -13,7 +13,6 @@
 op = _OpHolder()
 
 func2op = {}
-FunctionByName = {}   # dict {"operation_name": <built-in function>}
 
 class SpaceOperator(object):
     def __init__(self, name, arity, symbol, pyfunc):
@@ -27,13 +26,11 @@
     oper = SpaceOperator(name, arity, symbol, pyfunc)
     setattr(op, name, oper)
     if pyfunc is not None:
-        FunctionByName[name] = pyfunc
         func2op[pyfunc] = oper
     if operator_func:
         func2op[operator_func] = oper
         if pyfunc is None:
             oper.pyfunc = operator_func
-            FunctionByName[name] = operator_func
 
 # ____________________________________________________________
 
@@ -189,6 +186,7 @@
 add_operator('pos', 1, 'pos')
 add_operator('neg', 1, 'neg')
 add_operator('nonzero', 1, 'truth', pyfunc=bool)
+op.is_true = op.nonzero
 add_operator('abs' , 1, 'abs', pyfunc=abs)
 add_operator('hex', 1, 'hex', pyfunc=hex)
 add_operator('oct', 1, 'oct', pyfunc=oct)
@@ -249,8 +247,6 @@
     ovf_func = lambda *args: ovfcheck(oper.pyfunc(*args))
     add_operator(oper.name + '_ovf', oper.arity, oper.symbol, pyfunc=ovf_func)
 
-FunctionByName['is_true'] = bool
-
 # Other functions that get directly translated to SpaceOperators
 func2op[type] = op.type
 func2op[operator.truth] = op.nonzero
diff --git a/rpython/rtyper/lltypesystem/opimpl.py b/rpython/rtyper/lltypesystem/opimpl.py
--- a/rpython/rtyper/lltypesystem/opimpl.py
+++ b/rpython/rtyper/lltypesystem/opimpl.py
@@ -1,4 +1,4 @@
-from rpython.flowspace.operation import FunctionByName
+from rpython.flowspace.operation import op
 from rpython.rlib import debug
 from rpython.rlib.rarithmetic import is_valid_int
 from rpython.rtyper.lltypesystem import lltype, llmemory
@@ -13,7 +13,6 @@
                         'lt': True, 'le': True,
                         'eq': True, 'ne': True,
                         'is_true': True}
-ops_unary = {'is_true': True, 'neg': True, 'abs': True, 'invert': True}
 
 # global synonyms for some types
 from rpython.rlib.rarithmetic import intmask
@@ -46,11 +45,13 @@
 def get_primitive_op_src(fullopname):
     assert '_' in fullopname, "%s: not a primitive op" % (fullopname,)
     typname, opname = fullopname.split('_', 1)
-    if opname not in FunctionByName and (opname + '_') in FunctionByName:
-        func = FunctionByName[opname + '_']   # or_, and_
+    if hasattr(op, opname):
+        oper = getattr(op, opname)
+    elif hasattr(op, opname + '_'):
+        oper = getattr(op, opname + '_')   # or_, and_
     else:
-        assert opname in FunctionByName, "%s: not a primitive op" % (fullopname,)
-        func = FunctionByName[opname]
+        raise ValueError("%s: not a primitive op" % (fullopname,))
+    func = oper.pyfunc
 
     if typname == 'char':
         # char_lt, char_eq, ...
@@ -72,7 +73,7 @@
             fullopname,)
         argtype = argtype_by_name[typname]
 
-        if opname in ops_unary:
+        if oper.arity == 1:
             def op_function(x):
                 if not isinstance(x, argtype):
                     raise TypeError("%r arg must be %s, got %r instead" % (


More information about the pypy-commit mailing list