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

hakanardo at codespeak.net hakanardo at codespeak.net
Tue Jul 13 08:28:55 CEST 2010


Author: hakanardo
Date: Tue Jul 13 08:28:53 2010
New Revision: 76171

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:
speedups and bugfix

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	Tue Jul 13 08:28:53 2010
@@ -125,7 +125,7 @@
         if not self._isarray(other):
             return NotImplemented
         if self.typecode == 'c':
-            return buffer(self._data) == buffer(other._data)
+            return buffer(self) == buffer(other)
         else:
             return self.tolist() == other.tolist()
 
@@ -133,7 +133,7 @@
         if not self._isarray(other):
             return NotImplemented
         if self.typecode == 'c':
-            return buffer(self._data) != buffer(other._data)
+            return buffer(self) != buffer(other)
         else:
             return self.tolist() != other.tolist()
 
@@ -141,7 +141,7 @@
         if not self._isarray(other):
             return NotImplemented
         if self.typecode == 'c':
-            return buffer(self._data) < buffer(other._data)
+            return buffer(self) < buffer(other)
         else:
             return self.tolist() < other.tolist()
 
@@ -149,7 +149,7 @@
         if not self._isarray(other):
             return NotImplemented
         if self.typecode == 'c':
-            return buffer(self._data) > buffer(other._data)
+            return buffer(self) > buffer(other)
         else:
             return self.tolist() > other.tolist()
 
@@ -157,7 +157,7 @@
         if not self._isarray(other):
             return NotImplemented
         if self.typecode == 'c':
-            return buffer(self._data) <= buffer(other._data)
+            return buffer(self) <= buffer(other)
         else:
             return self.tolist() <= other.tolist()
 
@@ -165,7 +165,7 @@
         if not self._isarray(other):
             return NotImplemented
         if self.typecode == 'c':
-            return buffer(self._data) >= buffer(other._data)
+            return buffer(self) >= buffer(other)
         else:
             return self.tolist() >= other.tolist()
 

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	Tue Jul 13 08:28:53 2010
@@ -245,15 +245,27 @@
                 if mytype.typecode != w_iterable.typecode:
                     msg = "can only extend with array of same kind"
                     raise OperationError(space.w_TypeError, space.wrap(msg))
-            w_iterator = space.iter(w_iterable)
-            while True:
-                try:
-                    w_item = space.next(w_iterator)
-                except OperationError, e:
-                    if not e.match(space, space.w_StopIteration):
-                        raise
-                    break
-                self.descr_append(w_item)
+                if isinstance(w_iterable, W_Array):
+                    oldlen = self.len
+                    new = w_iterable.len
+                    self.setlen(self.len + new)
+                    for i in range(new):
+                        self.buffer[oldlen + i] = w_iterable.buffer[i]
+                else:
+                    assert False
+            elif (isinstance(w_iterable, W_ListObject) or
+                  isinstance(w_iterable, W_TupleObject)):
+                self.descr_fromsequence(w_iterable)
+            else:
+                w_iterator = space.iter(w_iterable)
+                while True:
+                    try:
+                        w_item = space.next(w_iterator)
+                    except OperationError, e:
+                        if not e.match(space, space.w_StopIteration):
+                            raise
+                        break
+                    self.descr_append(w_item)
         descr_extend.unwrap_spec = ['self', W_Root]
 
         def descr_setslice(self, w_idx, w_item):

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	Tue Jul 13 08:28:53 2010
@@ -44,6 +44,10 @@
         assert a[2] == 'c'
         assert len(a) == 3
 
+        b = self.array('c', a)
+        assert a == b
+        raises(TypeError, self.array, 'i', a)
+
     def test_value_range(self):
         values = (-129, 128, -128, 127, 0, 255, -1, 256,
                   -32768, 32767, -32769, 32768, 65535, 65536,
@@ -218,6 +222,9 @@
         a = self.array('b', (1, 2))
         assert len(a) == 2 and a[0] == 1 and a[1] == 2
 
+        a.extend(a)
+        assert repr(a) == "array('b', [1, 2, 1, 2])"
+
     def test_fromunicode(self):
         raises(ValueError, self.array('i').fromunicode, unicode('hi'))
         a = self.array('u')
@@ -432,51 +439,55 @@
         assert repr(a) == "array('i', [8, 2, 9, 7])"
 
     def test_compare(self):
-        a = self.array('i', [1, 2, 3])
-        b = self.array('i', [1, 2, 3])
-        c = self.array('i', [1, 3, 2])
-
-        assert (a == a) is True
-        assert (a == b) is True
-        assert (b == a) is True
-        assert (a == c) is False
-        assert (c == a) is False
-
-        assert (a != a) is False
-        assert (a != b) is False
-        assert (b != a) is False
-        assert (a != c) is True
-        assert (c != a) is True
-
-        assert (a < a) is False
-        assert (a < b) is False
-        assert (b < a) is False
-        assert (a < c) is True
-        assert (c < a) is False
-
-        assert (a > a) is False
-        assert (a > b) is False
-        assert (b > a) is False
-        assert (a > c) is False
-        assert (c > a) is True
-
-        assert (a <= a) is True
-        assert (a <= b) is True
-        assert (b <= a) is True
-        assert (a <= c) is True
-        assert (c <= a) is False
-
-        assert (a >= a) is True
-        assert (a >= b) is True
-        assert (b >= a) is True
-        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
+        for v1,v2,tt in (([1, 2, 3], [1, 3, 2], 'bhilBHIL'),
+                         ('abc', 'acb', 'c'),
+                         (unicode('abc'), unicode('acb'), 'u')):
+            for t in tt:
+                a = self.array(t, v1)
+                b = self.array(t, v1)
+                c = self.array(t, v2)
+
+                assert (a == a) is True
+                assert (a == b) is True
+                assert (b == a) is True
+                assert (a == c) is False
+                assert (c == a) is False
+
+                assert (a != a) is False
+                assert (a != b) is False
+                assert (b != a) is False
+                assert (a != c) is True
+                assert (c != a) is True
+
+                assert (a < a) is False
+                assert (a < b) is False
+                assert (b < a) is False
+                assert (a < c) is True
+                assert (c < a) is False
+
+                assert (a > a) is False
+                assert (a > b) is False
+                assert (b > a) is False
+                assert (a > c) is False
+                assert (c > a) is True
+
+                assert (a <= a) is True
+                assert (a <= b) is True
+                assert (b <= a) is True
+                assert (a <= c) is True
+                assert (c <= a) is False
+
+                assert (a >= a) is True
+                assert (a >= b) is True
+                assert (b >= a) is True
+                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



More information about the Pypy-commit mailing list