Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r48222:1c7672de6deb
Date: 2011-10-19 00:57 +0200
http://bitbucket.org/pypy/pypy/changeset/1c7672de6deb/
Log: Various fixes for the array module
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
@@ -487,7 +487,7 @@
s = rffi.charp2str(charp_addr)
else:
s = rffi.charp2strn(charp_addr, maxlength)
- return space.wrap(s)
+ return space.wrapbytes(s)
@unwrap_spec(address=r_uint, maxlength=int)
def wcharp2unicode(space, address, maxlength=-1):
@@ -505,7 +505,7 @@
if maxlength == -1:
return charp2string(space, address)
s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, address), maxlength)
- return space.wrap(s)
+ return space.wrapbytes(s)
@unwrap_spec(address=r_uint, maxlength=int)
def wcharp2rawunicode(space, address, maxlength=-1):
diff --git a/pypy/module/array/interp_array.py
b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -36,15 +36,15 @@
if len(__args__.arguments_w) > 0:
w_initializer = __args__.arguments_w[0]
- if space.type(w_initializer) is space.w_str:
- a.fromstring(space.str_w(w_initializer))
+ if space.type(w_initializer) is space.w_bytes:
+ a.fromstring(space.bytes_w(w_initializer))
elif space.type(w_initializer) is space.w_list:
a.fromlist(w_initializer)
else:
a.extend(w_initializer, True)
break
else:
- msg = 'bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)'
+ msg = 'bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)'
raise OperationError(space.w_ValueError, space.wrap(msg))
return a
@@ -63,6 +63,7 @@
array_tolist = SMM('tolist', 1)
array_fromlist = SMM('fromlist', 2)
array_tostring = SMM('tostring', 1)
+array_tobytes = SMM('tobytes', 1)
array_fromstring = SMM('fromstring', 2)
array_tounicode = SMM('tounicode', 1)
array_fromunicode = SMM('fromunicode', 2)
@@ -123,7 +124,6 @@
return True
types = {
- 'c': TypeCode(lltype.Char, 'str_w'),
'u': TypeCode(lltype.UniChar, 'unicode_w'),
'b': TypeCode(rffi.SIGNEDCHAR, 'int_w', True, True),
'B': TypeCode(rffi.UCHAR, 'int_w', True),
@@ -513,16 +513,18 @@
self.fromlist(w_lst)
def array_fromstring__Array_ANY(space, self, w_s):
- self.fromstring(space.str_w(w_s))
+ self.fromstring(space.bytes_w(w_s))
+
+ def array_tobytes__Array(space, self):
+ cbuf = self.charbuf()
+ return self.space.wrapbytes(rffi.charpsize2str(cbuf, self.len *
mytype.bytes))
def array_tostring__Array(space, self):
- cbuf = self.charbuf()
- return self.space.wrap(rffi.charpsize2str(cbuf, self.len *
mytype.bytes))
+ space.warn("tostring() is deprecated. Use tobytes() instead.",
+ space.w_DeprecationWarning)
+ return array_tobytes__Array(space, self)
def array_fromfile__Array_ANY_ANY(space, self, w_f, w_n):
- if not isinstance(w_f, W_File):
- msg = "arg1 must be open file"
- raise OperationError(space.w_TypeError, space.wrap(msg))
n = space.int_w(w_n)
try:
@@ -530,23 +532,20 @@
except OverflowError:
raise MemoryError
w_item = space.call_method(w_f, 'read', space.wrap(size))
- item = space.str_w(w_item)
+ item = space.bytes_w(w_item)
if len(item) < size:
n = len(item) % self.itemsize
elems = max(0, len(item) - (len(item) % self.itemsize))
if n != 0:
item = item[0:elems]
- w_item = space.wrap(item)
+ w_item = space.wrapbytes(item)
array_fromstring__Array_ANY(space, self, w_item)
msg = "not enough items in file"
raise OperationError(space.w_EOFError, space.wrap(msg))
array_fromstring__Array_ANY(space, self, w_item)
def array_tofile__Array_ANY(space, self, w_f):
- if not isinstance(w_f, W_File):
- msg = "arg1 must be open file"
- raise OperationError(space.w_TypeError, space.wrap(msg))
- w_s = array_tostring__Array(space, self)
+ w_s = array_tobytes__Array(space, self)
space.call_method(w_f, 'write', w_s)
if mytype.typecode == 'u':
@@ -593,7 +592,7 @@
def array_reduce__Array(space, self):
if self.len > 0:
- w_s = array_tostring__Array(space, self)
+ w_s = array_tobytes__Array(space, self)
args = [space.wrap(mytype.typecode), w_s]
else:
args = [space.wrap(mytype.typecode)]
@@ -631,10 +630,6 @@
def repr__Array(space, self):
if self.len == 0:
return space.wrap("array('%s')" % self.typecode)
- elif self.typecode == "c":
- r = space.repr(array_tostring__Array(space, self))
- s = "array('%s', %s)" % (self.typecode, space.str_w(r))
- return space.wrap(s)
elif self.typecode == "u":
r = space.repr(array_tounicode__Array(space, self))
s = "array('%s', %s)" % (self.typecode, space.str_w(r))
diff --git a/pypy/module/array/test/test_array.py
b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -22,40 +22,20 @@
def test_ctor(self):
- assert len(self.array('c')) == 0
assert len(self.array('i')) == 0
raises(TypeError, self.array, 'hi')
raises(TypeError, self.array, 1)
raises(ValueError, self.array, 'q')
- a = self.array('c')
+ a = self.array('u')
raises(TypeError, a.append, 7)
- raises(TypeError, a.append, 'hi')
+ raises(TypeError, a.append, u'hi')
a.append('h')
assert a[0] == 'h'
assert type(a[0]) is str
assert len(a) == 1
- a = self.array('u')
- raises(TypeError, a.append, 7)
- raises(TypeError, a.append, u'hi')
- a.append(unicode('h'))
- assert a[0] == unicode('h')
- assert type(a[0]) is unicode
- assert len(a) == 1
-
- a = self.array('c', ('a', 'b', 'c'))
- assert a[0] == 'a'
- assert a[1] == 'b'
- assert a[2] == 'c'
- assert len(a) == 3
-
- b = self.array('c', a)
- assert len(b) == 3
- assert a == b
- raises(TypeError, self.array, 'i', a)
-
a = self.array('i', (1, 2, 3))
b = self.array('h', (1, 2, 3))
assert a == b
@@ -85,7 +65,7 @@
('H', ( 0, 56783, 65535), int),
('i', (-32768, 30535, 32767), int),
('I', ( 0, 56783, 65535), long),
- ('l', (-2 ** 32 / 2, 34, 2 ** 32 / 2 - 1), int),
+ ('l', (-2 ** 32 // 2, 34, 2 ** 32 // 2 - 1), int),
('L', (0, 3523532, 2 ** 32 - 1), long),
):
a = self.array(tc, ok)
@@ -122,7 +102,7 @@
assert a.tolist() == vals
a = self.array(tc.lower())
- vals = [-1 * (2 ** a.itemsize) / 2, (2 ** a.itemsize) / 2 - 1]
+ vals = [-1 * (2 ** a.itemsize) // 2, (2 ** a.itemsize) // 2 - 1]
a.fromlist(vals)
assert a.tolist() == vals
@@ -141,7 +121,7 @@
assert len(a) == len(values)
def test_itemsize(self):
- for t in 'cbB':
+ for t in 'bB':
assert(self.array(t).itemsize >= 1)
for t in 'uhHiI':
assert(self.array(t).itemsize >= 2)
@@ -154,11 +134,11 @@
for t in inttypes:
a = self.array(t, [1, 2, 3])
b = a.itemsize
- for v in (-2 ** (8 * b) / 2, 2 ** (8 * b) / 2 - 1):
+ for v in (-2 ** (8 * b) // 2, 2 ** (8 * b) // 2 - 1):
a[1] = v
assert a[0] == 1 and a[1] == v and a[2] == 3
- raises(OverflowError, a.append, -2 ** (8 * b) / 2 - 1)
- raises(OverflowError, a.append, 2 ** (8 * b) / 2)
+ raises(OverflowError, a.append, -2 ** (8 * b) // 2 - 1)
+ raises(OverflowError, a.append, 2 ** (8 * b) // 2)
a = self.array(t.upper(), [1, 2, 3])
b = a.itemsize
@@ -169,20 +149,16 @@
raises(OverflowError, a.append, 2 ** (8 * b))
def test_fromstring(self):
- a = self.array('c')
- a.fromstring('Hi!')
- assert a[0] == 'H' and a[1] == 'i' and a[2] == '!' and len(a) == 3
-
for t in 'bBhHiIlLfd':
a = self.array(t)
- a.fromstring('\x00' * a.itemsize * 2)
+ a.fromstring(b'\x00' * a.itemsize * 2)
assert len(a) == 2 and a[0] == 0 and a[1] == 0
if a.itemsize > 1:
- raises(ValueError, a.fromstring, '\x00' * (a.itemsize - 1))
- raises(ValueError, a.fromstring, '\x00' * (a.itemsize + 1))
- raises(ValueError, a.fromstring, '\x00' * (2 * a.itemsize - 1))
- raises(ValueError, a.fromstring, '\x00' * (2 * a.itemsize + 1))
- b = self.array(t, '\x00' * a.itemsize * 2)
+ raises(ValueError, a.fromstring, b'\x00' * (a.itemsize - 1))
+ raises(ValueError, a.fromstring, b'\x00' * (a.itemsize + 1))
+ raises(ValueError, a.fromstring, b'\x00' * (2 * a.itemsize -
1))
+ raises(ValueError, a.fromstring, b'\x00' * (2 * a.itemsize +
1))
+ b = self.array(t, b'\x00' * a.itemsize * 2)
assert len(b) == 2 and b[0] == 0 and b[1] == 0
def test_fromfile(self):
@@ -194,28 +170,28 @@
## def read(self,n):
## return self.c*min(n,self.s)
def myfile(c, s):
- f = open(self.tempfile, 'w')
+ f = open(self.tempfile, 'wb')
f.write(c * s)
f.close()
- return open(self.tempfile, 'r')
+ return open(self.tempfile, 'rb')
- f = myfile('\x00', 100)
+ f = myfile(b'\x00', 100)
for t in 'bBhHiIlLfd':
a = self.array(t)
a.fromfile(f, 2)
assert len(a) == 2 and a[0] == 0 and a[1] == 0
a = self.array('b')
- a.fromfile(myfile('\x01', 20), 2)
+ a.fromfile(myfile(b'\x01', 20), 2)
assert len(a) == 2 and a[0] == 1 and a[1] == 1
a = self.array('h')
- a.fromfile(myfile('\x01', 20), 2)
+ a.fromfile(myfile(b'\x01', 20), 2)
assert len(a) == 2 and a[0] == 257 and a[1] == 257
for i in (0, 1):
a = self.array('h')
- raises(EOFError, a.fromfile, myfile('\x01', 2 + i), 2)
+ raises(EOFError, a.fromfile, myfile(b'\x01', 2 + i), 2)
assert len(a) == 1 and a[0] == 257
def test_fromlist(self):
@@ -255,12 +231,12 @@
assert repr(a) == "array('b', [1, 2, 1, 2])"
def test_fromunicode(self):
- raises(ValueError, self.array('i').fromunicode, unicode('hi'))
+ raises(ValueError, self.array('i').fromunicode, 'hi')
a = self.array('u')
- a.fromunicode(unicode('hi'))
+ a.fromunicode('hi')
assert len(a) == 2 and a[0] == 'h' and a[1] == 'i'
- b = self.array('u', unicode('hi'))
+ b = self.array('u', 'hi')
assert len(b) == 2 and b[0] == 'h' and b[1] == 'i'
def test_sequence(self):
@@ -380,24 +356,23 @@
assert type(l) is list and len(l) == 3
assert a[0] == 1 and a[1] == 2 and a[2] == 3
- b = self.array('i', a.tostring())
+ b = self.array('i', a.tobytes())
assert len(b) == 3 and b[0] == 1 and b[1] == 2 and b[2] == 3
- assert self.array('c', ('h', 'i')).tostring() == 'hi'
a = self.array('i', [0, 0, 0])
- assert a.tostring() == '\x00' * 3 * a.itemsize
+ assert a.tobytes() == b'\x00' * 3 * a.itemsize
- s = self.array('i', [1, 2, 3]).tostring()
- assert '\x00' in s
- assert '\x01' in s
- assert '\x02' in s
- assert '\x03' in s
+ s = self.array('i', [1, 2, 3]).tobytes()
+ assert 0x00 in s
+ assert 0x01 in s
+ assert 0x02 in s
+ assert 0x03 in s
a = self.array('i', s)
assert a[0] == 1 and a[1] == 2 and a[2] == 3
from struct import unpack
values = (-129, 128, -128, 127, 0, 255, -1, 256, -32760, 32760)
- s = self.array('i', values).tostring()
+ s = self.array('i', values).tobytes()
fmt = 'i' * len(values)
a = unpack(fmt, s)
assert a == values
@@ -406,28 +381,27 @@
('BHILfd', (127, 0, 1, 7, 255, 169)),
('hilHILfd', (32760, 30123, 3422, 23244))):
for tc in tcodes:
- values += ((2 ** self.array(tc).itemsize) / 2 - 1, )
- s = self.array(tc, values).tostring()
+ values += ((2 ** self.array(tc).itemsize) // 2 - 1, )
+ s = self.array(tc, values).tobytes()
a = unpack(tc * len(values), s)
assert a == values
- f = open(self.tempfile, 'w')
- self.array('c', ('h', 'i')).tofile(f)
+ f = open(self.tempfile, 'wb')
+ self.array('b', (ord('h'), ord('i'))).tofile(f)
f.close()
- assert open(self.tempfile, 'r').readline() == 'hi'
+ assert open(self.tempfile, 'rb').readline() == b'hi'
- a = self.array('c')
- a.fromfile(open(self.tempfile, 'r'), 2)
- assert repr(a) == "array('c', 'hi')"
+ a = self.array('b')
+ a.fromfile(open(self.tempfile, 'rb'), 2)
+ assert repr(a) == "array('b', [104, 105])"
raises(ValueError, self.array('i').tounicode)
- assert self.array('u', unicode('hello')).tounicode() == \
- unicode('hello')
+ assert self.array('u', 'hello').tounicode() == 'hello'
def test_buffer(self):
- a = self.array('h', 'Hi')
+ a = self.array('h', b'Hi')
buf = buffer(a)
- assert buf[1] == 'i'
+ assert buf[1] == b'i'
#raises(TypeError, buf.__setitem__, 1, 'o')
def test_list_methods(self):
@@ -482,8 +456,7 @@
pass
for v1, v2, tt in (([1, 2, 3], [1, 3, 2], 'bhilBHIL'),
- ('abc', 'acb', 'c'),
- (unicode('abc'), unicode('acb'), 'u')):
+ ('abc', 'acb', 'u')):
for t in tt:
a = self.array(t, v1)
b = self.array(t, v1)
@@ -677,7 +650,7 @@
def __len__(self):
return 3
- def next(self):
+ def __next__(self):
self.n -= 1
if self.n < 0:
raise StopIteration
@@ -697,7 +670,7 @@
assert repr(a) == "array('i', [4, 3, 2, 1, 0])"
def test_type(self):
- for t in 'bBhHiIlLfdcu':
+ for t in 'bBhHiIlLfdu':
assert type(self.array(t)) is self.array
assert isinstance(self.array(t), self.array)
@@ -727,7 +700,8 @@
self.height = height
return self
- def _index(self, (x,y)):
+ def _index(self, xy):
+ x, y = xy
x = min(max(x, 0), self.width-1)
y = min(max(y, 0), self.height-1)
return y * self.width + x
@@ -755,7 +729,7 @@
self.append(7)
def fromstring(self, lst):
- self.append('8')
+ self.append(8)
def fromunicode(self, lst):
self.append(u'9')
@@ -763,8 +737,7 @@
def extend(self, lst):
self.append(10)
- assert repr(mya('c', 'hi')) == "array('c', 'hi')"
- assert repr(mya('u', u'hi')) == "array('u', u'hi')"
+ assert repr(mya('u', u'hi')) == "array('u', 'hi')"
assert repr(mya('i', [1, 2, 3])) == "array('i', [1, 2, 3])"
assert repr(mya('i', (1, 2, 3))) == "array('i', [1, 2, 3])"
@@ -772,13 +745,13 @@
a.fromlist([1, 2, 3])
assert repr(a) == "array('i', [7])"
- a = mya('c')
- a.fromstring('hi')
- assert repr(a) == "array('c', '8')"
+ a = mya('b')
+ a.fromstring(b'hi')
+ assert repr(a) == "array('b', [8])"
a = mya('u')
a.fromunicode(u'hi')
- assert repr(a) == "array('u', u'9')"
+ assert repr(a) == "array('u', '9')"
a = mya('i')
a.extend([1, 2, 3])
@@ -789,34 +762,33 @@
def tolist(self):
return 'list'
- def tostring(self):
+ def tobytes(self):
return 'str'
def tounicode(self):
return 'unicode'
assert mya('i', [1, 2, 3]).tolist() == 'list'
- assert mya('c', 'hi').tostring() == 'str'
- assert mya('u', u'hi').tounicode() == 'unicode'
+ assert mya('u', 'hi').tobytes() == 'str'
+ assert mya('u', 'hi').tounicode() == 'unicode'
- assert repr(mya('c', 'hi')) == "array('c', 'hi')"
- assert repr(mya('u', u'hi')) == "array('u', u'hi')"
+ assert repr(mya('u', u'hi')) == "array('u', 'hi')"
assert repr(mya('i', [1, 2, 3])) == "array('i', [1, 2, 3])"
assert repr(mya('i', (1, 2, 3))) == "array('i', [1, 2, 3])"
def test_unicode_outofrange(self):
- a = self.array('u', unicode(r'\x01\u263a\x00\ufeff', 'unicode-escape'))
- b = self.array('u', unicode(r'\x01\u263a\x00\ufeff', 'unicode-escape'))
+ a = self.array('u', '\x01\u263a\x00\ufeff')
+ b = self.array('u', '\x01\u263a\x00\ufeff')
b.byteswap()
assert a != b
def test_weakref(self):
import weakref
- a = self.array('c', 'Hi!')
+ a = self.array('u', 'Hi!')
r = weakref.ref(a)
assert r() is a
-class TestCPythonsOwnArray(BaseArrayTests):
+class DontTestCPythonsOwnArray(BaseArrayTests):
def setup_class(cls):
import array
@@ -842,13 +814,13 @@
def test_buffer_info(self):
- a = self.array('c', 'Hi!')
+ a = self.array('b', b'Hi!')
bi = a.buffer_info()
assert bi[0] != 0
assert bi[1] == 3
import _rawffi
data = _rawffi.charp2string(bi[0])
- assert data[0:3] == 'Hi!'
+ assert data[0:3] == b'Hi!'
def test_array_reverse_slice_assign_self(self):
a = self.array('b', range(4))
diff --git a/pypy/module/array/test/test_array_old.py
b/pypy/module/array/test/test_array_old.py
--- a/pypy/module/array/test/test_array_old.py
+++ b/pypy/module/array/test/test_array_old.py
@@ -12,9 +12,9 @@
native_sizes = {'l': struct.calcsize('l')}
def test_attributes(self):
- a = self.array.array('c')
- assert a.typecode == 'c'
- assert a.itemsize == 1
+ a = self.array.array('u')
+ assert a.typecode == 'u'
+ assert a.itemsize == 4
a = self.array.array('l')
assert a.typecode == 'l'
assert a.itemsize == self.native_sizes['l']
@@ -26,9 +26,9 @@
def test_unicode(self):
a = self.array.array('u')
- a.fromunicode(unichr(9999))
+ a.fromunicode(chr(9999))
assert len(a) == 1
- assert a.tolist() == [unichr(9999)]
+ assert a.tolist() == [chr(9999)]
def test_pickle(self):
import sys
@@ -51,11 +51,11 @@
self.args = args
self.kwds = kwds
- a = A('c', foo='bar')
- assert a.args == ('c',)
+ a = A('u', foo='bar')
+ assert a.args == ('u',)
assert a.kwds == {'foo': 'bar'}
- a = A('i', range(10), some=42)
- assert a.args == ('i', range(10))
+ a = A('i', list(range(10)), some=42)
+ assert a.args == ('i', list(range(10)))
assert a.kwds == {'some': 42}
raises(TypeError, A)
raises(TypeError, A, 42)
@@ -63,7 +63,7 @@
raises(TypeError, self.array.array, 'i', [], foo='bar')
-class TestCPythonsOwnArray(BaseArrayTests):
+class DontTestCPythonsOwnArray(BaseArrayTests):
def setup_class(cls):
import array
diff --git a/pypy/objspace/std/stringobject.py
b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -8,6 +8,7 @@
from pypy.objspace.std.inttype import wrapint
from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
from pypy.objspace.std import slicetype, newformat
+from pypy.objspace.std.intobject import W_IntObject
from pypy.objspace.std.listobject import W_ListObject
from pypy.objspace.std.noneobject import W_NoneObject
from pypy.objspace.std.tupleobject import W_TupleObject
@@ -435,6 +436,11 @@
sub = w_sub._value
return space.newbool(self.find(sub) >= 0)
+def contains__String_Int(space, w_self, w_char):
+ self = w_self._value
+ char = w_char.intval
+ return space.newbool(self.find(chr(char)) >= 0)
+
def str_find__String_String_ANY_ANY(space, w_self, w_sub, w_start, w_end):
(self, sub, start, end) = _convert_idx_params(space, w_self, w_sub,
w_start, w_end)
res = self.find(sub, start, end)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit