[pypy-commit] pypy translation-cleanup: Move FlowObjSpace monkey-patching code to p/o/f/objspace.py

rlamy noreply at buildbot.pypy.org
Thu Aug 30 18:38:20 CEST 2012


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: translation-cleanup
Changeset: r57000:5366c9d475f1
Date: 2012-08-14 02:18 +0100
http://bitbucket.org/pypy/pypy/changeset/5366c9d475f1/

Log:	Move FlowObjSpace monkey-patching code to p/o/f/objspace.py

	This puts all FlowObjSpace code in the same file and frees
	operation.py from flowspace-specific code.

diff --git a/pypy/objspace/flow/objspace.py b/pypy/objspace/flow/objspace.py
--- a/pypy/objspace/flow/objspace.py
+++ b/pypy/objspace/flow/objspace.py
@@ -490,4 +490,82 @@
         raise RuntimeError("the interpreter raises RuntimeError during "
                            "flow graph construction")
     w_RuntimeError = prebuilt_recursion_error = property(w_RuntimeError)
-operation.add_operations(FlowObjSpace)
+
+def make_op(name, arity):
+    """Add function operation to the flow space."""
+    if getattr(FlowObjSpace, name, None) is not None:
+        return
+
+    op = None
+    skip = False
+    arithmetic = False
+
+    if (name.startswith('del') or
+        name.startswith('set') or
+        name.startswith('inplace_')):
+        # skip potential mutators
+        skip = True
+    elif name in ('id', 'hash', 'iter', 'userdel'):
+        # skip potential runtime context dependecies
+        skip = True
+    elif name in ('repr', 'str'):
+        rep = getattr(__builtin__, name)
+        def op(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
+
+    if not op and not skip:
+        raise ValueError("XXX missing operator: %s" % (name,))
+
+    def generic_operator(self, *args_w):
+        assert len(args_w) == arity, name + " got the wrong number of arguments"
+        if op:
+            args = []
+            for w_arg in args_w:
+                try:
+                    arg = self.unwrap_for_computation(w_arg)
+                except UnwrapException:
+                    break
+                else:
+                    args.append(arg)
+            else:
+                # All arguments are constants: call the operator now
+                try:
+                    result = op(*args)
+                except Exception, e:
+                    etype = e.__class__
+                    msg = "generated by a constant operation:\n\t%s%r" % (
+                        name, tuple(args))
+                    raise operation.OperationThatShouldNotBePropagatedError(
+                        self.wrap(etype), self.wrap(msg))
+                else:
+                    # don't try to constant-fold operations giving a 'long'
+                    # result.  The result is probably meant to be sent to
+                    # an intmask(), but the 'long' constant confuses the
+                    # annotator a lot.
+                    if arithmetic and type(result) is long:
+                        pass
+                    # don't constant-fold getslice on lists, either
+                    elif name == 'getslice' and type(result) is list:
+                        pass
+                    # otherwise, fine
+                    else:
+                        try:
+                            return self.wrap(result)
+                        except WrapException:
+                            # type cannot sanely appear in flow graph,
+                            # store operation with variable result instead
+                            pass
+        w_result = self.do_operation_with_implicit_exceptions(name, *args_w)
+        return w_result
+
+    setattr(FlowObjSpace, name, generic_operator)
+
+
+for (name, symbol, arity, specialnames) in ObjSpace.MethodTable:
+    make_op(name, arity)
diff --git a/pypy/objspace/flow/operation.py b/pypy/objspace/flow/operation.py
--- a/pypy/objspace/flow/operation.py
+++ b/pypy/objspace/flow/operation.py
@@ -302,83 +302,3 @@
 _add_exceptions("""pow""",
                 OverflowError) # for the float case
 del _add_exceptions, _add_except_ovf
-
-def make_op(fs, name, symbol, arity, specialnames):
-    if getattr(fs, name, None) is not None:
-        return
-
-    op = None
-    skip = False
-    arithmetic = False
-
-    if (name.startswith('del') or
-        name.startswith('set') or
-        name.startswith('inplace_')):
-        # skip potential mutators
-        skip = True
-    elif name in ('id', 'hash', 'iter', 'userdel'):
-        # skip potential runtime context dependecies
-        skip = True
-    elif name in ('repr', 'str'):
-        rep = getattr(__builtin__, name)
-        def op(obj):
-            s = rep(obj)
-            if "at 0x" in s:
-                print >>sys.stderr, "Warning: captured address may be awkward"
-            return s
-    else:
-        op = FunctionByName[name]
-        arithmetic = (name + '_ovf') in FunctionByName
-
-    if not op and not skip:
-        raise ValueError("XXX missing operator: %s" % (name,))
-
-    def generic_operator(self, *args_w):
-        assert len(args_w) == arity, name + " got the wrong number of arguments"
-        if op:
-            args = []
-            for w_arg in args_w:
-                try:
-                    arg = self.unwrap_for_computation(w_arg)
-                except model.UnwrapException:
-                    break
-                else:
-                    args.append(arg)
-            else:
-                # All arguments are constants: call the operator now
-                try:
-                    result = op(*args)
-                except Exception, e:
-                    etype = e.__class__
-                    msg = "generated by a constant operation:\n\t%s%r" % (
-                        name, tuple(args))
-                    raise OperationThatShouldNotBePropagatedError(
-                        self.wrap(etype), self.wrap(msg))
-                else:
-                    # don't try to constant-fold operations giving a 'long'
-                    # result.  The result is probably meant to be sent to
-                    # an intmask(), but the 'long' constant confuses the
-                    # annotator a lot.
-                    if arithmetic and type(result) is long:
-                        pass
-                    # don't constant-fold getslice on lists, either
-                    elif name == 'getslice' and type(result) is list:
-                        pass
-                    # otherwise, fine
-                    else:
-                        try:
-                            return self.wrap(result)
-                        except model.WrapException:
-                            # type cannot sanely appear in flow graph,
-                            # store operation with variable result instead
-                            pass
-        w_result = self.do_operation_with_implicit_exceptions(name, *args_w)
-        return w_result
-
-    setattr(fs, name, generic_operator)
-
-
-def add_operations(fs):
-    """Add function operations to the flow space."""
-    for line in ObjSpace.MethodTable:
-        make_op(fs, *line)


More information about the pypy-commit mailing list