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

arigo at codespeak.net arigo at codespeak.net
Thu Apr 13 12:15:47 CEST 2006


Author: arigo
Date: Thu Apr 13 12:15:45 2006
New Revision: 25776

Added:
   pypy/dist/pypy/rpython/rctypes/rstruct.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
Modified:
   pypy/dist/pypy/rpython/rctypes/implementation.py
Log:
Starting to reimplement ctypes structures.


Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Thu Apr 13 12:15:45 2006
@@ -4,6 +4,7 @@
 import pypy.rpython.rctypes.rarray
 import pypy.rpython.rctypes.rfunc
 import pypy.rpython.rctypes.rchar_p
+import pypy.rpython.rctypes.rstruct
 
 
 # Register the correspondance between SomeCTypesObject and the get_repr()

Added: pypy/dist/pypy/rpython/rctypes/rstruct.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/rstruct.py	Thu Apr 13 12:15:45 2006
@@ -0,0 +1,44 @@
+from ctypes import Structure
+from pypy.annotation.model import SomeCTypesObject, SomeBuiltin
+from pypy.rpython import extregistry
+from pypy.rpython.rctypes.rmodel import CTypesRefRepr, CTypesValueRepr
+
+StructType = type(Structure)
+
+
+
+
+def structtype_specialize_call(hop):
+    r_struct = hop.r_result
+    return hop.genop("malloc", [
+        hop.inputconst(lltype.Void, r_struct.lowleveltype.TO), 
+        ], resulttype=r_struct.lowleveltype,
+    )
+
+def structtype_compute_annotation(metatype, type):
+    def compute_result_annotation(*arg_s):
+        return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
+    return SomeBuiltin(compute_result_annotation, methodname=type.__name__)
+
+extregistry.register_type(StructType, 
+    compute_annotation=structtype_compute_annotation,
+    specialize_call=structtype_specialize_call)
+
+def struct_instance_compute_annotation(type, instance):
+    return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
+
+def struct_instance_field_annotation(s_struct, fieldname):
+    structtype = s_struct.knowntype
+    for name, ctype in structtype._fields_:
+        if name == fieldname:
+            s_result = SomeCTypesObject(ctype, SomeCTypesObject.MEMORYALIAS)
+            return s_result.return_annotation()
+    raise AttributeError('%r has no field %r' % (structtype, fieldname))
+
+def structtype_get_repr(rtyper, s_struct):
+    return StructRepr(rtyper, s_struct)
+
+entry = extregistry.register_metatype(StructType,
+    compute_annotation=struct_instance_compute_annotation,
+    get_repr=structtype_get_repr)
+entry.get_field_annotation = struct_instance_field_annotation

Added: pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py	Thu Apr 13 12:15:45 2006
@@ -0,0 +1,52 @@
+"""
+Test the rctypes implementation.
+"""
+
+import py.test
+import pypy.rpython.rctypes.implementation
+from pypy.annotation.annrpython import RPythonAnnotator
+from pypy.translator.translator import TranslationContext
+from pypy import conftest
+from pypy.translator.c.test.test_genc import compile
+import sys
+from pypy.rpython.test.test_llinterp import interpret
+
+try:
+    import ctypes
+except ImportError:
+    py.test.skip("this test needs ctypes installed")
+
+from ctypes import c_int, c_short, Structure, POINTER, pointer
+
+class tagpoint(Structure):
+    _fields_ = [("x", c_int),
+                ("y", c_int)]
+
+class Test_annotation:
+    def test_annotate_struct(self):
+        def create_struct():
+            return tagpoint()
+
+        a = RPythonAnnotator()
+        s = a.build_types(create_struct, [])
+        assert s.knowntype == tagpoint
+
+        if conftest.option.view:
+            a.translator.view()
+
+    def test_annotate_struct_access(self):
+        def access_struct(n):
+            my_point = tagpoint()
+            my_point.x = c_int(1)
+            my_point.y = 2
+            my_point.x += n
+
+            return my_point.x
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(access_struct, [int])
+        assert s.knowntype == int
+
+        if conftest.option.view:
+            t.view()



More information about the Pypy-commit mailing list