Commits

Ronan Lamy committed 75c1059

Replace unwrap_for_computation() with Constant.foldable()

Comments (0)

Files changed (2)

rpython/flowspace/model.py

 #
 # 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
     dummyname = 'v'
     namesdict = {dummyname : (dummyname, 0)}
 
+    @property
     def name(self):
         _name = self._name
         _nr = self._nr
             _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
         self._name = intern(name)
         self._nr = nr
 
+    def foldable(self):
+        return False
+
 
 class Constant(Hashable):
     __slots__ = ["concretetype"]
         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."""

rpython/flowspace/objspace.py

         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))
 
         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)
 
             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:
     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)