Commits

Manuel Jacob  committed dca1c35

Also accept integers but return one-byte strings.

  • Participants
  • Parent commits 976cb01
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/module/_rawffi/interp_rawffi.py

 from pypy.interpreter.error import OperationError, wrap_oserror, operationerrfmt
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.objspace.std.stringtype import getbytevalue
 
 from rpython.rlib.clibffi import *
 from rpython.rtyper.lltypesystem import lltype, rffi
         push_func(add_arg, argdesc, rffi.cast(rffi.LONGDOUBLE,
                                               space.float_w(w_arg)))
     elif letter == "c":
-        s = space.str_w(w_arg)
-        if len(s) != 1:
-            raise OperationError(space.w_TypeError, w(
-                "Expected string of length one as character"))
-        val = s[0]
+        if space.isinstance_w(w_arg, space.w_int):
+            val = getbytevalue(space, w_arg)
+        else:
+            s = space.str_w(w_arg)
+            if len(s) != 1:
+                raise OperationError(space.w_TypeError, w(
+                    "Expected string of length one as character"))
+            val = s[0]
         push_func(add_arg, argdesc, val)
     elif letter == 'u':
         s = space.unicode_w(w_arg)

File pypy/module/_rawffi/test/test__rawffi.py

         assert lib.ptr(1, [], 'i')()[0] == 42
 
     def test_getchar(self):
-        def _bytestring_to_list(bytestring):
-            return [bytestring[i:i+1] for i in range(len(bytestring))]
         import _rawffi
         lib = _rawffi.CDLL(self.lib_name)
         get_char = lib.ptr('get_char', ['P', 'H'], 'c')
         A = _rawffi.Array('c')
         B = _rawffi.Array('H')
-        dupa = A(5, _bytestring_to_list(b'dupa'))
+        dupa = A(5, b'dupa')
         dupaptr = dupa.byptr()
         for i in range(4):
             intptr = B(1)
         assert buf[:8] == b'*' + b'\x00'*6 + b'a'
 
     def test_returning_str(self):
-        def _bytestring_to_list(bytestring):
-            return [bytestring[i:i+1] for i in range(len(bytestring))]
         import _rawffi
         lib = _rawffi.CDLL(self.lib_name)
         char_check = lib.ptr('char_check', ['c', 'c'], 's')
         assert _rawffi.charp2string(res[0]) == b'xxxxxx'
         assert _rawffi.charp2rawstring(res[0]) == b'xxxxxx'
         assert _rawffi.charp2rawstring(res[0], 3) == b'xxx'
-        a = A(6, _bytestring_to_list(b'xx\x00\x00xx'))
+        a = A(6, b'xx\x00\x00xx')
         assert _rawffi.charp2string(a.buffer) == b'xx'
         assert _rawffi.charp2rawstring(a.buffer, 4) == b'xx\x00\x00'
         arg1[0] = b'x'
         S2E = _rawffi.Structure([('bah', (EMPTY, 1))])
         S2E.get_ffi_type()     # does not hang
 
+    def test_char_array_int(self):
+        import _rawffi
+        A = _rawffi.Array('c')
+        a = A(1)
+        a[0] = b'a'
+        assert a[0] == b'a'
+        # also accept int but return bytestring
+        a[0] = 100
+        assert a[0] == b'd'
+        a.free()
+
+
 class AppTestAutoFree:
     spaceconfig = dict(usemodules=['_rawffi', 'struct'])
     
         assert oldnum == _rawffi._num_of_allocated_objects()
 
     def test_array_autofree(self):
-        def _bytestring_to_list(bytestring):
-            return [bytestring[i:i+1] for i in range(len(bytestring))]
         import gc, _rawffi
         gc.collect()
         oldnum = _rawffi._num_of_allocated_objects()
 
         A = _rawffi.Array('c')
-        a = A(6, _bytestring_to_list(b'xxyxx\x00'), autofree=True)
+        a = A(6, b'xxyxx\x00', autofree=True)
         assert _rawffi.charp2string(a.buffer) == b'xxyxx'
         a = None
         gc.collect()