[pypy-svn] r60058 - in pypy/branch/oo-jit/pypy/rpython: . lltypesystem

arigo at codespeak.net arigo at codespeak.net
Fri Nov 21 19:01:53 CET 2008


Author: arigo
Date: Fri Nov 21 19:01:51 2008
New Revision: 60058

Modified:
   pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py
   pypy/branch/oo-jit/pypy/rpython/lltypesystem/rclass.py
   pypy/branch/oo-jit/pypy/rpython/rtyper.py
Log:
Waaa.  Incredible amounts of dirty hack to support
reading the tyeptr field of a ll2ctypes instance...


Modified: pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py	Fri Nov 21 19:01:51 2008
@@ -16,6 +16,7 @@
 from pypy.rlib.rarithmetic import r_uint, r_singlefloat
 from pypy.annotation import model as annmodel
 from pypy.rpython.llinterp import LLInterpreter
+from pypy.rpython.lltypesystem.rclass import OBJECT
 
 def uaddressof(obj):
     return fixid(ctypes.addressof(obj))
@@ -369,6 +370,12 @@
     def __ne__(self, other):
         return not (self == other)
 
+    def __hash__(self):
+        if self._storage is not None:
+            return ctypes.addressof(self._storage)
+        else:
+            return object.__hash__(self)
+
     def __repr__(self):
         if self._storage is None:
             return '<freed C object %s>' % (self._TYPE,)
@@ -561,6 +568,21 @@
                 carray = getattr(cobj.contents, T.TO._arrayfld)
                 container = lltype._struct(T.TO, carray.length)
             else:
+                # special treatment of 'OBJECT' subclasses
+                if rtyper and lltype._castdepth(T.TO, OBJECT) > 0:
+                    ctypes_object = get_ctypes_type(rtyper,
+                                                    lltype.Ptr(OBJECT))
+                    as_obj = ctypes2lltype(lltype.Ptr(OBJECT),
+                                           ctypes.cast(cobj, ctypes_object),
+                                           rtyper)
+                    TObj = rtyper.get_type_for_typeptr(as_obj.typeptr)
+                    if TObj != T.TO:
+                        ctypes_instance = get_ctypes_type(rtyper,
+                                                          lltype.Ptr(TObj))
+                        return lltype.cast_pointer(T,
+                            ctypes2lltype(lltype.Ptr(TObj),
+                                          ctypes.cast(cobj, ctypes_instance),
+                                          rtyper))
                 container = lltype._struct(T.TO)
             struct_use_ctypes_storage(container, cobj.contents)
         elif isinstance(T.TO, lltype.Array):

Modified: pypy/branch/oo-jit/pypy/rpython/lltypesystem/rclass.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/lltypesystem/rclass.py	(original)
+++ pypy/branch/oo-jit/pypy/rpython/lltypesystem/rclass.py	Fri Nov 21 19:01:51 2008
@@ -382,6 +382,9 @@
             self.rtyper.attachRuntimeTypeInfoFunc(self.object_type,
                                                   ll_runtime_type_info,
                                                   OBJECT, destrptr)
+            vtable = self.rclass.getvtable()
+            self.rtyper.type_for_typeptr[vtable._obj] = self.lowleveltype.TO
+
     def common_repr(self): # -> object or nongcobject reprs
         return getinstancerepr(self.rtyper, None, self.gcflavor)
 

Modified: pypy/branch/oo-jit/pypy/rpython/rtyper.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/rtyper.py	(original)
+++ pypy/branch/oo-jit/pypy/rpython/rtyper.py	Fri Nov 21 19:01:51 2008
@@ -57,6 +57,7 @@
         self._dict_traits = {}
         self.class_reprs = {}
         self.instance_reprs = {}
+        self.type_for_typeptr = {}
         self.pbc_reprs = {}
         self.classes_with_wrapper = {}
         self.wrapper_context = None # or add an extra arg to convertvar?
@@ -137,6 +138,17 @@
             result[repr.lowleveltype.TO] = repr.rclass.getvtable()
         return result
 
+    def get_type_for_typeptr(self, typeptr):
+        try:
+            return self.type_for_typeptr[typeptr._obj]
+        except KeyError:
+            # rehash the dictionary
+            type_for_typeptr = {}
+            for key, value in self.type_for_typeptr.items():
+                type_for_typeptr[key] = value
+            self.type_for_typeptr = type_for_typeptr
+            return self.type_for_typeptr[typeptr._obj]
+
     def makekey(self, s_obj):
         return pair(self.type_system, s_obj).rtyper_makekey(self)
 



More information about the Pypy-commit mailing list