Author: Ronan Lamy <[email protected]>
Branch: Opcode-class
Changeset: r63825:75c10594741f
Date: 2013-05-01 16:49 +0100
http://bitbucket.org/pypy/pypy/changeset/75c10594741f/

Log:    Replace unwrap_for_computation() with Constant.foldable()

diff --git a/rpython/flowspace/model.py b/rpython/flowspace/model.py
--- a/rpython/flowspace/model.py
+++ b/rpython/flowspace/model.py
@@ -3,6 +3,7 @@
 #
 # the below object/attribute model evolved from
 # a discussion in Berlin, 4th of october 2003
+import types
 import py
 
 from rpython.tool.uid import uid, Hashable
@@ -261,6 +262,7 @@
     dummyname = 'v'
     namesdict = {dummyname : (dummyname, 0)}
 
+    @property
     def name(self):
         _name = self._name
         _nr = self._nr
@@ -270,11 +272,10 @@
             _nr = self._nr = nd[_name][1]
             nd[_name] = (_name, _nr + 1)
         return "%s%d" % (_name, _nr)
-    name = property(name)
 
+    @property
     def renamed(self):
         return self._name is not self.dummyname
-    renamed = property(renamed)
 
     def __init__(self, name=None):
         self._name = self.dummyname
@@ -314,6 +315,9 @@
         self._name = intern(name)
         self._nr = nr
 
+    def foldable(self):
+        return False
+
 
 class Constant(Hashable):
     __slots__ = ["concretetype"]
@@ -323,6 +327,25 @@
         if concretetype is not None:
             self.concretetype = concretetype
 
+    def foldable(self):
+        to_check = self.value
+        if hasattr(to_check, 'im_self'):
+            to_check = to_check.im_self
+        if isinstance(to_check, (type, types.ClassType, types.ModuleType)):
+            # classes/types/modules are assumed immutable
+            return True
+        if (hasattr(to_check, '__class__') and
+                to_check.__class__.__module__ == '__builtin__'):
+            # builtin object
+            return True
+        # User-created instance
+        if hasattr(to_check, '_freeze_'):
+            assert to_check._freeze_() is True
+            return True
+        else:
+            # cannot count on it not mutating at runtime!
+            return False
+
 
 class UnwrapException(Exception):
     """Attempted to unwrap a Variable."""
diff --git a/rpython/flowspace/objspace.py b/rpython/flowspace/objspace.py
--- a/rpython/flowspace/objspace.py
+++ b/rpython/flowspace/objspace.py
@@ -176,22 +176,6 @@
         else:
             raise TypeError("not wrapped: " + repr(w_obj))
 
-    def unwrap_for_computation(self, w_obj):
-        obj = self.unwrap(w_obj)
-        to_check = obj
-        if hasattr(to_check, 'im_self'):
-            to_check = to_check.im_self
-        if (not isinstance(to_check, (type, types.ClassType, 
types.ModuleType)) and
-            # classes/types/modules are assumed immutable
-            hasattr(to_check, '__class__') and to_check.__class__.__module__ 
!= '__builtin__'):
-            frozen = hasattr(to_check, '_freeze_')
-            if frozen:
-                assert to_check._freeze_() is True
-            else:
-                # cannot count on it not mutating at runtime!
-                raise UnwrapException
-        return obj
-
     def exception_issubclass_w(self, w_cls1, w_cls2):
         return self.is_true(self.issubtype(w_cls1, w_cls2))
 
@@ -291,12 +275,8 @@
         return self.wrap(not self.is_true(w_obj))
 
     def is_true(self, w_obj):
-        try:
-            obj = self.unwrap_for_computation(w_obj)
-        except UnwrapException:
-            pass
-        else:
-            return bool(obj)
+        if w_obj.foldable():
+            return bool(w_obj.value)
         w_truthvalue = self.frame.do_operation('is_true', w_obj)
         return self.frame.guessbool(w_truthvalue)
 
@@ -343,12 +323,8 @@
             if w_name not in const_w:
                 return 
self.frame.do_operation_with_implicit_exceptions('getattr',
                                                                 w_obj, w_name)
-        try:
-            obj = self.unwrap_for_computation(w_obj)
-            name = self.unwrap_for_computation(w_name)
-        except UnwrapException:
-            pass
-        else:
+        if w_obj.foldable() and w_name.foldable():
+            obj, name = w_obj.value, w_name.value
             try:
                 result = getattr(obj, name)
             except Exception, e:
@@ -495,14 +471,8 @@
     def generic_operator(self, *args_w):
         assert len(args_w) == arity, name + " got the wrong number of 
arguments"
         args = []
-        for w_arg in args_w:
-            try:
-                arg = self.unwrap_for_computation(w_arg)
-            except UnwrapException:
-                break
-            else:
-                args.append(arg)
-        else:
+        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)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to