Esempio n. 1
0
 def astype(self, space, dtype):
     strides, backstrides = calc_strides(self.get_shape(), dtype,
                                                 self.order)
     impl = ConcreteArray(self.get_shape(), dtype, self.order,
                          strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 2
0
 def astype(self, space, dtype, order):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if order not in ('C', 'F'):
         raise oefmt(space.w_ValueError, "Unknown order %s in astype", order)
     if len(strides) == 0:
         t_strides = []
         backstrides = []
     elif order != self.order:
         t_strides, backstrides = calc_strides(shape, dtype, order)
     else:
         indx_array = range(len(strides))
         list_sorter = StrideSort(indx_array, strides)
         list_sorter.sort()
         t_elsize = dtype.elsize
         t_strides = strides[:]
         base = dtype.elsize
         for i in indx_array:
             t_strides[i] = base
             base *= shape[i]
         backstrides = calc_backstrides(t_strides, shape)
     impl = ConcreteArray(shape, dtype, order, t_strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 3
0
 def astype(self, space, dtype, order, copy=True):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if order not in (NPY.KEEPORDER, NPY.FORTRANORDER, NPY.CORDER):
         raise oefmt(space.w_ValueError, "Unknown order %d in astype", order)
     if len(strides) == 0:
         t_strides = []
         backstrides = []
     elif order in (NPY.FORTRANORDER, NPY.CORDER):
         t_strides, backstrides = calc_strides(shape, dtype, order)
     else:
         indx_array = range(len(strides))
         list_sorter = StrideSort(indx_array, strides, self.order)
         list_sorter.sort()
         t_elsize = dtype.elsize
         t_strides = strides[:]
         base = dtype.elsize
         for i in indx_array:
             t_strides[i] = base
             base *= shape[i]
         backstrides = calc_backstrides(t_strides, shape)
     order = support.get_order_as_CF(self.order, order)
     impl = ConcreteArray(shape, dtype, order, t_strides, backstrides)
     if copy:
         loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 4
0
 def astype(self, space, dtype, order, copy=True):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if order not in (NPY.KEEPORDER, NPY.FORTRANORDER, NPY.CORDER):
         raise oefmt(space.w_ValueError, "Unknown order %d in astype", order)
     if len(strides) == 0:
         t_strides = []
         backstrides = []
     elif order in (NPY.FORTRANORDER, NPY.CORDER):
         t_strides, backstrides = calc_strides(shape, dtype, order)
     else:
         indx_array = range(len(strides))
         list_sorter = StrideSort(indx_array, strides, self.order)
         list_sorter.sort()
         t_elsize = dtype.elsize
         t_strides = strides[:]
         base = dtype.elsize
         for i in indx_array:
             t_strides[i] = base
             base *= shape[i]
         backstrides = calc_backstrides(t_strides, shape)
     order = support.get_order_as_CF(self.order, order)
     impl = ConcreteArray(shape, dtype, order, t_strides, backstrides)
     if copy:
         loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 5
0
 def astype(self, space, dtype):
     strides, backstrides = calc_strides(self.get_shape(), dtype,
                                         self.order)
     impl = ConcreteArray(self.get_shape(), dtype, self.order, strides,
                          backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 6
0
 def setslice(self, space, arr):
     impl = arr.implementation
     if impl.is_scalar():
         self.fill(impl.get_scalar_value())
         return
     shape = shape_agreement(space, self.get_shape(), arr)
     if impl.storage == self.storage:
         impl = impl.copy(space)
     loop.setslice(space, shape, self, impl)
Esempio n. 7
0
 def setslice(self, space, arr):
     impl = arr.implementation
     if impl.is_scalar():
         self.fill(impl.get_scalar_value())
         return
     shape = shape_agreement(space, self.get_shape(), arr)
     if impl.storage == self.storage:
         impl = impl.copy(space)
     loop.setslice(space, shape, self, impl)
Esempio n. 8
0
 def setslice(self, space, arr):
     if len(arr.get_shape()) > 0 and len(self.get_shape()) == 0:
         raise oefmt(space.w_ValueError,
             "could not broadcast input array from shape "
             "(%s) into shape ()",
             ','.join([str(x) for x in arr.get_shape()]))
     shape = shape_agreement(space, self.get_shape(), arr)
     impl = arr.implementation
     if impl.storage == self.storage:
         impl = impl.copy(space)
     loop.setslice(space, shape, self, impl)
Esempio n. 9
0
 def astype(self, space, dtype):
     strides, backstrides = support.calc_strides(self.get_shape(), dtype,
                                                 self.order)
     impl = ConcreteArray(self.get_shape(), dtype, self.order,
                          strides, backstrides)
     if self.dtype.is_str_or_unicode() and not dtype.is_str_or_unicode():
         raise OperationError(space.w_NotImplementedError, space.wrap(
             "astype(%s) not implemented yet" % self.dtype))
     else:
         loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 10
0
 def astype(self, space, dtype):
     strides, backstrides = support.calc_strides(self.get_shape(), dtype,
                                                 self.order)
     impl = ConcreteArray(self.get_shape(), dtype, self.order, strides,
                          backstrides)
     if self.dtype.is_str_or_unicode() and not dtype.is_str_or_unicode():
         raise OperationError(
             space.w_NotImplementedError,
             space.wrap("astype(%s) not implemented yet" % self.dtype))
     else:
         loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 11
0
 def setslice(self, space, arr):
     if len(arr.get_shape()) > 0 and len(self.get_shape()) == 0:
         raise oefmt(
             space.w_ValueError,
             "could not broadcast input array from shape "
             "(%s) into shape ()",
             ','.join([str(x) for x in arr.get_shape()]))
     shape = shape_agreement(space, self.get_shape(), arr)
     impl = arr.implementation
     if impl.storage == self.storage:
         impl = impl.copy(space)
     loop.setslice(space, shape, self, impl)
Esempio n. 12
0
 def setslice(self, space, arr):
     if len(arr.get_shape()) >  len(self.get_shape()):
         # record arrays get one extra dimension
         if not self.dtype.is_record() or \
                 len(arr.get_shape()) > len(self.get_shape()) + 1:
             raise oefmt(space.w_ValueError,
                 "could not broadcast input array from shape "
                 "(%s) into shape (%s)",
                 ','.join([str(x) for x in arr.get_shape()]),
                 ','.join([str(x) for x in self.get_shape()]),
                 )
     shape = shape_agreement(space, self.get_shape(), arr)
     impl = arr.implementation
     if impl.storage == self.storage:
         impl = impl.copy(space)
     loop.setslice(space, shape, self, impl)
Esempio n. 13
0
 def setslice(self, space, arr):
     if len(arr.get_shape()) > len(self.get_shape()):
         # record arrays get one extra dimension
         if not self.dtype.is_record() or \
                 len(arr.get_shape()) > len(self.get_shape()) + 1:
             raise oefmt(
                 space.w_ValueError,
                 "could not broadcast input array from shape "
                 "(%s) into shape (%s)",
                 ','.join([str(x) for x in arr.get_shape()]),
                 ','.join([str(x) for x in self.get_shape()]),
             )
     shape = shape_agreement(space, self.get_shape(), arr)
     impl = arr.implementation
     if impl.storage == self.storage:
         impl = impl.copy(space)
     loop.setslice(space, shape, self, impl)
Esempio n. 14
0
 def astype(self, space, dtype):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if len(strides) > 0:
         mins = strides[0]
         t_elsize = dtype.elsize
         for s in strides:
             if s < mins:
                 mins = s
         t_strides = [s * t_elsize / mins for s in strides]
         backstrides = calc_backstrides(t_strides, shape)
     else:
         t_strides = []
         backstrides = []
     impl = ConcreteArray(shape, dtype, self.order, t_strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 15
0
 def astype(self, space, dtype):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if len(strides) > 0:
         mins = strides[0]
         t_elsize = dtype.elsize
         for s in strides:
             if s < mins:
                 mins = s
         t_strides = [s * t_elsize / mins for s in strides]
         backstrides = calc_backstrides(t_strides, shape)
     else:
         t_strides = []
         backstrides = []
     impl = ConcreteArray(shape, dtype, self.order, t_strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 16
0
 def astype(self, space, dtype, order):
     # copy the general pattern of the strides
     # but make the array storage contiguous in memory
     shape = self.get_shape()
     strides = self.get_strides()
     if order not in ('C', 'F'):
         raise oefmt(space.w_ValueError, "Unknown order %s in astype",
                     order)
     if len(strides) == 0:
         t_strides = []
         backstrides = []
     elif order != self.order:
         t_strides, backstrides = calc_strides(shape, dtype, order)
     else:
         mins = strides[0]
         t_elsize = dtype.elsize
         for s in strides:
             if s < mins:
                 mins = s
         t_strides = [s * t_elsize / mins for s in strides]
         backstrides = calc_backstrides(t_strides, shape)
     impl = ConcreteArray(shape, dtype, order, t_strides, backstrides)
     loop.setslice(space, impl.get_shape(), impl, self)
     return impl
Esempio n. 17
0
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False):
    from pypy.module.micronumpy import strides

    # for anything that isn't already an array, try __array__ method first
    if not isinstance(w_object, W_NDimArray):
        w_array = try_array_method(space, w_object, w_dtype)
        if w_array is not None:
            # continue with w_array, but do further operations in place
            w_object = w_array
            copy = False
    if not isinstance(w_object, W_NDimArray):
        w_array = try_interface_method(space, w_object)
        if w_array is not None:
            w_object = w_array
            copy = False
    dtype = descriptor.decode_w_dtype(space, w_dtype)

    if space.is_none(w_order):
        order = 'C'
    else:
        order = space.str_w(w_order)
        if order == 'K':
            order = 'C'
        if order != 'C':  # or order != 'F':
            raise oefmt(space.w_ValueError, "Unknown order: %s", order)

    if isinstance(w_object, W_NDimArray):
        if (dtype is None or w_object.get_dtype() is dtype):
            if copy and (subok or type(w_object) is W_NDimArray):
                return w_object.descr_copy(space, w_order)
            elif not copy and (subok or type(w_object) is W_NDimArray):
                return w_object
        if subok and not type(w_object) is W_NDimArray:
            raise oefmt(space.w_NotImplementedError,
                "array(..., subok=True) only partially implemented")
        # we have a ndarray, but need to copy or change dtype
        if dtype is None:
            dtype = w_object.get_dtype()
        if dtype != w_object.get_dtype():
            # silently reject the copy value
            copy = True
        if copy:
            shape = w_object.get_shape()
            w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
            if support.product(shape) == 1:
                w_arr.set_scalar_value(dtype.coerce(space,
                        w_object.implementation.getitem(0)))
            else:
                loop.setslice(space, shape, w_arr.implementation, w_object.implementation)
            return w_arr
        else:
            imp = w_object.implementation
            w_base = w_object
            if imp.base() is not None:
                w_base = imp.base()
            with imp as storage:
                sz = support.product(w_object.get_shape()) * dtype.elsize
                return W_NDimArray.from_shape_and_storage(space,
                    w_object.get_shape(), storage, dtype, storage_bytes=sz,
                    w_base=w_base, start=imp.start)
    else:
        # not an array
        shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype)
    if dtype is None and space.isinstance_w(w_object, space.w_buffer):
        dtype = descriptor.get_dtype_cache(space).w_uint8dtype
    if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
        dtype = find_dtype_for_seq(space, elems_w, dtype)
        if dtype is None:
            dtype = descriptor.get_dtype_cache(space).w_float64dtype
        elif dtype.is_str_or_unicode() and dtype.elsize < 1:
            # promote S0 -> S1, U0 -> U1
            dtype = descriptor.variable_dtype(space, dtype.char + '1')

    w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
    if support.product(shape) == 1: # safe from overflow since from_shape checks
        w_arr.set_scalar_value(dtype.coerce(space, elems_w[0]))
    else:
        loop.assign(space, w_arr, elems_w)
    return w_arr
Esempio n. 18
0
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False):

    from pypy.module.micronumpy.boxes import W_GenericBox
    # numpy testing calls array(type(array([]))) and expects a ValueError
    if space.isinstance_w(w_object, space.w_type):
        raise oefmt(space.w_ValueError, "cannot create ndarray from type instance")
    # for anything that isn't already an array, try __array__ method first
    dtype = descriptor.decode_w_dtype(space, w_dtype)
    if not isinstance(w_object, W_NDimArray):
        w_array = try_array_method(space, w_object, w_dtype)
        if w_array is None:
            if (    not space.isinstance_w(w_object, space.w_str) and 
                    not space.isinstance_w(w_object, space.w_unicode) and
                    not isinstance(w_object, W_GenericBox)):
                # use buffer interface
                w_object = _array_from_buffer_3118(space, w_object, dtype)
        else:
            # continue with w_array, but do further operations in place
            w_object = w_array
            copy = False
            dtype = w_object.get_dtype()
    if not isinstance(w_object, W_NDimArray):
        w_array, _copy = try_interface_method(space, w_object, copy)
        if w_array is not None:
            w_object = w_array
            copy = _copy
            dtype = w_object.get_dtype()

    if isinstance(w_object, W_NDimArray):
        npy_order = order_converter(space, w_order, NPY.ANYORDER)
        if (dtype is None or w_object.get_dtype() is dtype) and (subok or
                type(w_object) is W_NDimArray):
            flags = w_object.get_flags()
            must_copy = copy
            must_copy |= (npy_order == NPY.CORDER and not flags & NPY.ARRAY_C_CONTIGUOUS)
            must_copy |= (npy_order == NPY.FORTRANORDER and not flags & NPY.ARRAY_F_CONTIGUOUS)
            if must_copy:
                return w_object.descr_copy(space, space.wrap(npy_order))
            else:
                return w_object
        if subok and not type(w_object) is W_NDimArray:
            raise oefmt(space.w_NotImplementedError,
                "array(..., subok=True) only partially implemented")
        # we have a ndarray, but need to copy or change dtype
        if dtype is None:
            dtype = w_object.get_dtype()
        if dtype != w_object.get_dtype():
            # silently reject the copy value
            copy = True
        if copy:
            shape = w_object.get_shape()
            order = support.get_order_as_CF(w_object.get_order(), npy_order)
            w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
            if support.product(shape) == 1:
                w_arr.set_scalar_value(dtype.coerce(space,
                        w_object.implementation.getitem(0)))
            else:
                loop.setslice(space, shape, w_arr.implementation, w_object.implementation)
            return w_arr
        else:
            imp = w_object.implementation
            w_base = w_object
            sz = w_base.get_size() * dtype.elsize
            if imp.base() is not None:
                w_base = imp.base()
                if type(w_base) is W_NDimArray:
                    sz = w_base.get_size() * dtype.elsize
                else:
                    # this must succeed (mmap, buffer, ...)
                    sz = space.int_w(space.call_method(w_base, 'size'))
            with imp as storage:
                return W_NDimArray.from_shape_and_storage(space,
                    w_object.get_shape(), storage, dtype, storage_bytes=sz,
                    w_base=w_base, strides=imp.strides, start=imp.start)
    else:
        # not an array
        npy_order = order_converter(space, w_order, NPY.CORDER)
        shape, elems_w = find_shape_and_elems(space, w_object, dtype)
    if dtype is None and space.isinstance_w(w_object, space.w_buffer):
        dtype = descriptor.get_dtype_cache(space).w_uint8dtype
    if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
        dtype = find_dtype_for_seq(space, elems_w, dtype)

    w_arr = W_NDimArray.from_shape(space, shape, dtype, order=npy_order)
    if support.product(shape) == 1: # safe from overflow since from_shape checks
        w_arr.set_scalar_value(dtype.coerce(space, elems_w[0]))
    else:
        loop.assign(space, w_arr, elems_w)
    return w_arr
Esempio n. 19
0
 def copy(self, space):
     strides, backstrides = calc_strides(self.get_shape(), self.dtype,
                                         self.order)
     impl = ConcreteArray(self.get_shape(), self.dtype, self.order, strides,
                          backstrides)
     return loop.setslice(space, self.get_shape(), impl, self)
Esempio n. 20
0
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False):
    from pypy.module.micronumpy import strides

    # for anything that isn't already an array, try __array__ method first
    if not isinstance(w_object, W_NDimArray):
        w_array = try_array_method(space, w_object, w_dtype)
        if w_array is not None:
            # continue with w_array, but do further operations in place
            w_object = w_array
            copy = False
    if not isinstance(w_object, W_NDimArray):
        w_array = try_interface_method(space, w_object)
        if w_array is not None:
            w_object = w_array
            copy = False
    dtype = descriptor.decode_w_dtype(space, w_dtype)

    if space.is_none(w_order):
        order = 'C'
    else:
        order = space.str_w(w_order)
        if order == 'K':
            order = 'C'
        if order != 'C':  # or order != 'F':
            raise oefmt(space.w_ValueError, "Unknown order: %s", order)

    if isinstance(w_object, W_NDimArray):
        if (dtype is None or w_object.get_dtype() is dtype):
            if copy and (subok or type(w_object) is W_NDimArray):
                return w_object.descr_copy(space, w_order)
            elif not copy and (subok or type(w_object) is W_NDimArray):
                return w_object
        if subok and not type(w_object) is W_NDimArray:
            raise oefmt(space.w_NotImplementedError,
                "array(..., subok=True) only partially implemented")
        # we have a ndarray, but need to copy or change dtype
        if dtype is None:
            dtype = w_object.get_dtype()
        if dtype != w_object.get_dtype():
            # silently reject the copy value
            copy = True
        if copy:
            shape = w_object.get_shape()
            w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
            if support.product(shape) == 1:
                w_arr.set_scalar_value(dtype.coerce(space,
                        w_object.implementation.getitem(0)))
            else:
                loop.setslice(space, shape, w_arr.implementation, w_object.implementation)
            return w_arr
        else:
            imp = w_object.implementation
            w_base = imp.base() or w_object
            with imp as storage:
                sz = support.product(w_object.get_shape()) * dtype.elsize
                return W_NDimArray.from_shape_and_storage(space,
                    w_object.get_shape(), storage, dtype, storage_bytes=sz,
                    w_base=w_base, start=imp.start)
    else:
        # not an array
        shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype)
    if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
        dtype = find_dtype_for_seq(space, elems_w, dtype)
        if dtype is None:
            dtype = descriptor.get_dtype_cache(space).w_float64dtype
        elif dtype.is_str_or_unicode() and dtype.elsize < 1:
            # promote S0 -> S1, U0 -> U1
            dtype = descriptor.variable_dtype(space, dtype.char + '1')

    w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
    if support.product(shape) == 1:
        w_arr.set_scalar_value(dtype.coerce(space, elems_w[0]))
    else:
        loop.assign(space, w_arr, elems_w)
    return w_arr
