Commits

Brian Kearns  committed 9964022

allow setting array.dtype attribute

  • Participants
  • Parent commits f739878

Comments (0)

Files changed (4)

File pypy/module/micronumpy/arrayimpl/concrete.py

         return SliceArray(0, strides, backstrides, new_shape, self,
                           orig_array)
 
+    def set_dtype(self, space, dtype):
+        self.dtype = dtype
+
     def argsort(self, space, w_axis):
         from pypy.module.micronumpy.arrayimpl.sort import argsort_array
         return argsort_array(self, space, w_axis)

File pypy/module/micronumpy/arrayimpl/scalar.py

         raise OperationError(space.w_ValueError, space.wrap(
             "total size of the array must be unchanged"))
 
+    def set_dtype(self, space, dtype):
+        self.value = self.value.convert_to(space, dtype)
+        self.dtype = dtype
+
     def reshape(self, space, orig_array, new_shape):
         return self.set_shape(space, orig_array, new_shape)
 

File pypy/module/micronumpy/interp_numarray.py

     def descr_get_dtype(self, space):
         return self.implementation.dtype
 
+    def descr_set_dtype(self, space, w_dtype):
+        dtype = space.interp_w(interp_dtype.W_Dtype,
+            space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
+        if (dtype.get_size() != self.get_dtype().get_size() or
+                dtype.is_flexible_type() or self.get_dtype().is_flexible_type()):
+            raise OperationError(space.w_ValueError, space.wrap(
+                "new type not compatible with array."))
+        self.implementation.set_dtype(space, dtype)
+
+    def descr_del_dtype(self, space):
+        raise OperationError(space.w_AttributeError, space.wrap(
+            "Cannot delete array dtype"))
+
     def descr_get_ndim(self, space):
         return space.wrap(len(self.get_shape()))
 
     __gt__ = interp2app(W_NDimArray.descr_gt),
     __ge__ = interp2app(W_NDimArray.descr_ge),
 
-    dtype = GetSetProperty(W_NDimArray.descr_get_dtype),
+    dtype = GetSetProperty(W_NDimArray.descr_get_dtype,
+                           W_NDimArray.descr_set_dtype,
+                           W_NDimArray.descr_del_dtype),
     shape = GetSetProperty(W_NDimArray.descr_get_shape,
                            W_NDimArray.descr_set_shape),
     strides = GetSetProperty(W_NDimArray.descr_get_strides),

File pypy/module/micronumpy/test/test_numarray.py

 
     def test_noop_ndmin(self):
         from numpypy import array
-
         arr = array([1], ndmin=3)
         assert arr.shape == (1, 1, 1)
 
         e = d.repeat(3, 0)
         assert e.shape == (9, 4, 0)
 
+    def test_dtype_attribute(self):
+        import numpy as np
+        a = np.array(40000, dtype='uint16')
+        assert a.dtype == np.uint16
+        a.dtype = np.int16
+        assert a == -25536
+        a = np.array([1, 2, 3, 4, 40000], dtype='uint16')
+        assert a.dtype == np.uint16
+        a.dtype = np.int16
+        assert a[4] == -25536
+        exc = raises(ValueError, 'a.dtype = None')
+        assert exc.value[0] == 'new type not compatible with array.'
+        exc = raises(ValueError, 'a.dtype = np.int32')
+        assert exc.value[0] == 'new type not compatible with array.'
+        exc = raises(AttributeError, 'del a.dtype')
+        assert exc.value[0] == 'Cannot delete array dtype'
+
     def test_buffer(self):
         import numpy as np
         a = np.array([1,2,3])