[pypy-commit] pypy default: truncate python longs when converting to C types

antocuni noreply at buildbot.pypy.org
Tue Jul 12 11:06:53 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r45499:194dbf1456fc
Date: 2011-07-12 10:51 +0200
http://bitbucket.org/pypy/pypy/changeset/194dbf1456fc/

Log:	truncate python longs when converting to C types

diff --git a/pypy/module/_ffi/interp_ffi.py b/pypy/module/_ffi/interp_ffi.py
--- a/pypy/module/_ffi/interp_ffi.py
+++ b/pypy/module/_ffi/interp_ffi.py
@@ -149,6 +149,11 @@
         raise OperationError(space.w_TypeError, space.wrap(msg))
     return res
 
+def unwrap_truncate_int(space, w_arg):
+    if space.is_true(space.isinstance(w_arg, space.w_int)):
+        return space.int_w(w_arg)
+    else:
+        return rffi.cast(rffi.LONG, space.bigint_w(w_arg).ulonglongmask())
 
 # ========================================================================
 
@@ -184,7 +189,7 @@
                 kind = libffi.types.getkind(w_argtype.ffitype) # XXX: remove the kind
                 self.arg_longlong(space, argchain, kind, w_arg)
             elif w_argtype.is_signed():
-                argchain.arg(space.int_w(w_arg))
+                argchain.arg(unwrap_truncate_int(space, w_arg))
             elif w_argtype.is_pointer():
                 w_arg = self.convert_pointer_arg_maybe(space, w_arg, w_argtype)
                 argchain.arg(intmask(space.uint_w(w_arg)))
diff --git a/pypy/module/_ffi/test/test__ffi.py b/pypy/module/_ffi/test/test__ffi.py
--- a/pypy/module/_ffi/test/test__ffi.py
+++ b/pypy/module/_ffi/test/test__ffi.py
@@ -119,10 +119,12 @@
                 return x+y;
             }
         """
+        import sys
         from _ffi import CDLL, types
         libfoo = CDLL(self.libfoo_name)
         sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint)
         assert sum_xy(30, 12) == 42
+        assert sum_xy(sys.maxint*2, 0) == -2
 
     def test_void_result(self):
         """


More information about the pypy-commit mailing list