Esempio n. 21
0
 def copy(self, space):
     strides, backstrides = calc_strides(self.get_shape(), self.dtype,
                                                 self.order)
     impl = ConcreteArray(self.get_shape(), self.dtype, self.order, strides,
                          backstrides)
     return loop.setslice(space, self.get_shape(), impl, self)
Esempio n. 22
0
def _array(space,
           w_object,
           w_dtype=None,
           copy=True,
           w_order=None,
           subok=False):

    from pypy.module.micronumpy.boxes import W_GenericBox
    # numpy testing calls array(type(array([]))) and expects a ValueError
    if space.isinstance_w(w_object, space.w_type):
        raise oefmt(space.w_ValueError,
                    "cannot create ndarray from type instance")
    # for anything that isn't already an array, try __array__ method first
    dtype = descriptor.decode_w_dtype(space, w_dtype)
    if not isinstance(w_object, W_NDimArray):
        w_array = try_array_method(space, w_object, w_dtype)
        if w_array is None:
            if (not space.isinstance_w(w_object, space.w_bytes)
                    and not space.isinstance_w(w_object, space.w_unicode)
                    and not isinstance(w_object, W_GenericBox)):
                # use buffer interface
                w_object = _array_from_buffer_3118(space, w_object, dtype)
        else:
            # continue with w_array, but do further operations in place
            w_object = w_array
            copy = False
            dtype = w_object.get_dtype()
    if not isinstance(w_object, W_NDimArray):
        w_array, _copy = try_interface_method(space, w_object, copy)
        if w_array is not None:
            w_object = w_array
            copy = _copy
            dtype = w_object.get_dtype()

    if isinstance(w_object, W_NDimArray):
        npy_order = order_converter(space, w_order, NPY.ANYORDER)
        if (dtype is None or w_object.get_dtype() is dtype) and (
                subok or type(w_object) is W_NDimArray):
            flags = w_object.get_flags()
            must_copy = copy
            must_copy |= (npy_order == NPY.CORDER
                          and not flags & NPY.ARRAY_C_CONTIGUOUS)
            must_copy |= (npy_order == NPY.FORTRANORDER
                          and not flags & NPY.ARRAY_F_CONTIGUOUS)
            if must_copy:
                return w_object.descr_copy(space, space.newint(npy_order))
            else:
                return w_object
        if subok and not type(w_object) is W_NDimArray:
            raise oefmt(space.w_NotImplementedError,
                        "array(..., subok=True) only partially implemented")
        # we have a ndarray, but need to copy or change dtype
        if dtype is None:
            dtype = w_object.get_dtype()
        if dtype != w_object.get_dtype():
            # silently reject the copy value
            copy = True
        if copy:
            shape = w_object.get_shape()
            order = support.get_order_as_CF(w_object.get_order(), npy_order)
            w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
            if support.product(shape) == 1:
                w_arr.set_scalar_value(
                    dtype.coerce(space, w_object.implementation.getitem(0)))
            else:
                loop.setslice(space, shape, w_arr.implementation,
                              w_object.implementation)
            return w_arr
        else:
            imp = w_object.implementation
            w_base = w_object
            sz = w_base.get_size() * dtype.elsize
            if imp.base() is not None:
                w_base = imp.base()
                if type(w_base) is W_NDimArray:
                    sz = w_base.get_size() * dtype.elsize
                else:
                    # this must succeed (mmap, buffer, ...)
                    sz = space.int_w(space.call_method(w_base, 'size'))
            with imp as storage:
                return W_NDimArray.from_shape_and_storage(space,
                                                          w_object.get_shape(),
                                                          storage,
                                                          dtype,
                                                          storage_bytes=sz,
                                                          w_base=w_base,
                                                          strides=imp.strides,
                                                          start=imp.start)
    else:
        # not an array
        npy_order = order_converter(space, w_order, NPY.CORDER)
        shape, elems_w = find_shape_and_elems(space, w_object, dtype)
    if dtype is None and space.isinstance_w(w_object, space.w_buffer):
        dtype = descriptor.get_dtype_cache(space).w_uint8dtype
    if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
        dtype = find_dtype_for_seq(space, elems_w, dtype)

    w_arr = W_NDimArray.from_shape(space, shape, dtype, order=npy_order)
    if support.product(
            shape) == 1:  # safe from overflow since from_shape checks
        w_arr.set_scalar_value(dtype.coerce(space, elems_w[0]))
    else:
        loop.assign(space, w_arr, elems_w)
    return w_arr