[pypy-svn] r76084 - in pypy/branch/interplevel-array/pypy/module/array: . test

hakanardo at codespeak.net hakanardo at codespeak.net
Fri Jul 9 20:49:45 CEST 2010


Author: hakanardo
Date: Fri Jul  9 20:49:44 2010
New Revision: 76084

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:
a few more 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	Fri Jul  9 20:49:44 2010
@@ -116,6 +116,12 @@
         self._setlen(0)
         self.fromlist(lst)
 
+    def insert(self, i, x):
+        lst=self.tolist()
+        lst.insert(i, x)
+        self._setlen(0)
+        self.fromlist(lst)
+
     def __eq__(self, other):
         if not self._isarray(other):
             return NotImplemented

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	Fri Jul  9 20:49:44 2010
@@ -234,6 +234,9 @@
                 self.descr_setslice(start, stop, step, w_item)
         descr_setitem.unwrap_spec = ['self', W_Root, W_Root]
 
+        def charbuf(self):
+            return  rffi.cast(rffi.CCHARP, self.buffer)
+
         def descr_fromstring(self, s):
             if len(s)%mytype.bytes !=0:
                 msg = 'string length not a multiple of item size'
@@ -248,7 +251,7 @@
                     #self.buffer[oldlen + i]=self.item_w(self.space.wrap(item))
                     self.buffer[oldlen + i]=rffi.cast(mytype.itemtype, item)
             else:
-                pbuf = rffi.cast(rffi.CCHARP, self.buffer)
+                pbuf =self.charbuf()
                 for i in range(len(s)):
                     pbuf[oldlen * mytype.bytes + i] = s[i]
                     
@@ -263,10 +266,10 @@
         descr_tolist.unwrap_spec = ['self']
 
         def descr_tostring(self):
-            pbuf = rffi.cast(rffi.CCHARP, self.buffer)
+            pbuf = self.charbuf()
             s = ''
             i=0
-            while i < self.len * self.itemsize:
+            while i < self.len * mytype.bytes:
                 s += pbuf[i]
                 i+=1
             return self.space.wrap(s)
@@ -292,12 +295,51 @@
             w_new_inst = mod.get('array')
             return space.newtuple([w_new_inst, space.newtuple(args)])
 
+        def descr_pop(self, i=-1):
+            if i < 0: i += self.len
+            if i < 0 or i >= self.len:
+                msg = 'pop index out of range'
+                raise OperationError(self.space.w_IndexError, self.space.wrap(msg))
+            val = self.buffer[i]
+            while i < self.len - 1:
+                self.buffer[i] = self.buffer[i + 1]
+                i += 1
+            self.setlen(self.len-1)
+            return self.space.wrap(val)
+        descr_pop.unwrap_spec = ['self', int]
+
+
+        def descr_copy(self):
+            w_a=mytype.w_class(self.space)
+            w_a.descr_fromsequence(self)
+            return w_a
+
+        def descr_buffer_info(self):
+            space = self.space
+            w_ptr = space.wrap(rffi.cast(lltype.Unsigned, self.buffer))
+            w_len = space.wrap(self.len)
+            return space.newtuple([w_ptr, w_len])
+
+        def descr_byteswap(self):
+            if mytype.bytes not in [1, 2, 4, 8]:
+                msg="byteswap not supported for this array"
+                raise OperationError(self.space.w_RuntimeError, self.space.wrap(msg))
+            bytes = self.charbuf()
+            tmp = [0] * mytype.bytes
+            for start in range(0, self.len * mytype.bytes, mytype.bytes):
+                stop = start + mytype.bytes - 1
+                for i in range(mytype.bytes):
+                    tmp[i] = bytes[start + i]
+                for i in range(mytype.bytes):
+                    bytes[stop - i] = tmp[i]
+
+
 
 
     def descr_itemsize(space, self):
