[pypy-svn] r20941 - in pypy/dist/pypy/rpython: lltypesystem test

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Dec 9 16:07:06 CET 2005


Author: cfbolz
Date: Fri Dec  9 16:07:05 2005
New Revision: 20941

Modified:
   pypy/dist/pypy/rpython/lltypesystem/lltype.py
   pypy/dist/pypy/rpython/test/test_lltype.py
Log:
(johahn, cfbolz):

make it possible to attach custom destructor function pointers to GcStructs.



Modified: pypy/dist/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lltype.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/lltype.py	Fri Dec  9 16:07:05 2005
@@ -250,7 +250,7 @@
 class GcStruct(Struct):
     _runtime_type_info = None
 
-    def _attach_runtime_type_info_funcptr(self, funcptr):
+    def _attach_runtime_type_info_funcptr(self, funcptr, destrptr):
         if self._runtime_type_info is None:
             self._runtime_type_info = opaqueptr(RuntimeTypeInfo, name=self._name, about=self)._obj
         if funcptr is not None:
@@ -263,6 +263,17 @@
                 raise TypeError("expected a runtime type info function "
                                 "implementation, got: %s" % funcptr)
             self._runtime_type_info.query_funcptr = funcptr
+        if destrptr is not None :
+            T = typeOf(destrptr)
+            if (not isinstance(T, Ptr) or
+                not isinstance(T.TO, FuncType) or
+                len(T.TO.ARGS) != 1 or
+                T.TO.RESULT != Void or
+                castable(T.TO.ARGS[0], Ptr(self)) < 0):
+                raise TypeError("expected a destructor function "
+                                "implementation, got: %s" % destrptr)
+            self._runtime_type_info.destructor_funcptr = destrptr
+           
 
 class Array(ContainerType):
     __name__ = 'array'
@@ -969,10 +980,10 @@
     return id(obj)
 
 
-def attachRuntimeTypeInfo(GCSTRUCT, funcptr=None):
+def attachRuntimeTypeInfo(GCSTRUCT, funcptr=None, destrptr=None):
     if not isinstance(GCSTRUCT, GcStruct):
         raise TypeError, "expected a GcStruct: %s" % GCSTRUCT
-    GCSTRUCT._attach_runtime_type_info_funcptr(funcptr)
+    GCSTRUCT._attach_runtime_type_info_funcptr(funcptr, destrptr)
     return _ptr(Ptr(RuntimeTypeInfo), GCSTRUCT._runtime_type_info)
 
 def getRuntimeTypeInfo(GCSTRUCT):

Modified: pypy/dist/pypy/rpython/test/test_lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_lltype.py	(original)
+++ pypy/dist/pypy/rpython/test/test_lltype.py	Fri Dec  9 16:07:05 2005
@@ -361,6 +361,27 @@
     assert getRuntimeTypeInfo(Sbis) != pinf0
     assert Sbis != S # the attached runtime type info distinguishes them
 
+def test_getRuntimeTypeInfo_destrpointer():
+    S = GcStruct('s', ('x', Signed))
+    def f(s):
+        s.x = 1
+    def type_info_S(p):
+        return getRuntimeTypeInfo(S)
+    qp = functionptr(FuncType([Ptr(S)], Ptr(RuntimeTypeInfo)), 
+                     "type_info_S", 
+                     _callable=type_info_S)
+    dp = functionptr(FuncType([Ptr(S)], Void), 
+                     "destructor_funcptr", 
+                     _callable=f)
+    pinf0 = attachRuntimeTypeInfo(S, qp, destrptr=dp)
+    assert pinf0._obj.about == S
+    pinf = getRuntimeTypeInfo(S)
+    assert pinf == pinf0
+    pinf1 = getRuntimeTypeInfo(S)
+    assert pinf == pinf1
+    assert pinf._obj.destructor_funcptr == dp
+    assert pinf._obj.query_funcptr == qp
+
 def test_runtime_type_info():
     S = GcStruct('s', ('x', Signed))
     attachRuntimeTypeInfo(S)



More information about the Pypy-commit mailing list