[pypy-svn] r71868 - in pypy/branch/micronumpy/pypy/module/micronumpy: . test

dan at codespeak.net dan at codespeak.net
Sat Mar 6 21:03:17 CET 2010


Author: dan
Date: Sat Mar  6 21:02:19 2010
New Revision: 71868

Modified:
   pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
Log:
Added basic sdarray setitem testing, fixed an obvious error in implementation.

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py	Sat Mar  6 21:02:19 2010
@@ -4,6 +4,8 @@
 from pypy.interpreter.gateway import interp2app
 from pypy.rlib.debug import make_sure_not_resized
 
+from pypy.objspace.std.sliceobject import W_SliceObject
+
 from pypy.module.micronumpy.array import BaseNumArray
 from pypy.module.micronumpy.array import base_typedef
 from pypy.module.micronumpy.array import \
@@ -156,8 +158,9 @@
         def descr_getitem(self, w_index):
             space = self.space
             if space.is_true(space.isinstance(w_index, space.w_slice)):
+                assert isinstance(w_index, W_SliceObject)
                 start, stop, step, slen = w_index.indices4(space, self.len())
-                res = sdresult(self.dtype.code)(space, slen, self.dtype)
+                res = NumArray(space, slen, self.dtype)
                 if step == 1:
                     res.storage[:] = self.storage[start:stop]
                 else:
@@ -170,7 +173,7 @@
                     index = space.int_w(w_index)
                 except TypeError, e:
                     raise OperationError(space.w_IndexError,
-                                        space.wrap('Wrong index'))
+                                        space.wrap("index must either be an int or a sequence"))
             try:
                 return space.wrap(self.storage[index])
             except IndexError:
@@ -181,6 +184,7 @@
         def descr_setitem(self, w_index, w_value):
             space = self.space
             if space.is_true(space.isinstance(w_index, space.w_slice)):
+                assert isinstance(w_index, W_SliceObject)
                 start, stop, step, slen = w_index.indices4(space, self.len())
                 try:
                     space.iter(w_value)
@@ -196,8 +200,8 @@
                         for i in range(start, stop, step):
                             self.storage[i] = value
                     return
-                lop = space.int_w(space.len(w_value))
-                if lop != slen:
+                operand_length = space.int_w(space.len(w_value))
+                if operand_length != slen:
                     raise OperationError(space.w_TypeError,
                                                 space.wrap('shape mismatch'))
                 value = space.fixedview(w_value)
@@ -212,11 +216,16 @@
             else:
                 try:
                     index = space.int_w(w_index)
-                except TypeError, e:
-                    raise OperationError(space.w_IndexError,
-                                                    space.wrap('Wrong index'))
+                except OperationError, e:
+                    if e.match(space, space.w_TypeError):
+                        raise OperationError(space.w_ValueError,
+                                             space.wrap("can't understand index")) #FIXME: more meaningful message based on type
             try:
                 self.storage[index] = coerce(space, w_value)
+            except OperationError, e:
+                if e.match(space, space.w_TypeError):
+                    raise OperationError(space.w_ValueError,
+                                         space.wrap("can't understand value")) #FIXME: more meaningful message based on type
             except IndexError:
                 raise OperationError(space.w_IndexError,
                                      space.wrap("list index out of range"))

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	Sat Mar  6 21:02:19 2010
@@ -139,11 +139,13 @@
     
     def test_setitem_getitem(self):
         from numpy import zeros
+        compare = self.compare
+
         ar = zeros(8, dtype=int)
         assert ar[0] == 0
         ar[1] = 3
         assert ar[1] == 3
-        raises((TypeError, ValueError), ar.__getitem__, 'xyz')
+        raises((TypeError, ValueError), ar.__getitem__, 'xyz') #FIXME: why TypeError?
         raises(IndexError, ar.__getitem__, 38)
         assert ar[-2] == 0
         assert ar[-7] == 3
@@ -151,11 +153,17 @@
 
         ar[2:3] = [5]
         assert ar[2] == 5
-        compare = self.compare
         assert compare(ar[1:3], [3, 5])
         assert compare(ar[-6:-4], [5, 0])
         assert compare(ar[-6:-8:-1], [5, 3])
 
+        #setitem
+        ar[3] = 2
+        assert ar[3] == 2
+        ar[5] = 3.5
+        assert ar[5] == 3
+        raises(ValueError, ar.__setitem__, 0, [99])
+        raises(ValueError, ar.__setitem__, 0, 'f')
 
     def test_minimum(self):
         from numpy import zeros, minimum



More information about the Pypy-commit mailing list