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

hakanardo at codespeak.net hakanardo at codespeak.net
Mon Jul 5 17:24:20 CEST 2010


Author: hakanardo
Date: Mon Jul  5 17:24:18 2010
New Revision: 75836

Modified:
   pypy/branch/interplevel-array/pypy/module/array/interp_array.py
   pypy/branch/interplevel-array/pypy/module/array/test/test_array.py
Log:
fromfile, fromlist, fromstring and fromunicode

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 17:24:18 2010
@@ -169,7 +169,52 @@
 
     def descr_fromstring(self, s):
         import struct
+        if len(s)%self.itemsize !=0:
+            msg = 'string length not a multiple of item size'
+            raise OperationError(self.space.w_ValueError, self.space.wrap(msg))
+        for i in range(len(s)/self.itemsize):
+            p = i * self.itemsize
+            item=struct.unpack(self.typecode, s[p:p + self.itemsize])[0]
+            self.descr_append(self.space.wrap(item))
+    descr_fromstring.unwrap_spec = ['self', str]
+
+    def descr_fromfile(self, w_f, n):
+        space=self.space
+        size = n*self.itemsize
+        w_s = space.call_function(
+            space.getattr(w_f, space.wrap('read')),
+            space.wrap(size))
+        s=space.str_w(w_s)
+        if len(s) != size:
+            n = len(s) % self.itemsize
+            if n != 0: s = s[0:-(len(s) % self.itemsize)]
+            self.descr_fromstring(s)
+            msg='not enough items in file'
+            raise OperationError(self.space.w_EOFError, self.space.wrap(msg))
+        else:
+            self.descr_fromstring(s)
+    descr_fromfile.unwrap_spec = ['self', W_Root, int]
+
+    def descr_fromlist(self, w_lst):
+        oldbuf = self.buffer
+        oldlen = self.len
+        try:
+            self.descr_extend(w_lst)
+        except OperationError:
+            self.buffer = oldbuf
+            self.len = oldlen
+            raise
+    descr_fromlist.unwrap_spec = ['self', W_Root]
+
+    def descr_fromunicode(self, s):
+        if self.typecode != 'u':
+            msg = "fromunicode() may only be called on type 'u' arrays"
+            raise OperationError(self.space.w_ValueError, self.space.wrap(msg))
+        self.descr_extend(self.space.wrap(s))
+    descr_fromunicode.unwrap_spec = ['self', unicode]
+
         
+            
 
 def descr_itemsize(space, self):
     return space.wrap(self.itemsize)
@@ -181,14 +226,24 @@
     __len__     = interp2app(W_Array.descr_len),
     __getitem__ = interp2app(W_Array.descr_getitem),
     __setitem__ = interp2app(W_Array.descr_setitem),
-    itemsize    = GetSetProperty(descr_itemsize, cls=W_Array)
+    itemsize    = GetSetProperty(descr_itemsize, cls=W_Array),
+    fromstring  = interp2app(W_Array.descr_fromstring),
+    fromfile    = interp2app(W_Array.descr_fromfile),
+    fromlist    = interp2app(W_Array.descr_fromlist),
+    fromunicode = interp2app(W_Array.descr_fromunicode),
 )
 
 
 def array(space, typecode, w_initializer=None):
     a = W_Array(space, typecode)
     if w_initializer is not None:
-        if not space.is_w(w_initializer, space.w_None):
+        if space.is_w(space.type(w_initializer), space.w_str):
+            a.descr_fromstring(space.str_w(w_initializer))
+        elif space.is_w(space.type(w_initializer), space.w_unicode):
+            a.descr_fromunicode(space.unicode_w(w_initializer))
+        elif space.is_w(space.type(w_initializer), space.w_list):
+            a.descr_fromlist(w_initializer)
+        elif not space.is_w(w_initializer, space.w_None):
             a.descr_extend(w_initializer)  # FIXME: use fromlist, fromstring, ...
 
     return a

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 17:24:18 2010
@@ -127,3 +127,72 @@
                 assert a[0] == 1 and a[1] == v and a[2] == 3
             raises(OverflowError, a.append, -1)
             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)
+            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)
+            assert len(b) == 2 and b[0] == 0 and b[1] == 0            
+
+    def test_fromfile(self):
+        
+        class myfile(object):
+            def __init__(self, c, s):
+                self.c = c
+                self.s = s
+            def read(self,n):
+                return self.c*min(n,self.s)
+
+        f=myfile('\x00', 20)
+        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)
+        assert len(a)==2 and a[0]==1 and a[1]==1
+
+        a = self.array('h')
+        a.fromfile(myfile('\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)
+            assert len(a)==1 and a[0]==257
+
+
+    def test_fromlist(self):
+        a = self.array('b')
+        raises(OverflowError, a.fromlist, [1, 2, 400])
+        assert len(a) == 0
+
+        raises(OverflowError, a.extend, [1, 2, 400])
+        assert len(a) == 2 and a[0] == 1 and a[1] == 2
+
+        raises(OverflowError, self.array, 'b', [1, 2, 400])
+
+        a = self.array('b', [1, 2])
+        assert len(a) == 2 and a[0] == 1 and a[1] == 2
+
+    def test_fromunicode(self):
+        raises(ValueError, self.array('i').fromunicode, unicode('hi'))
+        a = self.array('u')
+        a.fromunicode(unicode('hi'))
+        assert len(a) == 2 and a[0] == 'h' and a[1]=='i'
+
+        b = self.array('u', unicode('hi'))
+        assert len(b) == 2 and b[0] == 'h' and b[1]=='i'
+        



More information about the Pypy-commit mailing list