[pypy-commit] pypy py3k: Change API for _rawffi Arrays of type 'c': accept and return one-byte strings, like in default.

Manuel Jacob noreply at buildbot.pypy.org
Sun Feb 17 23:07:40 CET 2013


Author: Manuel Jacob
Branch: py3k
Changeset: r61376:df47657149c2
Date: 2013-02-17 16:59 +0100
http://bitbucket.org/pypy/pypy/changeset/df47657149c2/

Log:	Change API for _rawffi Arrays of type 'c': accept and return one-
	byte strings, like in default.

diff --git a/pypy/module/_rawffi/interp_rawffi.py b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -2,7 +2,6 @@
 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
@@ -55,7 +54,7 @@
     return intmask(ffi_type.c_size), intmask(ffi_type.c_alignment)
 
 LL_TYPEMAP = {
-    'c' : rffi.UCHAR,
+    'c' : rffi.CHAR,
     'u' : lltype.UniChar,
     'b' : rffi.SIGNEDCHAR,
     'B' : rffi.UCHAR,
@@ -334,7 +333,11 @@
         push_func(add_arg, argdesc, rffi.cast(rffi.LONGDOUBLE,
                                               space.float_w(w_arg)))
     elif letter == "c":
-        val = getbytevalue(space, w_arg)
+        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)
@@ -363,7 +366,9 @@
             if c in TYPEMAP_PTR_LETTERS:
                 res = func(add_arg, argdesc, rffi.VOIDP)
                 return space.wrap(rffi.cast(lltype.Unsigned, res))
-            elif c == 'q' or c == 'Q' or c == 'L' or c == 'c' or c == 'u':
+            elif c == 'c':
+                return space.wrapbytes(func(add_arg, argdesc, ll_type))
+            elif c == 'q' or c == 'Q' or c == 'L' or c == 'u':
                 return space.wrap(func(add_arg, argdesc, ll_type))
             elif c == 'f' or c == 'd' or c == 'g':
                 return space.wrap(float(func(add_arg, argdesc, ll_type)))
diff --git a/pypy/module/_rawffi/test/test__rawffi.py b/pypy/module/_rawffi/test/test__rawffi.py
--- a/pypy/module/_rawffi/test/test__rawffi.py
+++ b/pypy/module/_rawffi/test/test__rawffi.py
@@ -260,12 +260,14 @@
         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, b'dupa')
+        dupa = A(5, _bytestring_to_list(b'dupa'))
         dupaptr = dupa.byptr()
         for i in range(4):
             intptr = B(1)
@@ -289,7 +291,8 @@
         assert buf[:8] == b'*' + b'\x00'*6 + b'a'
 
     def test_returning_str(self):
-        py3k_skip('bytes vs unicode')
+        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')
@@ -302,7 +305,7 @@
         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, b'xx\x00\x00xx')
+        a = A(6, _bytestring_to_list(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'
@@ -1046,12 +1049,14 @@
         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, b'xxyxx\x00', autofree=True)
+        a = A(6, _bytestring_to_list(b'xxyxx\x00'), autofree=True)
         assert _rawffi.charp2string(a.buffer) == b'xxyxx'
         a = None
         gc.collect()


More information about the pypy-commit mailing list