[pypy-commit] pypy default: Added ndarray.{itemsize, nbytes}

alex_gaynor noreply at buildbot.pypy.org
Thu Feb 2 04:30:32 CET 2012


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r52025:e2ff34308249
Date: 2012-02-01 22:30 -0500
http://bitbucket.org/pypy/pypy/changeset/e2ff34308249/

Log:	Added ndarray.{itemsize, nbytes}

diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -267,7 +267,7 @@
         out_size = support.product(out_shape)
         result = W_NDimArray(out_size, out_shape, dtype)
         # This is the place to add fpypy and blas
-        return multidim_dot(space, self.get_concrete(), 
+        return multidim_dot(space, self.get_concrete(),
                             other.get_concrete(), result, dtype,
                             other_critical_dim)
 
@@ -280,6 +280,12 @@
     def descr_get_ndim(self, space):
         return space.wrap(len(self.shape))
 
+    def descr_get_itemsize(self, space):
+        return space.wrap(self.find_dtype().itemtype.get_element_size())
+
+    def descr_get_nbytes(self, space):
+        return space.wrap(self.size * self.find_dtype().itemtype.get_element_size())
+
     @jit.unroll_safe
     def descr_get_shape(self, space):
         return space.newtuple([space.wrap(i) for i in self.shape])
@@ -507,7 +513,7 @@
             w_shape = space.newtuple(args_w)
         new_shape = get_shape_from_iterable(space, self.size, w_shape)
         return self.reshape(space, new_shape)
-        
+
     def reshape(self, space, new_shape):
         concrete = self.get_concrete()
         # Since we got to here, prod(new_shape) == self.size
@@ -1289,11 +1295,13 @@
                            BaseArray.descr_set_shape),
     size = GetSetProperty(BaseArray.descr_get_size),
     ndim = GetSetProperty(BaseArray.descr_get_ndim),
-    item = interp2app(BaseArray.descr_item),
+    itemsize = GetSetProperty(BaseArray.descr_get_itemsize),
+    nbytes = GetSetProperty(BaseArray.descr_get_nbytes),
 
     T = GetSetProperty(BaseArray.descr_get_transpose),
     flat = GetSetProperty(BaseArray.descr_get_flatiter),
     ravel = interp2app(BaseArray.descr_ravel),
+    item = interp2app(BaseArray.descr_item),
 
     mean = interp2app(BaseArray.descr_mean),
     sum = interp2app(BaseArray.descr_sum),
@@ -1349,8 +1357,8 @@
         return space.wrap(self.index)
 
     def descr_coords(self, space):
-        coords, step, lngth = to_coords(space, self.base.shape, 
-                            self.base.size, self.base.order, 
+        coords, step, lngth = to_coords(space, self.base.shape,
+                            self.base.size, self.base.order,
                             space.wrap(self.index))
         return space.newtuple([space.wrap(c) for c in coords])
 
@@ -1380,7 +1388,7 @@
                                              step=step,
                                              res=res,
                                              ri=ri,
-                                            ) 
+                                            )
             w_val = base.getitem(basei.offset)
             res.setitem(ri.offset,w_val)
             basei = basei.next_skip_x(shapelen, step)
@@ -1408,7 +1416,7 @@
                                              arr=arr,
                                              ai=ai,
                                              lngth=lngth,
-                                            ) 
+                                            )
             v = arr.getitem(ai).convert_to(base.dtype)
             base.setitem(basei.offset, v)
             # need to repeat input values until all assignments are done
@@ -1424,7 +1432,6 @@
 
 W_FlatIterator.typedef = TypeDef(
     'flatiter',
-    #__array__  = #MISSING
     __iter__ = interp2app(W_FlatIterator.descr_iter),
     __getitem__ = interp2app(W_FlatIterator.descr_getitem),
     __setitem__ = interp2app(W_FlatIterator.descr_setitem),
@@ -1434,7 +1441,6 @@
     __le__ = interp2app(BaseArray.descr_le),
     __gt__ = interp2app(BaseArray.descr_gt),
     __ge__ = interp2app(BaseArray.descr_ge),
-    #__sizeof__ #MISSING
     base = GetSetProperty(W_FlatIterator.descr_base),
     index = GetSetProperty(W_FlatIterator.descr_index),
     coords = GetSetProperty(W_FlatIterator.descr_coords),
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -173,7 +173,7 @@
         def _to_coords(index, order):
             return to_coords(self.space, [2, 3, 4], 24, order,
                              self.space.wrap(index))[0]
-        
+
         assert _to_coords(0, 'C') == [0, 0, 0]
         assert _to_coords(1, 'C') == [0, 0, 1]
         assert _to_coords(-1, 'C') == [1, 2, 3]
@@ -306,7 +306,7 @@
         from _numpypy import arange
         a = arange(15).reshape(3, 5)
         assert a[1, 3] == 8
-        assert a.T[1, 2] == 11 
+        assert a.T[1, 2] == 11
 
     def test_setitem(self):
         from _numpypy import array
@@ -1121,14 +1121,14 @@
         f1 = array([0,1])
         f = concatenate((f1, [2], f1, [7]))
         assert (f == [0,1,2,0,1,7]).all()
-        
+
         bad_axis = raises(ValueError, concatenate, (a1,a2), axis=1)
         assert str(bad_axis.value) == "bad axis argument"
-        
+
         concat_zero = raises(ValueError, concatenate, ())
         assert str(concat_zero.value) == \
             "concatenation of zero-length sequences is impossible"
-        
+
         dims_disagree = raises(ValueError, concatenate, (a1, b1), axis=0)
         assert str(dims_disagree.value) == \
             "array dimensions must agree except for axis being concatenated"
@@ -1163,6 +1163,25 @@
         a = array([[1, 2], [3, 4]])
         assert (a.T.flatten() == [1, 3, 2, 4]).all()
 
+    def test_itemsize(self):
+        from _numpypy import ones, dtype, array
+
+        for obj in [float, bool, int]:
+            assert ones(1, dtype=obj).itemsize == dtype(obj).itemsize
+        assert (ones(1) + ones(1)).itemsize == 8
+        assert array(1).itemsize == 8
+        assert ones(1)[:].itemsize == 8
+
+    def test_nbytes(self):
+        from _numpypy import array, ones
+
+        assert ones(1).nbytes == 8
+        assert ones((2, 2)).nbytes == 32
+        assert ones((2, 2))[1:,].nbytes == 16
+        assert (ones(1) + ones(1)).nbytes == 8
+        assert array(3).nbytes == 8
+
+
 class AppTestMultiDim(BaseNumpyAppTest):
     def test_init(self):
         import _numpypy
@@ -1458,13 +1477,13 @@
         b = a.T.flat
         assert (b == [0,  4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11]).all()
         assert not (b != [0,  4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11]).any()
-        assert ((b >= range(12)) == [True, True, True,False, True, True, 
+        assert ((b >= range(12)) == [True, True, True,False, True, True,
                              False, False, True, False, False, True]).all()
-        assert ((b < range(12)) != [True, True, True,False, True, True, 
+        assert ((b < range(12)) != [True, True, True,False, True, True,
                              False, False, True, False, False, True]).all()
-        assert ((b <= range(12)) != [False, True, True,False, True, True, 
+        assert ((b <= range(12)) != [False, True, True,False, True, True,
                             False, False, True, False, False, False]).all()
-        assert ((b > range(12)) == [False, True, True,False, True, True, 
+        assert ((b > range(12)) == [False, True, True,False, True, True,
                             False, False, True, False, False, False]).all()
     def test_flatiter_view(self):
         from _numpypy import arange


More information about the pypy-commit mailing list