[pypy-svn] r54018 - in pypy/dist/pypy: module/_rawffi rlib rlib/test translator/c/test

arigo at codespeak.net arigo at codespeak.net
Tue Apr 22 17:11:36 CEST 2008


Author: arigo
Date: Tue Apr 22 17:11:35 2008
New Revision: 54018

Modified:
   pypy/dist/pypy/module/_rawffi/interp_rawffi.py
   pypy/dist/pypy/rlib/libffi.py
   pypy/dist/pypy/rlib/test/test_libffi.py
   pypy/dist/pypy/translator/c/test/test_newgc.py
Log:
Never never never use ffi_type_ulong and ffi_type_slong.
On 32-bit machines it's sometimes of size 4 and sometimes of size 8.


Modified: pypy/dist/pypy/module/_rawffi/interp_rawffi.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/interp_rawffi.py	(original)
+++ pypy/dist/pypy/module/_rawffi/interp_rawffi.py	Tue Apr 22 17:11:35 2008
@@ -13,18 +13,6 @@
 from pypy.rlib.rarithmetic import intmask, r_uint, r_singlefloat
 from pypy.module._rawffi.tracker import tracker
 
-def _signed_type_for(TYPE):
-    sz = rffi.sizeof(TYPE)
-    if sz == 4:   return ffi_type_sint32
-    elif sz == 8: return ffi_type_sint64
-    else: raise ValueError("unsupported type size for %r" % (TYPE,))
-
-def _unsigned_type_for(TYPE):
-    sz = rffi.sizeof(TYPE)
-    if sz == 4:   return ffi_type_uint32
-    elif sz == 8: return ffi_type_uint64
-    else: raise ValueError("unsupported type size for %r" % (TYPE,))
-
 TYPEMAP = {
     # XXX A mess with unsigned/signed/normal chars :-/
     'c' : ffi_type_uchar,
@@ -33,14 +21,12 @@
     'h' : ffi_type_sshort,
     'u' : ffi_type_uint, # XXX think deeper how to map it properly
     'H' : ffi_type_ushort,
-    'i' : ffi_type_sint,
-    'I' : ffi_type_uint,
-    # xxx don't use ffi_type_slong and ffi_type_ulong - their meaning
-    # changes from a libffi version to another :-((
-    'l' : _signed_type_for(rffi.LONG),
-    'L' : _unsigned_type_for(rffi.ULONG),
-    'q' : _signed_type_for(rffi.LONGLONG),
-    'Q' : _unsigned_type_for(rffi.ULONGLONG),
+    'i' : cast_type_to_ffitype(rffi.INT),
+    'I' : cast_type_to_ffitype(rffi.UINT),
+    'l' : cast_type_to_ffitype(rffi.LONG),
+    'L' : cast_type_to_ffitype(rffi.ULONG),
+    'q' : cast_type_to_ffitype(rffi.LONGLONG),
+    'Q' : cast_type_to_ffitype(rffi.ULONGLONG),
     'f' : ffi_type_float,
     'd' : ffi_type_double,
     's' : ffi_type_pointer,

Modified: pypy/dist/pypy/rlib/libffi.py
==============================================================================
--- pypy/dist/pypy/rlib/libffi.py	(original)
+++ pypy/dist/pypy/rlib/libffi.py	Tue Apr 22 17:11:35 2008
@@ -70,7 +70,10 @@
     return l
 
 base_names = ['double', 'uchar', 'schar', 'sshort', 'ushort', 'uint', 'sint',
-              'ulong', 'slong', 'float', 'pointer', 'void',
+              # ffi_type_slong and ffi_type_ulong are omitted because
+              # their meaning changes too much from one libffi version to
+              # another.  DON'T USE THEM!  use cast_type_to_ffitype().
+              'float', 'pointer', 'void',
               # by size
               'sint8', 'uint8', 'sint16', 'uint16', 'sint32', 'uint32',
               'sint64', 'uint64']
@@ -90,6 +93,18 @@
 for name in type_names:
     locals()[name] = configure_simple_type(name)
 
+def _signed_type_for(TYPE):
+    sz = rffi.sizeof(TYPE)
+    if sz == 4:   return ffi_type_sint32
+    elif sz == 8: return ffi_type_sint64
+    else: raise ValueError("unsupported type size for %r" % (TYPE,))
+
+def _unsigned_type_for(TYPE):
+    sz = rffi.sizeof(TYPE)
+    if sz == 4:   return ffi_type_uint32
+    elif sz == 8: return ffi_type_uint64
+    else: raise ValueError("unsupported type size for %r" % (TYPE,))
+
 TYPE_MAP = {
     rffi.DOUBLE : ffi_type_double,
     rffi.FLOAT  : ffi_type_float,
@@ -99,8 +114,12 @@
     rffi.USHORT : ffi_type_ushort,
     rffi.UINT   : ffi_type_uint,
     rffi.INT    : ffi_type_sint,
-    rffi.ULONG  : ffi_type_ulong,
-    rffi.LONG   : ffi_type_slong,
+    # xxx don't use ffi_type_slong and ffi_type_ulong - their meaning
+    # changes from a libffi version to another :-((
+    rffi.ULONG     : _unsigned_type_for(rffi.ULONG),
+    rffi.LONG      : _signed_type_for(rffi.LONG),
+    rffi.ULONGLONG : _unsigned_type_for(rffi.ULONGLONG),
+    rffi.LONGLONG  : _signed_type_for(rffi.LONGLONG),
     lltype.Void : ffi_type_void,
     }
 

Modified: pypy/dist/pypy/rlib/test/test_libffi.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_libffi.py	(original)
+++ pypy/dist/pypy/rlib/test/test_libffi.py	Tue Apr 22 17:11:35 2008
@@ -5,6 +5,7 @@
 from pypy.rpython.test.test_llinterp import interpret
 from pypy.translator.c.test.test_genc import compile
 from pypy.rlib.libffi import *
+from pypy.rlib.objectmodel import keepalive_until_here
 from pypy.rpython.lltypesystem.ll2ctypes import ALLOCATED
 from pypy.rpython.lltypesystem import rffi, lltype
 import os, sys
@@ -78,7 +79,8 @@
     def test_wrong_args(self):
         libc = CDLL('libc.so.6')
         # XXX assume time_t is long
-        ctime = libc.getpointer('time', [ffi_type_pointer], ffi_type_ulong)
+        ulong = cast_type_to_ffitype(rffi.ULONG)
+        ctime = libc.getpointer('time', [ffi_type_pointer], ulong)
         x = lltype.malloc(lltype.GcStruct('xxx'))
         y = lltype.malloc(lltype.GcArray(rffi.LONG), 3)
         z = lltype.malloc(lltype.Array(rffi.LONG), 4, flavor='raw')
@@ -93,7 +95,8 @@
     def test_call_time(self):
         libc = CDLL('libc.so.6')
         # XXX assume time_t is long
-        ctime = libc.getpointer('time', [ffi_type_pointer], ffi_type_ulong)
+        ulong = cast_type_to_ffitype(rffi.ULONG)
+        ctime = libc.getpointer('time', [ffi_type_pointer], ulong)
         ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG)))
         t0 = ctime.call(rffi.LONG)
         time.sleep(2)
