[pypy-svn] r36103 - in pypy/dist/pypy/rlib/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Tue Jan 2 15:31:54 CET 2007


Author: arigo
Date: Tue Jan  2 15:31:50 2007
New Revision: 36103

Added:
   pypy/dist/pypy/rlib/rctypes/rchar_p.py   (contents, props changed)
   pypy/dist/pypy/rlib/rctypes/test/test_rchar_p.py
      - copied, changed from r36079, pypy/dist/pypy/rpython/rctypes/test/test_rchar_p.py
   pypy/dist/pypy/rlib/rctypes/test/test_rstruct.py
      - copied, changed from r36072, pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
Modified:
   pypy/dist/pypy/rlib/rctypes/implementation.py
   pypy/dist/pypy/rlib/rctypes/rstruct.py
Log:
rchar_p implementation.  Some more work on rstruct.


Modified: pypy/dist/pypy/rlib/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/implementation.py	Tue Jan  2 15:31:50 2007
@@ -43,13 +43,16 @@
     register_for_metatype = classmethod(register_for_metatype)
 
     def convert(self, x):
-        assert isinstance(x, self.ctype)
-        key = id(x)
+        if isinstance(x, self.ctype):
+            key = "by_id", id(x)
+        else:
+            key = "by_value", x
+            x = self.ctype(x)
         try:
-            return self.instance_cache[key]
+            return self.instance_cache[key][0]
         except KeyError:
             obj = self.new()
-            self.instance_cache[key] = obj
+            self.instance_cache[key] = obj, x     # keep 'x' alive
             self.initialize_prebuilt(obj, x)
             return obj
 
@@ -172,3 +175,4 @@
 import pypy.rlib.rctypes.rpointer
 import pypy.rlib.rctypes.rstruct
 import pypy.rlib.rctypes.rbuiltin
+import pypy.rlib.rctypes.rchar_p

Added: pypy/dist/pypy/rlib/rctypes/rchar_p.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rlib/rctypes/rchar_p.py	Tue Jan  2 15:31:50 2007
@@ -0,0 +1,34 @@
+from pypy.rlib.rctypes.implementation import CTypeController
+from pypy.rlib.rctypes import rctypesobject
+
+from ctypes import c_char_p
+
+
+class CCharPCTypeController(CTypeController):
+    knowntype = rctypesobject.rc_char_p
+
+    def new(self, initialvalue=None):
+        obj = rctypesobject.rc_char_p.allocate()
+        obj.set_value(initialvalue)
+        return obj
+
+    def initialize_prebuilt(self, obj, x):
+        string = x.value
+        obj.set_value(string)
+
+    def get_value(self, obj):
+        return obj.get_value()
+
+    def set_value(self, obj, string):
+        obj.set_value(string)
+
+    # ctypes automatically unwraps the c_char_p() instances when
+    # they are returned by most operations
+    return_value = get_value
+    store_value = set_value
+
+    def is_true(self, obj):
+        return obj.get_value() is not None
+
+
+CCharPCTypeController.register_for_type(c_char_p)

Modified: pypy/dist/pypy/rlib/rctypes/rstruct.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rstruct.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rstruct.py	Tue Jan  2 15:31:50 2007
@@ -1,5 +1,6 @@
 from pypy.rlib.rctypes.implementation import CTypeController, getcontroller
 from pypy.rlib.rctypes import rctypesobject
+from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem import lltype
 from pypy.rlib.unroll import unrolling_iterable
 
@@ -24,6 +25,7 @@
         external = getattr(ctype, '_external_', False)
         self.knowntype = rctypesobject.RStruct(ctype.__name__, fields,
                                                c_external = external)
+        self.fieldcontrollers = controllers
 
         # Build a custom new() method where the setting of the fields
         # is unrolled
@@ -68,5 +70,33 @@
         self.setattr = structsetattr
         self.setboxattr = structsetboxattr
 
+    def initialize_prebuilt(self, obj, x):
+        for name, controller in self.fieldcontrollers:
+            fieldbox = controller.convert(getattr(x, name))
+            self.setboxattr(obj, name, fieldbox)
+
 
 StructCTypeController.register_for_metatype(StructType)
+
+# ____________________________________________________________
+
+def offsetof(Struct, fieldname):
+    "Utility function that returns the offset of a field in a structure."
+    return getattr(Struct, fieldname).offset
+
+class OffsetOfFnEntry(ExtRegistryEntry):
+    "Annotation and rtyping of calls to offsetof()"
+    _about_ = offsetof
+
+    def compute_result_annotation(self, s_Struct, s_fieldname):
+        assert s_Struct.is_constant()
+        assert s_fieldname.is_constant()
+        ofs = offsetof(s_Struct.const, s_fieldname.const)
+        assert ofs >= 0
+        s_result = SomeInteger(nonneg=True)
+        s_result.const = ofs
+        return s_result
+
+    def specialize_call(self, hop):
+        ofs = hop.s_result.const
+        return hop.inputconst(lltype.Signed, ofs)


More information about the pypy-svn mailing list