Commits

Brian Kearns committed c8e4f95

test/fix ndarray init from list of array scalars

  • Participants
  • Parent commits 6269dd1

Comments (0)

Files changed (3)

File pypy/module/micronumpy/interp_numarray.py

     if dtype is None or (
                  dtype.is_str_or_unicode() and dtype.get_size() < 1):
         for w_elem in elems_w:
+            if isinstance(w_elem, W_NDimArray) and w_elem.is_scalar():
+                w_elem = w_elem.get_scalar_value()
             dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
                                                         dtype)
             #if dtype is interp_dtype.get_dtype_cache(space).w_float64dtype:

File pypy/module/micronumpy/strides.py

     if (is_rec_type and space.isinstance_w(w_elem, space.w_tuple)):
         return True
     if (space.isinstance_w(w_elem, space.w_tuple) or
-        isinstance(w_elem, W_NDimArray) or
         space.isinstance_w(w_elem, space.w_list)):
         return False
+    if isinstance(w_elem, W_NDimArray) and not w_elem.is_scalar():
+        return False
     return True
 
 def find_shape_and_elems(space, w_iterable, dtype):
     batch = space.listview(w_iterable)
     is_rec_type = dtype is not None and dtype.is_record_type()
     while True:
-        new_batch = []
         if not batch:
             return shape[:], []
         if is_single_elem(space, batch[0], is_rec_type):
                     raise OperationError(space.w_ValueError, space.wrap(
                         "setting an array element with a sequence"))
             return shape[:], batch
+        new_batch = []
         size = space.len_w(batch[0])
         for w_elem in batch:
             if (is_single_elem(space, w_elem, is_rec_type) or

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

         a = array(range(5))
         assert a[3] == 3
 
+    def test_list_of_array_init(self):
+        import numpy as np
+        a = np.array([np.array(True), np.array(False)])
+        assert a.shape == (2,)
+        assert a.dtype == np.bool_
+        assert (a == [True, False]).all()
+        a = np.array([np.array(True), np.array(2)])
+        assert a.shape == (2,)
+        assert a.dtype == np.int_
+        assert (a == [1, 2]).all()
+        a = np.array([np.array(True), np.int_(2)])
+        assert a.shape == (2,)
+        assert a.dtype == np.int_
+        assert (a == [1, 2]).all()
+        a = np.array([np.array([True]), np.array([2])])
+        assert a.shape == (2, 1)
+        assert a.dtype == np.int_
+        assert (a == [[1], [2]]).all()
+
     def test_getitem(self):
         from numpypy import array
         a = array(range(5))