[pypy-commit] pypy default: a debug repr for numpy objects

fijal noreply at buildbot.pypy.org
Mon Nov 28 15:29:44 CET 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r49898:a21d75a378ad
Date: 2011-11-28 16:27 +0200
http://bitbucket.org/pypy/pypy/changeset/a21d75a378ad/

Log:	a debug repr for numpy objects

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
@@ -811,6 +811,9 @@
     def start_iter(self, res_shape=None):
         raise NotImplementedError
 
+    def descr_debug_repr(self, space):
+        return space.wrap(self.debug_repr())
+
 def convert_to_array(space, w_obj):
     if isinstance(w_obj, BaseArray):
         return w_obj
@@ -865,6 +868,8 @@
     def copy(self):
         return Scalar(self.dtype, self.value)
 
+    def debug_repr(self):
+        return 'Scalar'
 
 class VirtualArray(BaseArray):
     """
@@ -957,6 +962,15 @@
             return self.forced_result.start_iter(res_shape)
         return Call1Iterator(self.values.start_iter(res_shape))
 
+    def debug_repr(self):
+        call_sig = self.signature.components[0]
+        assert isinstance(call_sig, signature.Call1)
+        if self.forced_result is not None:
+            return 'Call1(%s, forced=%s)' % (call_sig.func.func_name,
+                                             self.forced_result.debug_repr())
+        return 'Call1(%s, %s)' % (call_sig.func.func_name,
+                                  self.values.debug_repr())
+
 class Call2(VirtualArray):
     """
     Intermediate class for performing binary operations.
@@ -996,6 +1010,16 @@
         assert isinstance(call_sig, signature.Call2)
         return call_sig.func(self.calc_dtype, lhs, rhs)
 
+    def debug_repr(self):
+        call_sig = self.signature.components[0]
+        assert isinstance(call_sig, signature.Call2)
+        if self.forced_result is not None:
+            return 'Call2(%s, forced=%s)' % (call_sig.func.func_name,
+                self.forced_result.debug_repr())
+        return 'Call2(%s, %s, %s)' % (call_sig.func.func_name,
+            self.left.debug_repr(),
+            self.right.debug_repr())
+
 class ViewArray(BaseArray):
     """
     Class for representing views of arrays, they will reflect changes of parent
@@ -1036,9 +1060,6 @@
         return space.wrap(1)
 
 
-class VirtualView(VirtualArray):
-    pass
-
 class NDimSlice(ViewArray):
     signature = signature.BaseSignature()
 
@@ -1090,6 +1111,9 @@
     def setitem(self, item, value):
         self.parent.setitem(item, value)
 
+    def debug_repr(self):
+        return 'Slice(%s)' % self.parent.debug_repr()
+
 class NDimArray(BaseArray):
     """ A class representing contiguous array. We know that each iteration
     by say ufunc will increase the data index by one
@@ -1149,6 +1173,9 @@
             return ArrayIterator(self.size)
         raise NotImplementedError  # use ViewIterator simply, test it
 
+    def debug_repr(self):
+        return 'Array'
+
     def __del__(self):
         lltype.free(self.storage, flavor='raw', track_allocation=False)
 
@@ -1225,6 +1252,7 @@
 
     __repr__ = interp2app(BaseArray.descr_repr),
     __str__ = interp2app(BaseArray.descr_str),
+    __debug_repr__ = interp2app(BaseArray.descr_debug_repr),
 
     dtype = GetSetProperty(BaseArray.descr_get_dtype),
     shape = GetSetProperty(BaseArray.descr_get_shape),
@@ -1287,6 +1315,9 @@
     def descr_iter(self):
         return self
 
+    def debug_repr(self):
+        return 'FlatIter(%s)' % self.arr.debug_repr()
+
 
 W_FlatIterator.typedef = TypeDef(
     'flatiter',
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
@@ -980,6 +980,19 @@
         a = array([1, 2, 3])
         assert dot(a.flat, a.flat) == 14
 
+    def test_debug_repr(self):
+        from numpypy import zeros, sin
+        a = zeros(1)
+        assert a.__debug_repr__() == 'Array'
+        assert (a + a).__debug_repr__() == 'Call2(add, Array, Array)'
+        assert (a[::2]).__debug_repr__() == 'Slice(Array)'
+        assert (a + 2).__debug_repr__() == 'Call2(add, Array, Scalar)'
+        assert (a + a.flat).__debug_repr__() == 'Call2(add, Array, FlatIter(Array))'
+        assert sin(a).__debug_repr__() == 'Call1(sin, Array)'
+        b = a + a
+        b[0] = 3
+        assert b.__debug_repr__() == 'Call2(add, forced=Array)'
+
 class AppTestSupport(object):
     def setup_class(cls):
         import struct


More information about the pypy-commit mailing list