[pypy-commit] pypy py3k: Also accept integers but return one-byte strings.

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


Author: Manuel Jacob
Branch: py3k
Changeset: r61378:dca1c3574ab8
Date: 2013-02-17 18:51 +0100
http://bitbucket.org/pypy/pypy/changeset/dca1c3574ab8/

Log:	Also accept integers but return one-byte strings.

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,6 +2,7 @@
 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
@@ -333,11 +334,14 @@
         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)
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,14 +260,12 @@
         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)
@@ -291,8 +289,6 @@
         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')
@@ -305,7 +301,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, _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'
@@ -1028,6 +1024,18 @@
         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'])
     
@@ -1049,14 +1057,12 @@
         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()


More information about the pypy-commit mailing list