[pypy-svn] r75859 - in pypy/branch/interplevel-array/pypy/module/array: . test
hakanardo at codespeak.net
hakanardo at codespeak.net
Mon Jul 5 21:29:42 CEST 2010
Author: hakanardo
Date: Mon Jul 5 21:29:40 2010
New Revision: 75859
Modified:
pypy/branch/interplevel-array/pypy/module/array/interp_array.py
pypy/branch/interplevel-array/pypy/module/array/test/test_array.py
Log:
sequence interface
Modified: pypy/branch/interplevel-array/pypy/module/array/interp_array.py
==============================================================================
--- pypy/branch/interplevel-array/pypy/module/array/interp_array.py (original)
+++ pypy/branch/interplevel-array/pypy/module/array/interp_array.py Mon Jul 5 21:29:40 2010
@@ -149,19 +149,52 @@
self.descr_append(w_item)
descr_extend.unwrap_spec = ['self', W_Root]
- def descr_getitem(self, idx):
- item = self.buffer[idx]
- if self.typecode in ('b', 'B', 'h', 'H', 'i', 'l'):
- item = rffi.cast(lltype.Signed, item)
- if self.typecode == 'f':
- item = float(item)
- return self.space.wrap(item)
- descr_getitem.unwrap_spec = ['self', int]
-
- def descr_setitem(self, idx, w_item):
- item = self.item_w(w_item)
- self.buffer[idx] = item
- descr_setitem.unwrap_spec = ['self', int, W_Root]
+ def descr_getitem(self, w_idx):
+ space=self.space
+ start, stop, step = space.decode_index(w_idx, self.len)
+ if step==0:
+ item = self.buffer[start]
+ if self.typecode in ('b', 'B', 'h', 'H', 'i', 'l'):
+ item = rffi.cast(lltype.Signed, item)
+ elif self.typecode == 'f':
+ item = float(item)
+ return self.space.wrap(item)
+ else:
+ size = (stop - start) / step
+ if (stop - start) % step > 0: size += 1
+ w_a=W_Array(self.space, self.typecode)
+ w_a.setlen(size)
+ j=0
+ for i in range(start, stop, step):
+ w_a.buffer[j]=self.buffer[i]
+ j+=1
+ return w_a
+ descr_getitem.unwrap_spec = ['self', W_Root]
+
+ def descr_setitem(self, w_idx, w_item):
+ start, stop, step = self.space.decode_index(w_idx, self.len)
+ if step==0:
+ item = self.item_w(w_item)
+ self.buffer[start] = item
+ else:
+ if isinstance(w_item, W_Array):
+ if self.typecode == w_item.typecode:
+ size = (stop - start) / step
+ if (stop - start) % step > 0: size += 1
+ if w_item.len != size:
+ msg = ('attempt to assign array of size %d to ' +
+ 'slice of size %d') % (w_item.len, size)
+ raise OperationError(self.space.w_ValueError,
+ self.space.wrap(msg))
+ j=0
+ for i in range(start, stop, step):
+ self.buffer[i]=w_item.buffer[j]
+ j+=1
+ return
+ msg='can only assign array to array slice'
+ raise OperationError(self.space.w_TypeError, self.space.wrap(msg))
+
+ descr_setitem.unwrap_spec = ['self', W_Root, W_Root]
def descr_len(self):
return self.space.wrap(self.len)
@@ -178,7 +211,7 @@
for i in range(new):
p = i * self.itemsize
item=struct.unpack(self.typecode, s[p:p + self.itemsize])[0]
- self.descr_setitem(oldlen + i, self.space.wrap(item))
+ self.buffer[oldlen + i]=self.item_w(self.space.wrap(item))
descr_fromstring.unwrap_spec = ['self', str]
def descr_fromfile(self, w_f, n):
@@ -207,7 +240,7 @@
self.setlen(oldlen+new)
for i in range(new):
w_item=space.getitem(w_lst, space.wrap(i))
- self.descr_setitem(oldlen + i, w_item)
+ self.buffer[oldlen + i] = self.item_w(w_item)
except OperationError:
self.buffer = oldbuf
self.len = oldlen
Modified: pypy/branch/interplevel-array/pypy/module/array/test/test_array.py
==============================================================================
--- pypy/branch/interplevel-array/pypy/module/array/test/test_array.py (original)
+++ pypy/branch/interplevel-array/pypy/module/array/test/test_array.py Mon Jul 5 21:29:40 2010
@@ -208,3 +208,58 @@
b = self.array('u', unicode('hi'))
assert len(b) == 2 and b[0] == 'h' and b[1]=='i'
+ def test_sequence(self):
+ a=self.array('i', [1,2,3,4])
+ assert len(a)==4
+ assert a[0] == 1 and a[1] == 2 and a[2] == 3 and a[3] == 4
+ assert a[-4] == 1 and a[-3] == 2 and a[-2] == 3 and a[-1] == 4
+ a[-2]=5
+ assert a[0] == 1 and a[1] == 2 and a[2] == 5 and a[3] == 4
+
+ for i in (4, -5): raises(IndexError, a.__getitem__, i)
+
+ b = a[0:2]
+ assert len(b) == 2 and b[0] == 1 and b[1] == 2
+ b[0]=6
+ assert len(b) == 2 and b[0] == 6 and b[1] == 2
+ assert a[0] == 1 and a[1] == 2 and a[2] == 5 and a[3] == 4
+ assert a.itemsize == b.itemsize
+
+ b = a[0:100]
+ assert len(b)==4
+ assert b[0] == 1 and b[1] == 2 and b[2] == 5 and b[3] == 4
+
+ l1 = [2 * i + 1 for i in range(10)]
+ a1 = self.array('i', l1)
+ for start in range(10):
+ for stop in range(start, 10):
+ for step in range(1,10):
+ l2 = l1[start:stop:step]
+ a2 = a1[start:stop:step]
+ assert len(l2) == len(a2)
+ for i in range(len(l2)): assert l2[i] == a2[i]
+
+ a=self.array('i', [1,2,3,4])
+ a[1:3]=self.array('i', [5,6])
+ assert len(a)==4
+ assert a[0] == 1 and a[1] == 5 and a[2] == 6 and a[3] == 4
+ a[0:-1:2]=self.array('i', [7,8])
+ assert a[0] == 7 and a[1] == 5 and a[2] == 8 and a[3] == 4
+
+ try:
+ a[1:2:4]=self.array('i', [5,6,7])
+ assert False
+ except ValueError:
+ pass
+
+ try:
+ a[1:3]=self.array('I', [5,6])
+ assert False
+ except TypeError:
+ pass
+
+ try:
+ a[1:3]=[5,6]
+ assert False
+ except TypeError:
+ pass
More information about the Pypy-commit
mailing list