Commits

mattip committed 84ff285

add more tests, fix by adding __float__ (ronan, mattip)

Comments (0)

Files changed (2)

pypy/module/micronumpy/interp_numarray.py

             return space.int(self.descr_getitem(space, space.wrap(0)))
         raise OperationError(space.w_TypeError, space.wrap("only length-1 arrays can be converted to Python scalars"))
 
+    def descr_float(self, space):
+        shape = self.get_shape()
+        if len(shape) == 0:
+            assert isinstance(self.implementation, scalar.Scalar)
+            return space.float(space.wrap(self.implementation.get_scalar_value()))
+        if shape == [1]:
+            return space.float(self.descr_getitem(space, space.wrap(0)))
+        raise OperationError(space.w_TypeError, space.wrap("only length-1 arrays can be converted to Python scalars"))
+
+
 
 @unwrap_spec(offset=int)
 def descr_new_array(space, w_subtype, w_shape, w_dtype=None, w_buffer=None,
     __repr__ = interp2app(W_NDimArray.descr_repr),
     __str__ = interp2app(W_NDimArray.descr_str),
     __int__ = interp2app(W_NDimArray.descr_int),
+    __float__ = interp2app(W_NDimArray.descr_float),
 
     __pos__ = interp2app(W_NDimArray.descr_pos),
     __neg__ = interp2app(W_NDimArray.descr_neg),

pypy/module/micronumpy/test/test_numarray.py

         assert a[4] == 5.0
         raises(IndexError, "a[5] = 0.0")
         raises(IndexError, "a[-6] = 3.0")
+        a[1] = array(100)
+        a[2] = array([100])
+        assert a[1] == 100
+        assert a[2] == 100
         a = array(range(5), dtype=float)
         a[0] = 0.005
         assert a[0] == 0.005
         a[1] = array(-0.005)
+        a[2] = array([-0.005])
         assert a[1] == -0.005
+        assert a[2] == -0.005
 
 
     def test_setitem_tuple(self):