[pypy-svn] r50696 - in pypy/dist/pypy/objspace: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Jan 17 11:03:42 CET 2008


Author: cfbolz
Date: Thu Jan 17 11:03:36 2008
New Revision: 50696

Modified:
   pypy/dist/pypy/objspace/reflective.py
   pypy/dist/pypy/objspace/test/test_reflective.py
Log:
do more sensible things with new* operations


Modified: pypy/dist/pypy/objspace/reflective.py
==============================================================================
--- pypy/dist/pypy/objspace/reflective.py	(original)
+++ pypy/dist/pypy/objspace/reflective.py	Thu Jan 17 11:03:36 2008
@@ -11,23 +11,65 @@
         ec.w_reflectivespace = w_reflectivespace
 app_set_reflectivespace = gateway.interp2app(set_reflectivespace)
 
+def get_reflective_space(space):
+    ec = space.getexecutioncontext()
+    if ec.w_reflectivespace is not None:
+        w_rspace = ec.w_reflectivespace
+        ec.w_reflectivespace = None
+        return w_rspace
+    return None
+
+def reset_reflective_space(space, w_rspace):
+    ec = space.getexecutioncontext()
+    ec.w_reflectivespace = w_rspace
+
 
 def proxymaker(space, opname, parentfn):
-    def fn(*args_w):
-        ec = space.getexecutioncontext()
-        if ec.w_reflectivespace is not None:
-            w_rspace = ec.w_reflectivespace
-            ec.w_reflectivespace = None
-            try:
-                w_f = space.getattr(w_rspace, space.wrap(opname))
-            except OperationError, e:
-                if not e.match(space, space.w_AttributeError):
-                    raise
-            else:
-                w_res = space.call_function(w_f, *args_w)
-                ec.w_reflectivespace = w_rspace
-                return w_res
-        return parentfn(*args_w)
+    if opname == "wrap":
+        return parentfn # no way to override wrapping for now
+    elif opname == "newdict": # grr grr kwargs
+        def fn(track_builtin_shadowing=False):
+            w_obj = parentfn(track_builtin_shadowing)
+            w_rspace = get_reflective_space(space)
+            if w_rspace is not None:
+                try:
+                    w_f = space.getattr(w_rspace, space.wrap("newdict"))
+                except OperationError, e:
+                    if not e.match(space, space.w_AttributeError):
+                        raise
+                else:
+                    w_obj = space.call_function(w_f, w_obj)
+                    reset_reflective_space(space, w_rspace)
+            return w_obj
+    elif opname.startswith("new"):
+        def fn(*args_w, **kwargs):
+            w_obj = parentfn(*args_w, **kwargs)
+            w_rspace = get_reflective_space(space)
+            if w_rspace is not None:
+                try:
+                    w_f = space.getattr(w_rspace, space.wrap(opname))
+                except OperationError, e:
+                    if not e.match(space, space.w_AttributeError):
+                        raise
+                else:
+                    w_obj = space.call_function(w_f, w_obj)
+                    reset_reflective_space(space, w_rspace)
+            return w_obj
+    else:
+        def fn(*args_w):
+            ec = space.getexecutioncontext()
+            w_rspace = get_reflective_space(space)
+            if w_rspace is not None:
+                try:
+                    w_f = space.getattr(w_rspace, space.wrap(opname))
+                except OperationError, e:
+                    if not e.match(space, space.w_AttributeError):
+                        raise
+                else:
+                    w_res = space.call_function(w_f, *args_w)
+                    reset_reflective_space(space, w_rspace)
+                    return w_res
+            return parentfn(*args_w)
     fn.func_name = opname
     return fn
 

Modified: pypy/dist/pypy/objspace/test/test_reflective.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_reflective.py	(original)
+++ pypy/dist/pypy/objspace/test/test_reflective.py	Thu Jan 17 11:03:36 2008
@@ -12,7 +12,9 @@
                 return 40+2
 
         set_reflectivespace(Space())
-        assert 1+2 == 42
+        x = 1
+        y = 2
+        assert x + y == 42
 
         set_reflectivespace(None)
         assert 1+2 == 3
@@ -24,3 +26,28 @@
 
         set_reflectivespace(Space())
         assert 1+2 == 3
+
+    def test_newdict(self):
+        from __pypy__ import set_reflectivespace
+        class Space:
+            def newdict(self, d):
+                d['surprise'] = 42
+                return d
+
+        set_reflectivespace(Space())
+        d = {"b": 1}
+        assert d["surprise"] == 42
+        set_reflectivespace(None)
+
+
+    def test_newlist(self):
+        from __pypy__ import set_reflectivespace
+        class Space:
+            def newlist(self, l):
+                l.append(len(l))
+                return l
+
+        set_reflectivespace(Space())
+        l = [1, 2, 3, 4, 5]
+        assert len(l) == 6
+        set_reflectivespace(None)



More information about the Pypy-commit mailing list