Commits

Brian Kearns committed bf10cd3

fix some scalar setitem cases

  • Participants
  • Parent commits a93131d

Comments (0)

Files changed (3)

pypy/module/micronumpy/arrayimpl/scalar.py

                              space.wrap("0-d arrays can't be indexed"))
 
     def descr_setitem(self, space, _, w_idx, w_val):
+        if space.isinstance_w(w_idx, space.w_tuple):
+            if space.len_w(w_idx) == 0:
+                return self.set_scalar_value(self.dtype.coerce(space, w_val))
+        elif space.isinstance_w(w_idx, space.w_str):
+            if self.dtype.is_record_type():
+                return self.value.descr_setitem(space, w_idx, w_val)
         raise OperationError(space.w_IndexError,
                              space.wrap("0-d arrays can't be indexed"))
 
         s = self.dtype.itemtype.bool(self.value)
         w_res = W_NDimArray.from_shape(space, [s], index_type)
         if s == 1:
-            w_res.implementation.setitem(0, index_type.itemtype.box(0)) 
+            w_res.implementation.setitem(0, index_type.itemtype.box(0))
         return space.newtuple([w_res])
 
     def fill(self, space, w_value):

pypy/module/micronumpy/interp_boxes.py

             return space.wrap(dtype.itemtype.to_str(read_val))
         return read_val
 
-    @unwrap_spec(item=str)
-    def descr_setitem(self, space, item, w_value):
+    def descr_setitem(self, space, w_item, w_value):
+        if space.isinstance_w(w_item, space.w_basestring):
+            item = space.str_w(w_item)
+        else:
+            raise OperationError(space.w_IndexError, space.wrap(
+                "invalid index"))
         try:
             ofs, dtype = self.dtype.fields[item]
         except KeyError:
-            raise OperationError(space.w_IndexError,
-                                 space.wrap("Field %s does not exist" % item))
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("field named %s not found" % item))
         dtype.itemtype.store(self.arr, self.ofs, ofs,
                              dtype.coerce(space, w_value))
 

pypy/module/micronumpy/test/test_numarray.py

         b = a[()]
         assert type(b) is int_
         assert b == 3
+        a[()] = 4
+        assert a == 4
 
     def test_len(self):
         from numpypy import array
         b = [('x', int), ('y', a)]
         arr = zeros((), dtype=b)
         assert arr['x'] == 0
+        arr['x'] = 2
+        assert arr['x'] == 2
+        exc = raises(IndexError, "arr[3L]")
+        assert exc.value.message == "0-d arrays can't be indexed"
+        exc = raises(ValueError, "arr['xx'] = 2")
+        assert exc.value.message == "field named xx not found"
         arr['y']
         #assert arr['y'].shape == ()
         #assert arr['y'][()][0] == 0