Commits

Ronan Lamy committed d79bc0f

Kill p.o.f.operation.special_overrides()

Define the methods directly inside FlowObjSpace instead.
In the case of getattr, this causes a bit of code duplication, but that's
better than crazy monkey-patching.

Comments (0)

Files changed (2)

pypy/objspace/flow/objspace.py

             return w_item
 
     def setitem(self, w_obj, w_key, w_val):
+        # protect us from globals write access
+        ec = self.getexecutioncontext()
+        if ec and w_obj is ec.w_globals:
+            raise SyntaxError("attempt to modify global attribute %r in %r"
+                            % (w_key, ec.graph.func))
         if self.concrete_mode:
             try:
                 obj = self.unwrap_for_computation(w_obj)
         return self.do_operation_with_implicit_exceptions('setitem', w_obj,
                                                           w_key, w_val)
 
+    def getattr(self, w_obj, w_name):
+        # handling special things like sys
+        # unfortunately this will never vanish with a unique import logic :-(
+        if w_obj in self.not_really_const:
+            const_w = self.not_really_const[w_obj]
+            if w_name not in const_w:
+                return self.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:
+            try:
+                result = getattr(obj, name)
+            except Exception, e:
+                etype = e.__class__
+                msg = "generated by a constant operation:\n\t%s%r" % (
+                    'getattr', (obj, name))
+                raise operation.OperationThatShouldNotBePropagatedError(
+                    self.wrap(etype), self.wrap(msg))
+            try:
+                return self.wrap(result)
+            except WrapException:
+                pass
+        return self.do_operation_with_implicit_exceptions('getattr',
+                w_obj, w_name)
+
     def call_function(self, w_func, *args_w):
         nargs = len(args_w)
         args = argument.ArgumentsForTranslation(self, list(args_w))

pypy/objspace/flow/operation.py

     setattr(fs, name, generic_operator)
 
 
-"""
-This is just a placeholder for some code I'm checking in elsewhere.
-It is provenly possible to determine constantness of certain expressions
-a little later. I introduced this a bit too early, together with tieing
-this to something being global, which was a bad idea.
-The concept is still valid, and it can  be used to force something to
-be evaluated immediately because it is supposed to be a constant.
-One good possible use of this is loop unrolling.
-This will be found in an 'experimental' folder with some use cases.
-"""
-
-def special_overrides(fs):
-    def getattr(self, w_obj, w_name):
-        # handling special things like sys
-        # unfortunately this will never vanish with a unique import logic :-(
-        if w_obj in self.not_really_const:
-            const_w = self.not_really_const[w_obj]
-            if w_name not in const_w:
-                return self.do_operation_with_implicit_exceptions('getattr',
-                                                                  w_obj, w_name)
-        return self.regular_getattr(w_obj, w_name)
-
-    fs.regular_getattr = fs.getattr
-    fs.getattr = getattr
-
-    # protect us from globals write access
-    def setitem(self, w_obj, w_key, w_val):
-        ec = self.getexecutioncontext()
-        if not (ec and w_obj is ec.w_globals):
-            return self.regular_setitem(w_obj, w_key, w_val)
-        raise SyntaxError("attempt to modify global attribute %r in %r"
-                          % (w_key, ec.graph.func))
-
-    fs.regular_setitem = fs.setitem
-    fs.setitem = setitem
-
-
 def add_operations(fs):
     """Add function operations to the flow space."""
     for line in ObjSpace.MethodTable:
         make_op(fs, *line)
-    special_overrides(fs)