[pypy-svn] r45074 - in pypy/dist/pypy/rpython/lltypesystem: . test

arigo at codespeak.net arigo at codespeak.net
Sat Jul 14 14:59:20 CEST 2007


Author: arigo
Date: Sat Jul 14 14:59:18 2007
New Revision: 45074

Modified:
   pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
Log:
(lac, arigo)
- support for unsigned integers
- support for void-returning functions.


Modified: pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py	Sat Jul 14 14:59:18 2007
@@ -3,6 +3,7 @@
 import ctypes.util
 from pypy.rpython.lltypesystem import lltype
 from pypy.tool.uid import fixid
+from pypy.rlib.rarithmetic import r_uint
 
 
 def uaddressof(obj):
@@ -10,9 +11,10 @@
 
 
 _ctypes_cache = {
-    lltype.Signed: ctypes.c_long,
-    lltype.Char:   ctypes.c_ubyte,
-    lltype.Float:  ctypes.c_double,
+    lltype.Signed:   ctypes.c_long,
+    lltype.Unsigned: ctypes.c_ulong,
+    lltype.Char:     ctypes.c_ubyte,
+    lltype.Float:    ctypes.c_double,
     }
 
 def build_ctypes_struct(S, max_n=None):
@@ -210,9 +212,7 @@
     """Convert the ctypes object 'cobj' to its lltype equivalent.
     'T' is the expected lltype type.
     """
-    if T is lltype.Char:
-        llobj = chr(cobj)
-    elif isinstance(T, lltype.Ptr):
+    if isinstance(T, lltype.Ptr):
         if isinstance(T.TO, lltype.Struct):
             # XXX var-sized structs
             container = lltype._struct(T.TO)
@@ -227,6 +227,10 @@
         else:
             raise NotImplementedError(T)
         llobj = lltype._ptr(T, container, solid=True)
+    elif T is lltype.Char:
+        llobj = chr(cobj)
+    elif T is lltype.Unsigned:
+        llobj = r_uint(cobj)
     else:
         llobj = cobj
 
@@ -275,7 +279,10 @@
 
     # get_ctypes_type() can raise NotImplementedError too
     cfunc.argtypes = [get_ctypes_type(T) for T in FUNCTYPE.ARGS]
-    cfunc.restype = get_ctypes_type(FUNCTYPE.RESULT)
+    if FUNCTYPE.RESULT is lltype.Void:
+        cfunc.restype = None
+    else:
+        cfunc.restype = get_ctypes_type(FUNCTYPE.RESULT)
     return cfunc
 
 def make_callable_via_ctypes(funcptr):

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	Sat Jul 14 14:59:18 2007
@@ -1,7 +1,9 @@
 import py
+import sys
 import ctypes
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rpython.lltypesystem.ll2ctypes import lltype2ctypes, ctypes2lltype
+from pypy.rlib.rarithmetic import r_uint
 
 
 def test_primitive():
@@ -11,6 +13,11 @@
     assert ctypes2lltype(lltype.Signed, 5) == 5
     assert ctypes2lltype(lltype.Char, ord('a')) == 'a'
     assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF'
+    assert lltype2ctypes(5.25) == 5.25
+    assert ctypes2lltype(lltype.Float, 5.25) == 5.25
+    assert lltype2ctypes(r_uint(-1)) == sys.maxint * 2 + 1
+    res = ctypes2lltype(lltype.Unsigned, sys.maxint * 2 + 1)
+    assert (res, type(res)) == (r_uint(-1), r_uint)
 
 def test_simple_struct():
     S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
@@ -184,3 +191,23 @@
     assert res == 0.625
     assert p[0] == 2
     lltype.free(p, flavor='raw')
+
+def test_rand():
+    rand = rffi.llexternal('rand', [], lltype.Signed,
+                           includes=['stdlib.h'])
+    srand = rffi.llexternal('srand', [lltype.Unsigned], lltype.Void,
+                            includes=['stdlib.h'])
+    srand(r_uint(123))
+    res1 = rand()
+    res2 = rand()
+    res3 = rand()
+    srand(r_uint(123))
+    res1b = rand()
+    res2b = rand()
+    res3b = rand()
+    assert res1 == res1b
+    assert res2 == res2b
+    assert res3 == res3b
+
+# def test_qsort():...
+# def test_signal():...



More information about the Pypy-commit mailing list