[pypy-svn] r76021 - in pypy/branch/interplevel-array/pypy/module/array: . test
hakanardo at codespeak.net
hakanardo at codespeak.net
Thu Jul 8 13:28:12 CEST 2010
Author: hakanardo
Date: Thu Jul 8 13:28:11 2010
New Revision: 76021
Modified:
pypy/branch/interplevel-array/pypy/module/array/app_array.py
pypy/branch/interplevel-array/pypy/module/array/interp_array.py
pypy/branch/interplevel-array/pypy/module/array/test/test_array.py
Log:
some list methods
Modified: pypy/branch/interplevel-array/pypy/module/array/app_array.py
==============================================================================
--- pypy/branch/interplevel-array/pypy/module/array/app_array.py (original)
+++ pypy/branch/interplevel-array/pypy/module/array/app_array.py Thu Jul 8 13:28:11 2010
@@ -1,4 +1,5 @@
-
+import operator
+
if True:
def initiate(self, initializer):
if initializer is not None:
@@ -84,7 +85,34 @@
s+=struct.pack(self.typecode, self[i])
return s
-
+ def __repr__(self):
+ if len(self) == 0:
+ return "array('%s')" % self.typecode
+ elif self.typecode == "c":
+ return "array('%s', %s)" % (self.typecode, repr(self.tostring()))
+ elif self.typecode == "u":
+ return "array('%s', %s)" % (self.typecode, repr(self.tounicode()))
+ else:
+ return "array('%s', %s)" % (self.typecode, repr(self.tolist()))
+ ##### list methods
+ def count(self, x):
+ """Return number of occurences of x in the array."""
+ return operator.countOf(self, x)
+ def index(self, x):
+ """Return index of first occurence of x in the array."""
+ return operator.indexOf(self, x)
+
+ def remove(self, x):
+ """Remove the first occurence of x in the array."""
+ self.pop(self.index(x))
+
+ def reverse(self):
+ """Reverse the order of the items in the array."""
+ lst = self.tolist()
+ lst.reverse()
+ self._setlen(0)
+ self.fromlist(lst)
+
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 Thu Jul 8 13:28:11 2010
@@ -254,10 +254,19 @@
def descr_tostring(self):
pbuf = rffi.cast(rffi.CCHARP, self.buffer)
s = ''
- for i in range(self.len * self.itemsize):
+ i=0
+ while i < self.len * self.itemsize:
s += pbuf[i]
+ i+=1
return self.space.wrap(s)
+ def descr_buffer(self):
+ from pypy.interpreter.buffer import StringLikeBuffer
+ space = self.space
+ return space.wrap(StringLikeBuffer(space, self.descr_tostring()))
+ descr_buffer.unwrap_spec = ['self']
+
+
def descr_itemsize(space, self):
return space.wrap(self.itemsize)
@@ -280,16 +289,34 @@
fromstring = interp2app(W_Array.descr_fromstring),
fromunicode = appmethod('fromunicode'),
fromfile = appmethod('fromfile'),
+ read = appmethod('fromfile'),
_fromfile = appmethod('_fromfile'),
fromlist = appmethod('fromlist'),
tolist = interp2app(W_Array.descr_tolist),
tounicode = appmethod('tounicode'),
tofile = appmethod('tofile'),
+ write = appmethod('tofile'),
#tostring = appmethod('tostring'),
tostring = interp2app(W_Array.descr_tostring),
_setlen = interp2app(W_Array.setlen),
+ __buffer__ = interp2app(W_Array.descr_buffer),
+
+ __repr__ = appmethod('__repr__'),
+ count = appmethod('count'),
+ index = appmethod('index'),
+ remove = appmethod('remove'),
+ reverse = appmethod('reverse'),
+
+
+ # TODO:
+ # __cmp__
+ #byteswap =
+ #buffer_info =
+ #__copy__ =
+ #__reduce__ =
+ # insert, pop,
)
mytype.w_class = W_Array
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 Thu Jul 8 13:28:11 2010
@@ -310,6 +310,31 @@
raises(ValueError, self.array('i').tounicode)
assert self.array('u', unicode('hello')).tounicode() == unicode('hello')
+ def test_buffer(self):
+ assert buffer(self.array('h', 'Hi'))[1] == 'i'
+
+ def test_list_methods(self):
+ assert repr(self.array('i')) == "array('i')"
+ assert repr(self.array('i', [1, 2, 3])) == "array('i', [1, 2, 3])"
+ assert repr(self.array('h')) == "array('h')"
+
+ a=self.array('i', [1, 2, 3, 1, 2, 1])
+ assert a.count(1) == 3
+ assert a.count(2) == 2
+ assert a.index(3) == 2
+ assert a.index(2) == 1
+
+ a.reverse()
+ assert repr(a) == "array('i', [1, 2, 1, 3, 2, 1])"
+
+ if False:
+ a.remove(3)
+ assert repr(a) == "array('i', [1, 2, 1, 2, 1])"
+ a.remove(1)
+ assert repr(a) == "array('i', [2, 1, 2, 1])"
+
+
+
#FIXME
#def test_type(self):
# for t in 'bBhHiIlLfdcu':
More information about the Pypy-commit
mailing list