[pypy-commit] pypy voidtype_strformat: test, implement 90% of VoidType to_builtin_type

mattip noreply at buildbot.pypy.org
Fri Nov 22 13:07:53 CET 2013


Author: Matti Picus <matti.picus at gmail.com>
Branch: voidtype_strformat
Changeset: r68280:61f2c6a378fd
Date: 2013-11-22 14:06 +0200
http://bitbucket.org/pypy/pypy/changeset/61f2c6a378fd/

Log:	test, implement 90% of VoidType to_builtin_type

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
@@ -3109,6 +3109,9 @@
                                        [[7, 8, 9], [10, 11, 12]]])],
                   dtype=dt)
         s = str(a)
+        i = a.item()
+        assert isinstance(i, tuple)
+        assert len(i) == 4
         skip('incorrect formatting via dump_data')
         assert s.endswith("[('aaaa', 1.0, 8.0, [[[1, 2, 3], [4, 5, 6]], "
                           "[[7, 8, 9], [10, 11, 12]]])]")
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1800,6 +1800,28 @@
         arr = self.readarray(box.arr, box.ofs, 0, box.dtype)
         return arr.dump_data(prefix='', suffix='')
 
+    def to_builtin_type(self, space, item):
+        ''' From the documentation of ndarray.item():
+        "Void arrays return a buffer object for item(),
+         unless fields are defined, in which case a tuple is returned."
+        '''
+        dt = item.arr.dtype
+        tpl =()
+        for name in dt.fieldnames:
+            ofs, dtype = dt.fields[name]
+            if isinstance(dtype.itemtype, VoidType):
+                read_val = dtype.itemtype.readarray(item.arr, ofs, 0, dtype)
+            else:
+                read_val = dtype.itemtype.read(item.arr, ofs, 0, dtype)
+            if isinstance (read_val, interp_boxes.W_StringBox):
+                # StringType returns a str
+                read_val = space.wrap(dtype.itemtype.to_str(read_val))
+            tpl = tpl + (read_val,)
+        if len(tpl) == 0:
+            raise OperationError(space.w_NotImplementedError, space.wrap(
+                    "item() for Void aray with no fields not implemented"))
+        return space.wrap(tpl)
+
 class RecordType(FlexibleType):
     T = lltype.Char
 


More information about the pypy-commit mailing list