Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58822:cc904f324dc1
Date: 2012-11-10 18:16 +0100
http://bitbucket.org/pypy/pypy/changeset/cc904f324dc1/
Log: _rawffi.Array('c') now store char as numbers, like bytes 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 pypy.rlib.clibffi import *
from pypy.rpython.lltypesystem import lltype, rffi
@@ -54,7 +55,7 @@
return intmask(ffi_type.c_size), intmask(ffi_type.c_alignment)
LL_TYPEMAP = {
- 'c' : rffi.CHAR,
+ 'c' : rffi.UCHAR,
'u' : lltype.UniChar,
'b' : rffi.SIGNEDCHAR,
'B' : rffi.UCHAR,
@@ -324,11 +325,7 @@
push_func(add_arg, argdesc, rffi.cast(rffi.LONGDOUBLE,
space.float_w(w_arg)))
elif letter == "c":
- s = space.bytes_w(w_arg)
- if len(s) != 1:
- raise OperationError(space.w_TypeError, w(
- "Expected string of length one as character"))
- val = s[0]
+ val = getbytevalue(space, w_arg)
push_func(add_arg, argdesc, val)
elif letter == 'u':
s = space.unicode_w(w_arg)
@@ -357,9 +354,7 @@
if c in TYPEMAP_PTR_LETTERS:
res = func(add_arg, argdesc, rffi.VOIDP)
return space.wrap(rffi.cast(lltype.Unsigned, res))
- elif c == 'c':
- return space.wrapbytes(func(add_arg, argdesc, ll_type))
- elif c == 'q' or c == 'Q' or c == 'L' or c == 'u':
+ elif c == 'q' or c == 'Q' or c == 'L' or c == 'c' 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
@@ -282,10 +282,10 @@
import _rawffi
A = _rawffi.Array('c')
buf = A(10, autofree=True)
- buf[0] = b'*'
+ buf[0] = ord('*')
assert buf[1:5] == b'\x00' * 4
buf[7:] = b'abc'
- assert buf[9] == b'c'
+ assert buf[9] == ord('c')
assert buf[:8] == b'*' + b'\x00'*6 + b'a'
def test_returning_str(self):
@@ -450,13 +450,13 @@
X = _rawffi.Structure([('x1', 'i'), ('x2', 'h'), ('x3', 'c'), ('next',
'P')])
next = X()
next.next = 0
- next.x3 = b'x'
+ next.x3 = ord('x')
x = X()
x.next = next
x.x1 = 1
x.x2 = 2
- x.x3 = b'x'
- assert X.fromaddress(x.next).x3 == b'x'
+ x.x3 = ord('x')
+ assert X.fromaddress(x.next).x3 == ord('x')
x.free()
next.free()
create_double_struct = lib.ptr("create_double_struct", [], 'P')
@@ -770,16 +770,10 @@
b = _rawffi.Array('c').fromaddress(a.buffer, 38)
if sys.maxunicode > 65535:
# UCS4 build
- assert b[0] == b'x'
- assert b[1] == b'\x00'
- assert b[2] == b'\x00'
- assert b[3] == b'\x00'
- assert b[4] == b'y'
+ assert b[0:5] == b'x\x00\x00\x00y'
else:
# UCS2 build
- assert b[0] == b'x'
- assert b[1] == b'\x00'
- assert b[2] == b'y'
+ assert b[0:2] == b'x\x00y'
a.free()
def test_truncate(self):
@@ -1003,15 +997,15 @@
A = _rawffi.Array('c')
a = A(10, autofree=True)
- a[3] = b'x'
+ a[3] = ord('x')
b = memoryview(a)
assert len(b) == 10
assert b[3] == b'x'
b[6] = b'y'
- assert a[6] == b'y'
+ assert a[6] == ord('y')
b[3:5] = b'zt'
- assert a[3] == b'z'
- assert a[4] == b't'
+ assert a[3] == ord('z')
+ assert a[4] == ord('t')
def test_union(self):
import _rawffi
@@ -1052,14 +1046,13 @@
assert oldnum == _rawffi._num_of_allocated_objects()
def test_array_autofree(self):
- py3k_skip('bytes vs unicode')
import gc, _rawffi
gc.collect()
oldnum = _rawffi._num_of_allocated_objects()
A = _rawffi.Array('c')
a = A(6, b'xxyxx\x00', autofree=True)
- assert _rawffi.charp2string(a.buffer) == 'xxyxx'
+ assert _rawffi.charp2string(a.buffer) == b'xxyxx'
a = None
gc.collect()
assert oldnum == _rawffi._num_of_allocated_objects()
diff --git a/pypy/module/_rawffi/test/test_nested.py
b/pypy/module/_rawffi/test/test_nested.py
--- a/pypy/module/_rawffi/test/test_nested.py
+++ b/pypy/module/_rawffi/test/test_nested.py
@@ -43,14 +43,14 @@
assert S.fieldoffset('x') == 0
assert S.fieldoffset('s1') == S1.alignment
s = S()
- s.x = b'G'
+ s.x = ord('G')
raises(TypeError, 's.s1')
assert s.fieldaddress('s1') == s.buffer + S.fieldoffset('s1')
s1 = S1.fromaddress(s.fieldaddress('s1'))
- s1.c = b'H'
+ s1.c = ord('H')
rawbuf = _rawffi.Array('c').fromaddress(s.buffer, S.size)
- assert rawbuf[0] == b'G'
- assert rawbuf[S1.alignment + S1.fieldoffset('c')] == b'H'
+ assert rawbuf[0] == ord('G')
+ assert rawbuf[S1.alignment + S1.fieldoffset('c')] == ord('H')
s.free()
def test_array_of_structures(self):
@@ -60,17 +60,17 @@
a = A(3)
raises(TypeError, "a[0]")
s0 = S.fromaddress(a.buffer)
- s0.c = b'B'
+ s0.c = ord('B')
assert a.itemaddress(1) == a.buffer + S.size
s1 = S.fromaddress(a.itemaddress(1))
- s1.c = b'A'
+ s1.c = ord('A')
s2 = S.fromaddress(a.itemaddress(2))
- s2.c = b'Z'
+ s2.c = ord('Z')
rawbuf = _rawffi.Array('c').fromaddress(a.buffer, S.size * len(a))
ofs = S.fieldoffset('c')
- assert rawbuf[0*S.size+ofs] == b'B'
- assert rawbuf[1*S.size+ofs] == b'A'
- assert rawbuf[2*S.size+ofs] == b'Z'
+ assert rawbuf[0*S.size+ofs] == ord('B')
+ assert rawbuf[1*S.size+ofs] == ord('A')
+ assert rawbuf[2*S.size+ofs] == ord('Z')
a.free()
def test_array_of_array(self):
@@ -103,16 +103,16 @@
assert S.fieldoffset('x') == 0
assert S.fieldoffset('ar') == A5alignment
s = S()
- s.x = b'G'
+ s.x = ord('G')
raises(TypeError, 's.ar')
assert s.fieldaddress('ar') == s.buffer + S.fieldoffset('ar')
a1 = A.fromaddress(s.fieldaddress('ar'), 5)
a1[4] = 33
rawbuf = _rawffi.Array('c').fromaddress(s.buffer, S.size)
- assert rawbuf[0] == b'G'
+ assert rawbuf[0] == ord('G')
sizeofint = struct.calcsize("i")
v = 0
for i in range(sizeofint):
- v += ord(rawbuf[A5alignment + sizeofint*4+i])
+ v += rawbuf[A5alignment + sizeofint*4+i]
assert v == 33
s.free()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit