[pypy-svn] r22722 - in pypy/dist/pypy: annotation rpython/rctypes rpython/rctypes/test

stephan at codespeak.net stephan at codespeak.net
Fri Jan 27 12:57:40 CET 2006


Author: stephan
Date: Fri Jan 27 12:57:36 2006
New Revision: 22722

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/interface.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
added support for ctypes arrays

Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Fri Jan 27 12:57:36 2006
@@ -13,6 +13,7 @@
 from pypy.annotation.model import SomePBC, SomeSlice, SomeFloat, s_None
 from pypy.annotation.model import SomeExternalObject
 from pypy.annotation.model import SomeAddress, SomeTypedAddressAccess
+from pypy.annotation.model import SomeCTypesObject
 from pypy.annotation.model import unionof, UnionError, set, missing_operation, TLS
 from pypy.annotation.model import add_knowntypedata, merge_knowntypedata
 from pypy.annotation.bookkeeper import getbookkeeper
@@ -732,3 +733,18 @@
 class __extend__(pairtype(SomeObject, SomeAddress)):
     def union((s_obj, s_addr)):
         raise UnionError, "union of address and anything else makes no sense"
+
+class __extend__(pairtype(SomeCTypesObject, SomeInteger)):
+    def setitem((s_cto, s_index), s_value):
+        pass
+
+    def getitem((s_cto, s_index)):
+        if s_index.is_constant() and isinstance(s_index.const, int):
+            index = s_index.const
+            try:
+                return s_cto.knowntype._type_.annotator_type
+            except AttributeError:
+                return SomeCTypesObject(s_cto.knowntype._type_)
+        else:
+            return SomeObject()
+

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Fri Jan 27 12:57:36 2006
@@ -179,3 +179,15 @@
             """
             _flags_ = _FUNCFLAG_STDCALL
 
+def RARRAY(typ,length):
+    answer = ARRAY(typ,length)
+    def compute_result_annotation(cls, *arg_s):
+        """
+        Answer the result annotation of calling 'cls'.
+        """
+        assert answer is cls
+        return SomeCTypesObject(cls)
+    answer.compute_result_annotation = classmethod(compute_result_annotation)
+    return answer
+
+

Modified: pypy/dist/pypy/rpython/rctypes/interface.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/interface.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/interface.py	Fri Jan 27 12:57:36 2006
@@ -3,7 +3,8 @@
         c_char, c_byte, c_ubyte, \
         c_short, c_ushort, c_uint,\
         c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double, \
-        RStructure as Structure, RByref as byref, RPOINTER as POINTER
+        RStructure as Structure, RByref as byref, RPOINTER as POINTER, \
+        RARRAY as ARRAY
 try:
     from implementation import RWinDLL as WinDLL
 except ImportError:

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	Fri Jan 27 12:57:36 2006
@@ -14,7 +14,8 @@
 from pypy.rpython.rctypes import cdll, c_char_p, c_int, c_char, \
         c_char, c_byte, c_ubyte, c_short, c_ushort, c_uint,\
         c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double, \
-        POINTER, Structure, byref
+        POINTER, Structure, byref, ARRAY
+
 if sys.platform == 'win32':
     mylib = cdll.LoadLibrary('msvcrt.dll')
 elif sys.platform == 'linux2':
@@ -105,6 +106,18 @@
     a = 10
     return c_float( a + 10 )
 
+c_int_10 = ARRAY(c_int,10)
+
+def py_test_annotate_array():
+    return c_int_10()
+
+def py_test_annotate_array_content():
+    my_array = c_int_10()
+    my_array[0] = c_int(1)
+    my_array[1] = 2
+
+    return my_array[0]
+
 class Test_rctypes:
 
     def test_simple(self):
@@ -221,3 +234,15 @@
         s = a.build_types(py_test_simple_ctypes_non_const,[])
         assert s.knowntype == c_float
 
+    def test_annotate_array(self):
+        a = RPythonAnnotator()
+        s = a.build_types(py_test_annotate_array,[])
+        assert s.knowntype == c_int_10
+
+    def test_annotate_array_access(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(py_test_annotate_array_content,[])
+        assert s.knowntype == int
+        #d#t.view()
+



More information about the Pypy-commit mailing list