[pypy-svn] r25291 - pypy/dist/pypy/rpython/rctypes

arigo at codespeak.net arigo at codespeak.net
Tue Apr 4 15:30:26 CEST 2006


Author: arigo
Date: Tue Apr  4 15:30:25 2006
New Revision: 25291

Modified:
   pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/rmodel.py
   pypy/dist/pypy/rpython/rctypes/rpointer.py
   pypy/dist/pypy/rpython/rctypes/rprimitive.py
Log:
(arre, arigo)

Split some of the CTypesRepr functionality in two subclasses.


Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Tue Apr  4 15:30:25 2006
@@ -5,11 +5,11 @@
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.pairtype import pairtype
 from pypy.rpython.rmodel import IntegerRepr
-from pypy.rpython.rctypes.rmodel import CTypesRepr
+from pypy.rpython.rctypes.rmodel import CTypesRefRepr
 
 ArrayType = type(ARRAY(c_int, 10))
 
-class ArrayRepr(CTypesRepr):
+class ArrayRepr(CTypesRefRepr):
     def __init__(self, rtyper, s_array):
         array_ctype = s_array.knowntype
         
@@ -21,6 +21,7 @@
         self.r_item = item_entry.get_repr(rtyper, SomeCTypesObject(item_ctype,
                                             SomeCTypesObject.OWNSMEMORY))
 
+        # Here, self.c_data_type == self.ll_type
         c_data_type = lltype.Array(self.r_item.ll_type,
                                     hints={"nolength": True})
         
@@ -33,14 +34,12 @@
     def rtype_setitem((r_array, r_int), hop):
         v_array, v_index, v_item = hop.inputargs(r_array, lltype.Signed,
                 r_array.r_item.ll_type)
-        inputargs = [v_array, hop.inputconst(lltype.Void, "c_data")]
         v_c_data = r_array.get_c_data(hop.llops, v_array)
         hop.genop('setarrayitem', [v_c_data, v_index, v_item])
 
     def rtype_getitem((r_array, r_int), hop):
         v_array, v_index = hop.inputargs(r_array, lltype.Signed)
 
-        inputargs = [v_array, hop.inputconst(lltype.Void, "c_data")]
         v_c_data = r_array.get_c_data(hop.llops, v_array)
         return hop.genop('getarrayitem', [v_c_data, v_index],
                 r_array.r_item.ll_type)

Modified: pypy/dist/pypy/rpython/rctypes/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rmodel.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rmodel.py	Tue Apr  4 15:30:25 2006
@@ -8,6 +8,24 @@
 class CTypesRepr(Repr):
     "Base class for the Reprs representing ctypes object."
 
+    # Attributes that are types:
+    #
+    #  * 'ctype'        is the ctypes type.
+    #
+    #  * 'll_type'      is the low-level type representing the raw C data,
+    #                   like Signed or Array(...).
+    #
+    #  * 'c_data_type'  is a low-level container type that also represents
+    #                   the raw C data; the difference is that we can take
+    #                   an lltype pointer to it.  For primitives or pointers
+    #                   this is a Struct with a single field 'value' of
+    #                   type 'll_type'.  Otherwise, c_data_type == ll_type.
+    #
+    #  * 'lowleveltype' is the Repr's choosen low-level type for the RPython
+    #                   variables.  It's a Ptr to a GcStruct.  This is a box
+    #                   traked by our GC around the raw 'c_data_type'-shaped
+    #                   data.
+
     def __init__(self, rtyper, s_ctypesobject, ll_type):
         # s_ctypesobject: the annotation to represent
         # ll_type: the low-level type representing the raw
@@ -25,11 +43,7 @@
         else:
             raise TyperError("unsupported ctypes memorystate %r" % memorystate)
 
-        if isinstance(ll_type, lltype.ContainerType):
-            self.c_data_type = ll_type
-        else:
-            self.c_data_type = lltype.Struct('C_Data_%s' % (ctype.__name__,),
-                                                ('value', ll_type) )
+        self.c_data_type = self.get_c_data_type(ll_type)
 
         if self.ownsmemory:
             self.lowleveltype = lltype.Ptr(
@@ -56,16 +70,32 @@
             return llops.genop('getfield', inputargs,
                         lltype.Ptr(self.c_data_type) )
 
+
+class CTypesRefRepr(CTypesRepr):
+    """Base class for ctypes repr that have some kind of by-reference
+    semantics, like structures and arrays."""
+
+    def get_c_data_type(self, ll_type):
+        assert isinstance(ll_type, lltype.ContainerType)
+        return ll_type
+
+
+class CTypesValueRepr(CTypesRepr):
+    """Base class for ctypes repr that have some kind of by-value
+    semantics, like primitives and pointers."""
+
+    def get_c_data_type(self, ll_type):
+        return lltype.Struct('C_Data_%s' % (self.ctype.__name__,),
+                             ('value', ll_type) )
+
     def setvalue(self, llops, v_box, v_value):
-        """Writes the 'value' field of the raw data
-           (only if ll_type is not a container type)"""
+        """Writes to the 'value' field of the raw data."""
         v_c_data = self.get_c_data(llops, v_box)
         cname = inputconst(lltype.Void, 'value')
         llops.genop('setfield', [v_c_data, cname, v_value])
 
     def getvalue(self, llops, v_box):
-        """Reads the 'value' field of the raw data
-           (only if ll_type is not a container type)"""
+        """Reads from the 'value' field of the raw data."""
         v_c_data = self.get_c_data(llops, v_box)
         cname = inputconst(lltype.Void, 'value')
         return llops.genop('getfield', [v_c_data, cname],

Modified: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py	Tue Apr  4 15:30:25 2006
@@ -2,11 +2,11 @@
 from pypy.rpython import extregistry
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation import model as annmodel
-from pypy.rpython.rctypes.rmodel import CTypesRepr
+from pypy.rpython.rctypes.rmodel import CTypesValueRepr
 
 from ctypes import POINTER, c_int
 
-class PointerRepr(CTypesRepr):
+class PointerRepr(CTypesValueRepr):
     def __init__(self, rtyper, s_pointer, s_contents):
         self.s_pointer = s_pointer
         self.s_contents = s_contents

Modified: pypy/dist/pypy/rpython/rctypes/rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rprimitive.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rprimitive.py	Tue Apr  4 15:30:25 2006
@@ -7,7 +7,7 @@
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.pairtype import pairtype
 from pypy.rpython.rmodel import IntegerRepr
-from pypy.rpython.rctypes.rmodel import CTypesRepr
+from pypy.rpython.rctypes.rmodel import CTypesValueRepr
 
 ctypes_annotation_list = [
     (c_char,          lltype.Char),
@@ -25,7 +25,7 @@
     (c_double,        lltype.Float),
 ]
 
-class PrimitiveRepr(CTypesRepr):
+class PrimitiveRepr(CTypesValueRepr):
 
     def convert_const(self, ctype_value):
         assert isinstance(ctype_value, self.ctype)



More information about the Pypy-commit mailing list