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

hakanardo at codespeak.net hakanardo at codespeak.net
Thu Jul 8 15:34:05 CEST 2010


Author: hakanardo
Date: Thu Jul  8 15:34:03 2010
New Revision: 76024

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:
pickling and comparing

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 15:34:03 2010
@@ -116,3 +116,51 @@
         self._setlen(0)
         self.fromlist(lst)
 
+    def __eq__(self, other):
+        if not self._isarray(other):
+            return NotImplemented
+        if self.typecode == 'c':
+            return buffer(self._data) == buffer(other._data)
+        else:
+            return self.tolist() == other.tolist()
+
+    def __ne__(self, other):
+        if not self._isarray(other):
+            return NotImplemented
+        if self.typecode == 'c':
+            return buffer(self._data) != buffer(other._data)
+        else:
+            return self.tolist() != other.tolist()
+
+    def __lt__(self, other):
+        if not self._isarray(other):
+            return NotImplemented
+        if self.typecode == 'c':
+            return buffer(self._data) < buffer(other._data)
+        else:
+            return self.tolist() < other.tolist()
+
+    def __gt__(self, other):
+        if not self._isarray(other):
+            return NotImplemented
+        if self.typecode == 'c':
+            return buffer(self._data) > buffer(other._data)
+        else:
+            return self.tolist() > other.tolist()
+
+    def __le__(self, other):
+        if not self._isarray(other):
+            return NotImplemented
+        if self.typecode == 'c':
+            return buffer(self._data) <= buffer(other._data)
+        else:
+            return self.tolist() <= other.tolist()
+
+    def __ge__(self, other):
+        if not self._isarray(other):
+            return NotImplemented
+        if self.typecode == 'c':
+            return buffer(self._data) >= buffer(other._data)
+        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	Thu Jul  8 15:34:03 2010
@@ -265,7 +265,22 @@
             space = self.space
             return space.wrap(StringLikeBuffer(space, self.descr_tostring()))
         descr_buffer.unwrap_spec = ['self']
-        
+
+        def descr_isarray(self, w_other):
+            return self.space.wrap(isinstance(w_other, W_ArrayBase))
+
+        def descr_reduce(self):
+            space=self.space
+            if self.len>0:
+                args=[space.wrap(self.typecode), self.descr_tostring()]
+            else:
+                args=[space.wrap(self.typecode)]
+            from pypy.interpreter.mixedmodule import MixedModule
+            w_mod    = space.getbuiltinmodule('array')
+            mod      = space.interp_w(MixedModule, w_mod)
+            w_new_inst = mod.get('array')
+            return space.newtuple([w_new_inst, space.newtuple(args)])
+
 
 
     def descr_itemsize(space, self):
@@ -308,6 +323,16 @@
         index        = appmethod('index'),
         remove       = appmethod('remove'),
         reverse      = appmethod('reverse'),
+
+        __eq__       = appmethod('__eq__'),
+        __ne__       = appmethod('__ne__'),
+        __lt__       = appmethod('__lt__'),
+        __gt__       = appmethod('__gt__'),
+        __le__       = appmethod('__le__'),
+        __ge__       = appmethod('__ge__'),
+        
+        _isarray     = interp2app(W_Array.descr_isarray),
+        __reduce__   = interp2app(W_Array.descr_reduce),
         
         
         # TODO:

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 15:34:03 2010
@@ -332,8 +332,59 @@
             assert repr(a) == "array('i', [1, 2, 1, 2, 1])"
             a.remove(1)
             assert repr(a) == "array('i', [2, 1, 2, 1])"
-        
-        
+
+    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
+
+    def test_reduce(self):
+        import pickle
+        a = self.array('i', [1, 2, 3])
+        s = pickle.dumps(a,1)
+        b = pickle.loads(s)
+        assert a == b
+
+        a = self.array('l')
+        s = pickle.dumps(a,1)
+        b = pickle.loads(s)
+        assert len(b) == 0 and b.typecode == 'l'
 
     #FIXME
     #def test_type(self):



More information about the Pypy-commit mailing list