This surprisingly brief patch removes all uses of W_CPythonObject
(though not all uses of W_BuiltinFunctionObject), by using the
'automatically faking non re-implementations' technique I've talked
about before.

Index: objspace/std/fake.py
===================================================================
--- objspace/std/fake.py        (revision 0)
+++ objspace/std/fake.py        (revision 0)
@@ -0,0 +1,32 @@
+from pypy.objspace.std.stdtypedef import *
+from pypy.objspace.std.objspace import W_Object
+
+
+SlotWrapperType = type(type(None).__repr__)
+
+def fake_type(space, cpy_type):
+    assert type(cpy_type) is type
+    kw = {}
+    for s, v in cpy_type.__dict__.items():
+        kw[s] = v
+    def fake__new__(space, w_type, *args_w):
+        args = [space.unwrap(w_arg) for w_arg in args_w]
+        try:
+            r = cpy_type.__new__(cpy_type, *args)
+        except:
+            from cpythonobject import wrap_exception
+            wrap_exception(space)
+        return W_Fake(space, r)
+    def fake_unwrap(space, w_obj):
+        return w_obj.val
+    kw['__new__'] = gateway.interp2app(fake__new__)
+    class W_Fake(W_Object):
+        typedef = StdTypeDef(cpy_type.__name__, **kw)
+        def __init__(w_self, space, val):
+            W_Object.__init__(w_self, space)
+            w_self.val = val
+    space.__class__.unwrap.register(fake_unwrap, W_Fake)
+    W_Fake.__name__ = 'W_Fake(%s)'%(cpy_type.__name__)
+    W_Fake.typedef.fakedcpytype = cpy_type
+    return W_Fake
+        
Index: objspace/std/typeobject.py
===================================================================
--- objspace/std/typeobject.py  (revision 5552)
+++ objspace/std/typeobject.py  (working copy)
@@ -137,6 +137,11 @@
 # XXX __delattr__
 # XXX __hash__ ??
 
+def unwrap__Type(space, w_type):
+    if hasattr(w_type, 'fakedcpytype'):
+        return w_type.fakedcpytype
+    return FailedToImplement
+
 # ____________________________________________________________
 
 def compute_C3_mro(cls):
Index: objspace/std/objspace.py
===================================================================
--- objspace/std/objspace.py    (revision 5552)
+++ objspace/std/objspace.py    (working copy)
@@ -157,6 +157,7 @@
         global W_TypeObject, W_SliceObject, W_LongObject, W_NoneObject
         global W_SeqIterObject
         global W_CPythonObject, W_BuiltinFunctionObject
+        global W_FakeFile
         W_ObjectObject = objectobject.W_ObjectObject
         W_BoolObject = boolobject.W_BoolObject
         W_IntObject = intobject.W_IntObject
@@ -172,8 +173,9 @@
         W_SeqIterObject = iterobject.W_SeqIterObject
         W_CPythonObject = cpythonobject.W_CPythonObject
         W_BuiltinFunctionObject = cpythonobject.W_BuiltinFunctionObject
+        import fake
+        W_FakeFile = fake.fake_type(self, file)
         # end of hacks
-
         # singletons
         self.w_None  = W_NoneObject(self)
         self.w_False = W_BoolObject(self, False)
@@ -182,6 +184,8 @@
         self.w_NotImplemented = self.wrap(NotImplemented(self))  
         self.w_Ellipsis = self.wrap(Ellipsis(self))  
 
+        self.fake_type_cache = {}
+        
         for_builtins = {"False": self.w_False,
                         "True" : self.w_True,
                         "None" : self.w_None,
@@ -197,6 +201,11 @@
             setattr(self, 'w_' + typedef.name, w_type)
             for_builtins[typedef.name] = w_type
 
+        self.w_file = self.gettypeobject(W_FakeFile.typedef)
+        for_builtins['file'] = self.w_file
+        self.fake_type_cache[file] = W_FakeFile
+        
+        
         # exceptions
         for_builtins.update(self.clone_exception_hierarchy())
         
@@ -241,11 +250,29 @@
             return w_result
         # anything below this line is implicitly XXX'ed
         SlotWrapperType = type(type(None).__repr__)
-        if isinstance(x, (types.FunctionType, types.BuiltinFunctionType, 
SlotWrapperType)):
+        if isinstance(x, SlotWrapperType):
+#            print x
             return W_BuiltinFunctionObject(self, x)
         if isinstance(x, type(Exception)) and issubclass(x, Exception):
             if hasattr(self, 'w_' + x.__name__):
                 return getattr(self, 'w_' + x.__name__)
+        if isinstance(x, type):
+            if x in self.fake_type_cache:
+                return self.gettypeobject(self.fake_type_cache[x].typedef)
+            print 'faking %r'%(x,)
+            import fake
+            ft = fake.fake_type(self, x)
+            self.fake_type_cache[x] = ft
+            return self.gettypeobject(self.fake_type_cache[x].typedef)
+        if type(x) in self.fake_type_cache:
+            ft = self.fake_type_cache[type(x)]
+            return ft(self, x)
+        else:
+            print 'faking %r'%(type(x),)
+            import fake
+            ft = fake.fake_type(self, type(x))
+            self.fake_type_cache[type(x)] = ft
+            return ft(self, x)
         print "cpython wrapping %r" % (x,)
         #if hasattr(x, '__bases__'): 
         #    print "cpython wrapping a class %r (%s)" % (x, type(x))
Trying to finish the job -- i.e. removing use of
W_BuiltinFunctionObject wrappers for slot wrappers -- gave me infinite
recursions and a headache.

Thoughts?

Cheers,
mwh

-- 
  Good? Bad? Strap him into the IETF-approved witch-dunking
  apparatus immediately!                        -- NTK now, 21/07/2000
_______________________________________________
[EMAIL PROTECTED]
http://codespeak.net/mailman/listinfo/pypy-dev

Reply via email to