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

stephan at codespeak.net stephan at codespeak.net
Thu Jan 26 12:55:19 CET 2006


Author: stephan
Date: Thu Jan 26 12:55:16 2006
New Revision: 22677

Modified:
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/interface.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
provides annotation information for basic ctypes (+ tests)

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Thu Jan 26 12:55:16 2006
@@ -3,6 +3,7 @@
 """
 
 import sys
+import ctypes
 from ctypes import *
 from ctypes import _FUNCFLAG_CDECL
 if sys.platform == "win32":
@@ -49,6 +50,12 @@
         the_type.ll_type = ll_type
         if wrap_arg is not None:
             the_type.wrap_arg = wrap_arg
+        else:
+            # !!!! attention !!!!
+            # the basic c_types need some annotation information
+            # at the moment that are exactly the types that have
+            # no 'wrap_arg'. This might change in the future
+            the_type.compute_result_annotation = classmethod(lambda cls, s_arg:SomeCTypesObject(cls))
 
 create_ctypes_annotations()
 
@@ -82,7 +89,12 @@
                     answer.append(ctype_type.wrap_arg(ll_type, arg_name))
             return answer
 
+class CtypesBasicTypeInstantiationTranslation( FunctionPointerTranslation ):
 
+    compute_result_annotation = classmethod(
+            FunctionPointerTranslation.compute_result_annotation)
+
+    
 class RStructureMeta(type(Structure)):
     def __new__(mta,name,bases,clsdict):
         _fields = clsdict.get('_fields_',None)

Modified: pypy/dist/pypy/rpython/rctypes/interface.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/interface.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/interface.py	Thu Jan 26 12:55:16 2006
@@ -1,5 +1,8 @@
 from ctypes import _DLLS
-from implementation import RCDLL as CDLL, c_int, c_char_p, c_char, \
+from implementation import RCDLL as CDLL, c_int, c_char_p, \
+        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
 try:
     from implementation import RWinDLL as WinDLL

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	Thu Jan 26 12:55:16 2006
@@ -11,7 +11,10 @@
     py.test.skip("this test needs ctypes installed")
 
 
-from pypy.rpython.rctypes import cdll, c_char_p, c_int, c_char, POINTER, Structure, byref
+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
 if sys.platform == 'win32':
     mylib = cdll.LoadLibrary('msvcrt.dll')
 elif sys.platform == 'linux2':
@@ -78,6 +81,26 @@
     res  = testfunc_byval(inpoint,oppoint)
     return res, oppoint
 
+def py_test_simple_cint():
+    return c_int(10)
+
+def py_test_simple_ctypes():
+    return (
+            c_char('a'),
+            c_byte(1),
+            c_ubyte(1),
+            c_short(1),
+            c_ushort(1),
+            c_int(1),
+            c_uint(1),
+            c_long(1),
+            c_ulong(1),
+            c_longlong(1),
+            c_ulonglong(1),
+            c_float(1.0),
+            c_double(1.0)
+    )
+
 
 class Test_rctypes:
 
@@ -166,3 +189,27 @@
         assert s.items[0].knowntype == int
         assert s.items[1].knowntype == POINTER(tagpoint)
 
+    def test_annotate_simple_cint(self):
+        a = RPythonAnnotator()
+        s = a.build_types(py_test_simple_cint,[])
+        assert s.knowntype == c_int
+
+    def test_annotate_simple_types(self):
+        a = RPythonAnnotator()
+        s = a.build_types(py_test_simple_ctypes,[])
+        assert s.knowntype == tuple
+        assert len(s.items) == 13
+        assert s.items[0].knowntype == c_char
+        assert s.items[1].knowntype == c_byte
+        assert s.items[2].knowntype == c_ubyte
+        assert s.items[3].knowntype == c_short
+        assert s.items[4].knowntype == c_ushort
+        assert s.items[5].knowntype == c_int
+        assert s.items[6].knowntype == c_uint
+        assert s.items[7].knowntype == c_long
+        assert s.items[8].knowntype == c_ulong
+        assert s.items[9].knowntype == c_longlong
+        assert s.items[10].knowntype == c_ulonglong
+        assert s.items[11].knowntype == c_float
+        assert s.items[12].knowntype == c_double
+



More information about the Pypy-commit mailing list