[pypy-commit] pypy numpy-ndim-size: numpy: added ndim and size attributes. This includes work by Timo.

justinpeel noreply at buildbot.pypy.org
Sat Jul 16 18:38:24 CEST 2011


Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-ndim-size
Changeset: r45674:db28a9352aa2
Date: 2011-07-16 10:38 -0600
http://bitbucket.org/pypy/pypy/changeset/db28a9352aa2/

Log:	numpy: added ndim and size attributes. This includes work by Timo.

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
@@ -244,6 +244,12 @@
     def descr_len(self, space):
         return self.get_concrete().descr_len(space)
 
+    def descr_get_size(self, space):
+        return space.wrap(self.find_size())
+
+    def descr_get_ndim(self, space):
+        return space.wrap(self.find_ndim())
+
     def descr_getitem(self, space, w_idx):
         # TODO: indexing by tuples
         start, stop, step, slice_length = space.decode_index4(w_idx, self.find_size())
@@ -287,6 +293,9 @@
     def find_size(self):
         raise ValueError
 
+    def find_ndim(self):
+        raise ValueError
+
     def eval(self, i):
         return self.float_value
 
@@ -336,6 +345,12 @@
             return self.forced_result.find_size()
         return self._find_size()
 
+    def find_ndim(self):
+        if self.forced_result is not None:
+            # The result has been computed and sources may be unavailable
+            return self.forced_result.find_ndim()
+        return self._find_ndim()
+
 
 class Call1(VirtualArray):
     _immutable_fields_ = ["function", "values"]
@@ -351,6 +366,9 @@
     def _find_size(self):
         return self.values.find_size()
 
+    def _find_ndim(self):
+        return self.values.find_ndim()
+
     def _eval(self, i):
         return self.function(self.values.eval(i))
 
@@ -377,6 +395,13 @@
             pass
         return self.right.find_size()
 
+    def _find_ndim(self):
+        try:
+            return self.left.find_ndim()
+        except ValueError:
+            pass
+        return self.right.find_ndim()
+
     def _eval(self, i):
         lhs, rhs = self.left.eval(i), self.right.eval(i)
         return self.function(lhs, rhs)
@@ -426,10 +451,14 @@
         self.stop = stop
         self.step = step
         self.size = slice_length
+        self.ndim = 1
 
     def find_size(self):
         return self.size
 
+    def find_ndim(self):
+        return self.ndim
+
     def calc_index(self, item):
         return (self.start + item * self.step)
 
@@ -440,6 +469,7 @@
     def __init__(self, size):
         BaseArray.__init__(self)
         self.size = size
+        self.ndim = 1
         self.storage = lltype.malloc(TP, size, zero=True,
                                      flavor='raw', track_allocation=False)
         # XXX find out why test_zjit explodes with trackign of allocations
@@ -450,6 +480,9 @@
     def find_size(self):
         return self.size
 
+    def find_ndim(self):
+        return self.ndim
+
     def eval(self, i):
         return self.storage[i]
 
@@ -507,6 +540,8 @@
     __new__ = interp2app(descr_new_numarray),
 
     shape = GetSetProperty(BaseArray.descr_get_shape),
+    size = GetSetProperty(BaseArray.descr_get_size),
+    ndim = GetSetProperty(BaseArray.descr_get_ndim),
 
     __len__ = interp2app(BaseArray.descr_len),
     __getitem__ = interp2app(BaseArray.descr_getitem),
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
@@ -66,6 +66,20 @@
         assert len(a) == 5
         assert len(a + a) == 5
 
+    def test_ndim(self):
+        from numpy import array
+        a = array(range(4))
+        assert a.ndim == 1
+        assert (a + a).ndim == 1
+        assert a[:3].ndim == 1
+
+    def test_size(self):
+        from numpy import array
+        a = array(range(4))
+        assert a.size == 4
+        assert (a + a).size == 4
+        assert a[:3].size == 3
+
     def test_shape(self):
         from numpy import array
         a = array(range(5))


More information about the pypy-commit mailing list