Commits

mattip  committed 61f2c6a

test, implement 90% of VoidType to_builtin_type

  • Participants
  • Parent commits 1ac497f
  • Branches voidtype_strformat

Comments (0)

Files changed (2)

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

                                        [[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]]])]")

File pypy/module/micronumpy/types.py

         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