[pypy-commit] pypy default: fix numpy record initialization case

bdkearns noreply at buildbot.pypy.org
Fri Feb 21 09:48:36 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r69236:be5e3955ea8e
Date: 2014-02-21 03:44 -0500
http://bitbucket.org/pypy/pypy/changeset/be5e3955ea8e/

Log:	fix numpy record initialization case

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
@@ -3161,7 +3161,8 @@
         raises(IndexError, 'a[0]["xyz"]')
         assert a[0]['x'] == 0
         assert a[0]['y'] == 0
-        raises(ValueError, "a[0] = (1, 2, 3)")
+        exc = raises(ValueError, "a[0] = (1, 2, 3)")
+        assert exc.value[0] == 'size of tuple must match number of fields.'
         a[0]['x'] = 13
         assert a[0]['x'] == 13
         a[1] = (1, 2)
@@ -3487,6 +3488,15 @@
         a = np.array([1,2,3], dtype='int16')
         assert (a * 2).dtype == np.dtype('int16')
 
+    def test_coerce_record(self):
+        import numpy as np
+        dt = np.dtype([('a', '?'), ('b', '?')])
+        b = np.array([True, True])
+        a = np.array([b, b, b], dtype=dt)
+        assert a.shape == (3, 2)
+        for i in a.flat:
+            assert tuple(i) == (True, False)
+
 
 class AppTestPyPy(BaseNumpyAppTest):
     def setup_class(cls):
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
@@ -1873,22 +1873,24 @@
         if isinstance(w_item, interp_boxes.W_VoidBox):
             return w_item
         if w_item is not None:
-            # we treat every sequence as sequence, no special support
-            # for arrays
-            if not space.issequence_w(w_item):
-                raise OperationError(space.w_TypeError, space.wrap(
-                    "expected sequence"))
-            if len(dtype.fields) != space.len_w(w_item):
-                raise OperationError(space.w_ValueError, space.wrap(
-                    "wrong length"))
-            items_w = space.fixedview(w_item)
+            if space.isinstance_w(w_item, space.w_tuple):
+                if len(dtype.fields) != space.len_w(w_item):
+                    raise OperationError(space.w_ValueError, space.wrap(
+                        "size of tuple must match number of fields."))
+                items_w = space.fixedview(w_item)
+            else:
+                # XXX support initializing from readable buffers
+                items_w = [w_item]
         else:
             items_w = [None] * len(dtype.fields)
         arr = VoidBoxStorage(dtype.get_size(), dtype)
-        for i in range(len(items_w)):
+        for i in range(len(dtype.fields)):
             ofs, subdtype = dtype.fields[dtype.fieldnames[i]]
             itemtype = subdtype.itemtype
-            w_box = itemtype.coerce(space, subdtype, items_w[i])
+            try:
+                w_box = itemtype.coerce(space, subdtype, items_w[i])
+            except IndexError:
+                w_box = itemtype.coerce(space, subdtype, None)
             itemtype.store(arr, 0, ofs, w_box)
         return interp_boxes.W_VoidBox(arr, 0, dtype)
 


More information about the pypy-commit mailing list