@@ -140,14 +143,17 @@
         assert snd == rffi.cast(rffi.VOIDP, a)
         
     def test_callback(self):
+        slong = cast_type_to_ffitype(rffi.LONG)
         libc = CDLL('libc.so.6')
-        qsort = libc.getpointer('qsort', [ffi_type_pointer, ffi_type_slong,
-                                          ffi_type_slong, ffi_type_pointer],
+        qsort = libc.getpointer('qsort', [ffi_type_pointer, slong,
+                                          slong, ffi_type_pointer],
                                 ffi_type_void)
 
         def callback(ll_args, ll_res, stuff):
-            a1 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[0])[0]
-            a2 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[1])[0]
+            p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0]
+            p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0]
+            a1 = rffi.cast(rffi.INTP, p_a1)[0]
+            a2 = rffi.cast(rffi.INTP, p_a2)[0]
             res = rffi.cast(rffi.INTP, ll_res)
             if a1 > a2:
                 res[0] = 1
@@ -170,6 +176,8 @@
         qsort.call(lltype.Void)
         assert [to_sort[i] for i in range(4)] == [1,2,3,4]
         lltype.free(to_sort, flavor='raw')
+        keepalive_until_here(ptr)  # <= this test is not translated, but don't
+                                   #    forget this in code that is meant to be
 
     def test_compile(self):
         import py
@@ -241,11 +249,12 @@
 
         lib = CDLL(lib_name)
 
-        size = ffi_type_slong.c_size*2
-        alignment = ffi_type_slong.c_alignment
+        slong = cast_type_to_ffitype(rffi.LONG)
+        size = slong.c_size*2
+        alignment = slong.c_alignment
         tp = make_struct_ffitype(size, alignment)
 
-        sum_x_y = lib.getrawpointer('sum_x_y', [tp], ffi_type_slong)
+        sum_x_y = lib.getrawpointer('sum_x_y', [tp], slong)
 
         buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw')
         buffer[0] = 200

Modified: pypy/dist/pypy/translator/c/test/test_newgc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_newgc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_newgc.py	Tue Apr 22 17:11:35 2008
@@ -10,6 +10,7 @@
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rpython.lltypesystem.lloperation import llop
 from pypy.rpython.memory.test import snippet
+from pypy.rlib.objectmodel import keepalive_until_here
 from pypy import conftest
 
 def compile_func(fn, inputtypes, t=None, gcpolicy="ref"):
@@ -815,15 +816,18 @@
 
     def test_callback_with_collect(self):
         py.test.skip("Segfaults")
-        from pypy.rlib.libffi import ffi_type_pointer, ffi_type_slong,\
+        from pypy.rlib.libffi import ffi_type_pointer, cast_type_to_ffitype,\
              CDLL, ffi_type_void, CallbackFuncPtr, ffi_type_sint
         from pypy.rpython.lltypesystem import rffi
         from pypy.rlib import rgc
         import gc
+        slong = cast_type_to_ffitype(rffi.LONG)
 
         def callback(ll_args, ll_res, stuff):
-            a1 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[0])[0]
-            a2 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[1])[0]
+            p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0]
+            p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0]
+            a1 = rffi.cast(rffi.INTP, p_a1)[0]
+            a2 = rffi.cast(rffi.INTP, p_a2)[0]
             res = rffi.cast(rffi.INTP, ll_res)
             if a1 > a2:
                 res[0] = 1
@@ -833,8 +837,8 @@
 
         def f():
             libc = CDLL('libc.so.6')
-            qsort = libc.getpointer('qsort', [ffi_type_pointer, ffi_type_slong,
-                                              ffi_type_slong, ffi_type_pointer],
+            qsort = libc.getpointer('qsort', [ffi_type_pointer, slong,
+                                              slong, ffi_type_pointer],
                                 ffi_type_void)
 
             ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer],
@@ -853,6 +857,7 @@
             qsort.call(lltype.Void)
             result = [to_sort[i] for i in range(4)] == [1,2,3,4]
             lltype.free(to_sort, flavor='raw')
+            keepalive_until_here(ptr)
             return int(result)
 
         c_fn = self.getcompiled(f)



More information about the Pypy-commit mailing list