[pypy-svn] r50644 - in pypy/branch/applevel-ctypes2/pypy/module/_rawffi: . test

arigo at codespeak.net arigo at codespeak.net
Tue Jan 15 17:48:20 CET 2008


Author: arigo
Date: Tue Jan 15 17:48:20 2008
New Revision: 50644

Modified:
   pypy/branch/applevel-ctypes2/pypy/module/_rawffi/interp_rawffi.py
   pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py
Log:
(fijal, arigo)
Kill all the nice work on the size checkers.  Instead, _rawffi
now truncates whatever int or long Python object it receives,
just like ctypes.  :-/


Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/interp_rawffi.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/interp_rawffi.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/interp_rawffi.py	Tue Jan 15 17:48:20 2008
@@ -161,34 +161,7 @@
 buffer."""
 )
 
-def make_size_checker(format, size, signed):
-    min, max, _ = min_max_acc_method(size, signed)
-
-    def checker(value):
-        if value < min:
-            raise FfiValueError("%d too small for format %s" % (value, format))
-        elif value > max:
-            raise FfiValueError("%d too large for format %s" % (value, format))
-    return checker
-
-_SIZE_CHECKERS = {
-    'b' : True,
-    'B' : False,
-    'h' : True,
-    'H' : False,
-    'i' : True,
-    'I' : False,
-    'l' : True,
-    'L' : False,
-    'q' : True,
-    'Q' : False,
-}
-
-# XXX check for single float as well
-SIZE_CHECKERS = {}
-for c, signed in _SIZE_CHECKERS.items():
-    SIZE_CHECKERS[c] = make_size_checker(c, native_fmttable[c]['size'], signed)
-unroll_size_checkers = unrolling_iterable(SIZE_CHECKERS.items())
+unroll_letters_for_numbers = unrolling_iterable("bBhHiIlLqQ")
 
 def segfault_exception(space, reason):
     w_mod = space.getbuiltinmodule("_rawffi")
@@ -254,22 +227,14 @@
         val = s[0]
         push_func(add_arg, argdesc, val)
     else:
-        for c, checker in unroll_size_checkers:
+        for c in unroll_letters_for_numbers:
             if letter == c:
-                if c == "q":
-                    val = space.r_longlong_w(w_arg)
-                elif c == "Q":
-                    val = space.r_ulonglong_w(w_arg)
-                elif c == "I" or c == "L" or c =="B":
-                    val = space.uint_w(w_arg)
-                else:
-                    val = space.int_w(w_arg)
-                try:
-                    checker(val)
-                except FfiValueError, e:
-                    raise OperationError(space.w_ValueError, w(e.msg))
                 TP = LL_TYPEMAP[c]
-                push_func(add_arg, argdesc, rffi.cast(TP, val))
+                if space.is_true(space.isinstance(w_arg, space.w_int)):
+                    val = rffi.cast(TP, space.int_w(w_arg))
+                else:
+                    val = rffi.cast(TP, space.bigint_w(w_arg).ulonglongmask())
+                push_func(add_arg, argdesc, val)
                 return
         else:
             raise OperationError(space.w_TypeError,
@@ -292,9 +257,6 @@
                 return space.wrap(func(add_arg, argdesc, ll_type))
             elif c == 'f' or c == 'd':
                 return space.wrap(float(func(add_arg, argdesc, ll_type)))
-            elif c == 'h' or c == 'H':
-                return space.wrap(rffi.cast(lltype.Signed, func(add_arg, argdesc,
-                                                           ll_type)))
             else:
                 return space.wrap(intmask(func(add_arg, argdesc, ll_type)))
     raise OperationError(space.w_TypeError,

Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py	Tue Jan 15 17:48:20 2008
@@ -350,9 +350,6 @@
         res = some_huge_uvalue()
         assert res[0] == 1<<42
         res.free()
-        arg1 = _rawffi.Array('Q')(1)
-        raises(ValueError, "arg1[0] = -1")
-        arg1.free()
         pass_ll = lib.ptr('pass_ll', ['q'], 'q')
         arg1 = _rawffi.Array('q')(1)
         arg1[0] = 1<<42
@@ -455,3 +452,55 @@
         assert repr(a) == "<_rawffi array %d of length %d>" % (a.buffer,
                                                                len(a))
         a.free()
+
+    def test_truncate(self):
+        import _rawffi
+        a = _rawffi.Array('b')(1)
+        a[0] = -5
+        assert a[0] == -5
+        a[0] = 123L
+        assert a[0] == 123
+        a[0] = 0x97817182ab128111111111111171817d042
+        assert a[0] == 0x42
+        a[0] = 255
+        assert a[0] == -1
+        a[0] = -2
+        assert a[0] == -2
+        a[0] = -255
+        assert a[0] == 1
+        a.free()
+
+        a = _rawffi.Array('B')(1)
+        a[0] = 123L
+        assert a[0] == 123
+        a[0] = 0x18329b1718b97d89b7198db817d042
+        assert a[0] == 0x42
+        a[0] = 255
+        assert a[0] == 255
+        a[0] = -2
+        assert a[0] == 254
+        a[0] = -255
+        assert a[0] == 1
+        a.free()
+
+        a = _rawffi.Array('h')(1)
+        a[0] = 123L
+        assert a[0] == 123
+        a[0] = 0x9112cbc91bd91db19aaaaaaaaaaaaaa8170d42
+        assert a[0] == 0x0d42
+        a[0] = 65535
+        assert a[0] == -1
+        a[0] = -2
+        assert a[0] == -2
+        a[0] = -65535
+        assert a[0] == 1
+        a.free()
+
+        a = _rawffi.Array('H')(1)
+        a[0] = 123L
+        assert a[0] == 123
+        a[0] = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeee817d042
+        assert a[0] == 0xd042
+        a[0] = -2
+        assert a[0] == 65534
+        a.free()



More information about the Pypy-commit mailing list