[pypy-svn] r50655 - in pypy/dist/pypy: config objspace objspace/test

antocuni at codespeak.net antocuni at codespeak.net
Wed Jan 16 00:03:21 CET 2008


Author: antocuni
Date: Wed Jan 16 00:03:21 2008
New Revision: 50655

Added:
   pypy/dist/pypy/objspace/reflective.py   (contents, props changed)
   pypy/dist/pypy/objspace/test/test_reflective.py   (contents, props changed)
Modified:
   pypy/dist/pypy/config/pypyoption.py
Log:
(cfbolz, antocuni)

a completely experimental object space that allows to override space
operations from applevel code. Reflection++



Modified: pypy/dist/pypy/config/pypyoption.py
==============================================================================
--- pypy/dist/pypy/config/pypyoption.py	(original)
+++ pypy/dist/pypy/config/pypyoption.py	Wed Jan 16 00:03:21 2008
@@ -41,7 +41,7 @@
 
 pypy_optiondescription = OptionDescription("objspace", "Object Space Options", [
     ChoiceOption("name", "Object Space name",
-                 ["std", "flow", "thunk", "dump", "taint"],
+                 ["std", "flow", "thunk", "dump", "taint", "reflective"],
                  "std",
                  cmdline='--objspace -o'),
 

Added: pypy/dist/pypy/objspace/reflective.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/reflective.py	Wed Jan 16 00:03:21 2008
@@ -0,0 +1,51 @@
+from pypy.interpreter import gateway
+from pypy.interpreter.error import OperationError
+from pypy.objspace import std
+from pypy.objspace.proxy import patch_space_in_place
+
+def set_reflectivespace(space, w_reflectivespace):
+    ec = space.getexecutioncontext()
+    if space.is_w(ec.w_reflectivespace, space.w_None):
+        ec.w_reflectivespace = None
+    else:
+        ec.w_reflectivespace = w_reflectivespace
+app_set_reflectivespace = gateway.interp2app(set_reflectivespace)
+
+
+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)
+    fn.func_name = opname
+    return fn
+
+def createexecutioncontextmaker(space, parentfn):
+    def createexecutioncontext():
+        ec = parentfn()
+        ec.w_reflectivespace = None
+        return ec
+    return createexecutioncontext
+
+def Space(*args, **kwds):
+    space = std.Space(*args, **kwds)
+    space.createexecutioncontext = createexecutioncontextmaker(
+        space, space.createexecutioncontext)
+    space.getexecutioncontext().w_reflectivespace = None # patch the already built ec
+    patch_space_in_place(space, 'reflective', proxymaker)
+    w___pypy__ = space.getbuiltinmodule("__pypy__")
+    space.setattr(w___pypy__, space.wrap('set_reflectivespace'),
+                  space.wrap(app_set_reflectivespace))
+    return space
+

Added: pypy/dist/pypy/objspace/test/test_reflective.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/test/test_reflective.py	Wed Jan 16 00:03:21 2008
@@ -0,0 +1,26 @@
+from pypy.conftest import gettestobjspace
+
+class AppTest_Reflective:
+
+    def setup_class(cls):
+        cls.space = gettestobjspace('reflective')
+
+    def test_add(self):
+        from __pypy__ import set_reflectivespace
+        class Space:
+            def add(self, x, y):
+                return 40+2
+
+        set_reflectivespace(Space())
+        assert 1+2 == 42
+
+        set_reflectivespace(None)
+        assert 1+2 == 3
+        
+    def test_default_behaviour(self):
+        from __pypy__ import set_reflectivespace
+        class Space:
+            pass
+
+        set_reflectivespace(Space())
+        assert 1+2 == 3



More information about the Pypy-commit mailing list