-        return space.wrap(self.itemsize)
+        return space.wrap(mytype.bytes)
     def descr_typecode(space, self):
-        return space.wrap(self.typecode)
+        return space.wrap(mytype.typecode)
 
     W_Array.__name__ = 'W_ArrayType_'+mytype.typecode
     W_Array.typedef = TypeDef(
@@ -334,6 +376,8 @@
         index        = appmethod('index'),
         remove       = appmethod('remove'),
         reverse      = appmethod('reverse'),
+        insert       = appmethod('insert'),
+        pop          = interp2app(W_Array.descr_pop),
 
         __eq__       = appmethod('__eq__'),
         __ne__       = appmethod('__ne__'),
@@ -344,15 +388,11 @@
         
         _isarray     = interp2app(W_Array.descr_isarray),
         __reduce__   = interp2app(W_Array.descr_reduce),
+        __copy__     = interp2app(W_Array.descr_copy),
+
+        buffer_info  = interp2app(W_Array.descr_buffer_info),
         
-        
-        # TODO:
-        # __cmp__
-        #byteswap     =
-        #buffer_info  =
-        #__copy__     =
-        #__reduce__   =
-        # insert, pop, 
+        byteswap     = interp2app(W_Array.descr_byteswap),
     )
 
     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	Fri Jul  9 20:49:44 2010
@@ -337,8 +337,12 @@
         assert self.array('u', unicode('hello')).tounicode() == unicode('hello')
 
     def test_buffer(self):
-        assert buffer(self.array('h', 'Hi'))[1] == 'i'
-
+        a = self.array('h', 'Hi')
+        buf = buffer(a)
+        assert buf[1] == 'i'
+        raises(TypeError, buf.__setitem__, 1, 'o')
+        
+        
     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])"
@@ -353,11 +357,24 @@
         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])"
+        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])"
+
+        a.pop()
+        assert repr(a) == "array('i', [2, 1, 2])"
+
+        a.pop(1)
+        assert repr(a) == "array('i', [2, 2])"
+
+        a.pop(-2)
+        assert repr(a) == "array('i', [2])"
+
+        a.insert(1,7)
+        a.insert(0,8)
+        a.insert(-1,9)
+        assert repr(a) == "array('i', [8, 2, 9, 7])"
 
     def test_compare(self):
         a = self.array('i', [1, 2, 3])
@@ -400,6 +417,12 @@
         assert (a >= c) is False
         assert (c >= a) is True
 
+        assert cmp(a, a) == 0
+        assert cmp(a, b) == 0
+        assert cmp(a, c) <  0
+        assert cmp(b, a) == 0
+        assert cmp(c, a) >  0
+        
     def test_reduce(self):
         import pickle
         a = self.array('i', [1, 2, 3])
@@ -412,6 +435,31 @@
         b = pickle.loads(s)
         assert len(b) == 0 and b.typecode == 'l'
 
+    def test_misc(self):
+        a = self.array('i', [1, 2, 3])
+        from copy import copy
+        b = copy(a)
+        a[1] = 7
+        assert repr(b) == "array('i', [1, 2, 3])"
+
+        bi=b.buffer_info()
+        assert bi[0] != 0 # FIXME: How can the address be tested?
+        assert bi[1] == 3
+
+        for tc in 'bhilBHIL':
+            a=self.array(tc, [1, 2, 3])
+            a.byteswap()
+            assert len(a)==3
+            assert a[0] == 1 * (256 ** (a.itemsize-1))
+            assert a[1] == 2 * (256 ** (a.itemsize-1))
+            assert a[2] == 3 * (256 ** (a.itemsize-1))
+            a.byteswap()
+            assert len(a)==3
+            assert a[0] == 1
+            assert a[1] == 2
+            assert a[2] == 3
+            
+
     #FIXME
     #def test_type(self):
     #    for t in 'bBhHiIlLfdcu':



More information about the Pypy-commit mailing list