[pypy-svn] r49639 - in pypy/dist/pypy: module/_file objspace/std objspace/std/test

fijal at codespeak.net fijal at codespeak.net
Tue Dec 11 19:11:53 CET 2007


Author: fijal
Date: Tue Dec 11 19:11:50 2007
New Revision: 49639

Modified:
   pypy/dist/pypy/module/_file/__init__.py
   pypy/dist/pypy/objspace/std/test/test_proxy_internals.py
   pypy/dist/pypy/objspace/std/test/test_proxy_usercreated.py
   pypy/dist/pypy/objspace/std/transparent.py
Log:
Fix tproxy for files. Seems to be a bit too hand-written to me, but we can
attach a gateway magic (I hope)


Modified: pypy/dist/pypy/module/_file/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_file/__init__.py	(original)
+++ pypy/dist/pypy/module/_file/__init__.py	Tue Dec 11 19:11:50 2007
@@ -22,3 +22,8 @@
                     pass    # key was removed in the meantime
                 else:
                     stream.flush()
+
+    def setup_after_space_initialization(self):
+        from pypy.module._file.interp_file import W_File
+        from pypy.objspace.std.transparent import register_proxyable
+        register_proxyable(self.space, W_File)

Modified: pypy/dist/pypy/objspace/std/test/test_proxy_internals.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_proxy_internals.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_proxy_internals.py	Tue Dec 11 19:11:50 2007
@@ -123,7 +123,6 @@
         assert get_tproxy_controller(lst) is f
 
     def test_proxy_file(self):
-        skip("Not working")
         from __pypy__ import tproxy
 
         def f(name, *args, **kwds):

Modified: pypy/dist/pypy/objspace/std/test/test_proxy_usercreated.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_proxy_usercreated.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_proxy_usercreated.py	Tue Dec 11 19:11:50 2007
@@ -3,21 +3,34 @@
 from pypy.objspace.std.test.test_proxy_internals import AppProxy
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.gateway import interp2app
+from pypy.objspace.std.transparent import register_proxyable
 
 class W_Wrapped(Wrappable):
     def new(space, w_type):
         return space.wrap(W_Wrapped())
 
+    def name(self, space):
+        return space.wrap("wrapped")
+    name.unwrap_spec = ['self', ObjSpace]
+
 W_Wrapped.typedef = TypeDef(
     'Wrapped',
-    __new__ = interp2app(W_Wrapped.new.im_func)
+    __new__ = interp2app(W_Wrapped.new.im_func),
+    __name__ = interp2app(W_Wrapped.name),
 )
 
 class AppTestProxyNewtype(AppProxy):
     def setup_class(cls):
         AppProxy.setup_class.im_func(cls)
         cls.w_wrapped = cls.space.wrap(W_Wrapped())
+        register_proxyable(cls.space, W_Wrapped)
         
     def test_one(self):
         x = type(self.wrapped)()
-        print x
+        from __pypy__ import tproxy
+
+        def f(name, *args, **kwds):
+            return getattr(x, name)(*args, **kwds)
+
+        t = tproxy(type(x), f)
+        assert t.__name__ == x.__name__

Modified: pypy/dist/pypy/objspace/std/transparent.py
==============================================================================
--- pypy/dist/pypy/objspace/std/transparent.py	(original)
+++ pypy/dist/pypy/objspace/std/transparent.py	Tue Dec 11 19:11:50 2007
@@ -8,6 +8,8 @@
 from pypy.objspace.std.proxyobject import *
 from pypy.objspace.std.typeobject import W_TypeObject
 
+type_cache = {}
+
 def proxy(space, w_type, w_controller):
     """tproxy(typ, controller) -> obj
 Return something that looks like it is of type typ. Its behaviour is
@@ -37,9 +39,17 @@
             return W_Transparent(space, w_type, w_controller)
     else:
         raise OperationError(space.w_TypeError, space.wrap("type expected as first argument"))
-    #return type_cache[w_type or w_type.w_bestbase]
-    raise OperationError(space.w_TypeError, space.wrap("Object type %s could not "\
-          "be wrapped (YET)" % w_type.getname(space, "?")))
+    try:
+        return type_cache[w_type or w_type.w_bestbase](space, w_type, w_controller)
+    except KeyError:
+        raise OperationError(space.w_TypeError, space.wrap("Object type %s could not "\
+                                                           "be wrapped (YET)" % w_type.getname(space, "?")))
+
+def register_proxyable(space, cls):
+    tpdef = cls.typedef
+    class W_TransparentUserCreated(W_Transparent):
+        typedef = tpdef
+    type_cache[space.gettypeobject(tpdef)] = W_TransparentUserCreated
 
 def proxy_controller(space, w_object):
     """get_tproxy_controller(obj) -> controller



More information about the Pypy-commit mailing list