[pypy-svn] r25296 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Tue Apr 4 16:12:05 CEST 2006


Author: arigo
Date: Tue Apr  4 16:12:04 2006
New Revision: 25296

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

Passing the specialization test for rpointer.


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 16:12:04 2006
@@ -2,7 +2,7 @@
 from pypy.rpython.error import TyperError
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.model import SomeCTypesObject
-
+from pypy.annotation.pairtype import pairtype
 
 
 class CTypesRepr(Repr):
@@ -70,6 +70,34 @@
             return llops.genop('getfield', inputargs,
                         lltype.Ptr(self.c_data_type) )
 
+    def allocate_instance(self, llops):
+        c1 = inputconst(lltype.Void, self.lowleveltype.TO) 
+        return llops.genop("malloc", [c1], resulttype=self.lowleveltype)
+
+    def allocate_instance_ref(self, llops, v_c_data):
+        """Only if self.ownsmemory is false.  This allocates a new instance
+        and initialize its c_data_ref field."""
+        if self.ownsmemory:
+            raise TyperError("allocate_instance_ref: %r owns its memory" % (
+                self,))
+        v_box = self.allocate_instance(llops)
+        inputargs = [v_box, inputconst(lltype.Void, "c_data_ref"), v_c_data]
+        llops.genop('setfield', inputargs)
+        return v_box
+
+
+class __extend__(pairtype(CTypesRepr, CTypesRepr)):
+
+    def convert_from_to((r_from, r_to), v, llops):
+        """Transparent conversion from the memory-owned to the memory-aliased
+        version of the same ctypes repr."""
+        if (r_from.ctype == r_to.ctype and
+            r_from.ownsmemory and not r_to.ownsmemory):
+            v_c_data = r_from.get_c_data(llops, v)
+            return r_to.allocate_instance_ref(llops, v_c_data)
+        else:
+            return NotImplemented
+
 
 class CTypesRefRepr(CTypesRepr):
     """Base class for ctypes repr that have some kind of by-reference

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 16:12:04 2006
@@ -11,18 +11,19 @@
         self.s_pointer = s_pointer
         self.s_contents = s_contents
         self.ref_ctype = s_contents.knowntype
-
-        if not extregistry.is_registered_type(self.ref_ctype):
-            raise TypeError("Unregistered referenced "
-                            "type: %s" % (self.ref_ctype.__name__,))
-
-        ref_entry = extregistry.lookup_type(self.ref_ctype)
-        contents_repr = ref_entry.get_repr(rtyper, self.s_contents)
-
-        ll_contents = lltype.Ptr(contents_repr.c_data_type)
+        self.r_contents = rtyper.getrepr(s_contents)
+        ll_contents = lltype.Ptr(self.r_contents.c_data_type)
 
         super(PointerRepr, self).__init__(rtyper, s_pointer, ll_contents)
 
+    def rtype_getattr(self, hop):
+        s_attr = hop.args_s[1]
+        assert s_attr.is_constant()
+        assert s_attr.const == 'contents'
+        v_ptr = hop.inputarg(self, 0)
+        v_value = self.getvalue(hop.llops, v_ptr)
+        return self.r_contents.allocate_instance_ref(hop.llops, v_value)
+
 #def registerPointerType(ptrtype):
 #    """Adds a new pointer type to the extregistry.
 #
@@ -107,7 +108,13 @@
                                 methodname=type.__name__)
 
 def pointertype_specialize_call(hop):
-    xxx #...
+    r_ptr = hop.r_result
+    v_result = r_ptr.allocate_instance(hop.llops)
+    if len(hop.args_s):
+        v_contentsbox, = hop.inputargs(r_ptr.r_contents)
+        v_c_data = r_ptr.r_contents.get_c_data(hop.llops, v_contentsbox)
+        r_ptr.setvalue(hop.llops, v_result, v_c_data)
+    return v_result
 
 def pointerinstance_compute_annotation(type, instance):
     return annmodel.SomeCTypesObject(type,

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 16:12:04 2006
@@ -65,8 +65,7 @@
 
 def primitive_specialize_call(hop):
     r_primitive = hop.r_result
-    c1 = hop.inputconst(lltype.Void, r_primitive.lowleveltype.TO) 
-    v_result = hop.genop("malloc", [c1], resulttype=r_primitive.lowleveltype)
+    v_result = r_primitive.allocate_instance(hop.llops)
     if len(hop.args_s):
         v_value, = hop.inputargs(r_primitive.ll_type)
         r_primitive.setvalue(hop.llops, v_result, v_value)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py	Tue Apr  4 16:12:04 2006
@@ -51,7 +51,6 @@
 
 class Test_specialization:
     def test_specialize_c_int_ptr(self):
-        py.test.skip("in-progress")
         ptrtype = POINTER(c_int)
         def func():
             res = c_int(42)
@@ -60,3 +59,15 @@
 
         res = interpret(func, [])
         assert res == 42
+
+    def test_specialize_mutate_via_pointer(self):
+        ptrtype = POINTER(c_int)
+        def func():
+            res = c_int(6)
+            p1 = ptrtype(res)
+            p2 = ptrtype(p1.contents)
+            p2.contents.value *= 7
+            return res.value
+
+        res = interpret(func, [])
+        assert res == 42



More information about the Pypy-commit mailing list