Esempio n. 1
0
def _zeros_or_empty(space, w_shape, w_dtype, w_order, zero):
    dtype = space.interp_w(descriptor.W_Dtype,
        space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
    if dtype.is_str_or_unicode() and dtype.elsize < 1:
        dtype = descriptor.variable_dtype(space, dtype.char + '1')
    shape = shape_converter(space, w_shape, dtype)
    for dim in shape:
        if dim < 0:
            raise OperationError(space.w_ValueError, space.wrap(
                "negative dimensions are not allowed"))
    try:
        support.product_check(shape)
    except OverflowError:
        raise oefmt(space.w_ValueError, "array is too big.")
    return W_NDimArray.from_shape(space, shape, dtype=dtype, zero=zero)
Esempio n. 2
0
 def __init__(self, start, strides, backstrides, shape, parent, orig_arr,
              dtype=None):
     self.strides = strides
     self.backstrides = backstrides
     self.shape = shape
     if dtype is None:
         dtype = parent.dtype
     if isinstance(parent, SliceArray):
         parent = parent.parent # one level only
     self.parent = parent
     self.storage = parent.storage
     self.gcstruct = parent.gcstruct
     if parent.order not in (NPY.CORDER, NPY.FORTRANORDER):
         raise oefmt(dtype.itemtype.space.w_ValueError, "SliceArray but parent order is not 0,1 rather %d", parent.order)
     self.order = parent.order
     self.dtype = dtype
     try:
         self.size = ovfcheck(support.product_check(shape) * self.dtype.elsize)
     except OverflowError:
         raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big.")
     self.start = start
     self.orig_arr = orig_arr
     flags = parent.flags & NPY.ARRAY_ALIGNED
     flags |= parent.flags & NPY.ARRAY_WRITEABLE
     if is_c_contiguous(self):
         flags |= NPY.ARRAY_C_CONTIGUOUS
     if is_f_contiguous(self):
         flags |= NPY.ARRAY_F_CONTIGUOUS
     self.flags = flags
Esempio n. 3
0
 def __init__(self, shape, dtype, order, strides, backstrides,
              storage=lltype.nullptr(RAW_STORAGE), zero=True):
     gcstruct = V_OBJECTSTORE
     flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
     try:
         length = support.product_check(shape)
         self.size = ovfcheck(length * dtype.elsize)
     except OverflowError:
         raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big.")
     if storage == lltype.nullptr(RAW_STORAGE):
         if dtype.num == NPY.OBJECT:
             storage = dtype.itemtype.malloc(length * dtype.elsize, zero=True)
             gcstruct = _create_objectstore(storage, length, dtype.elsize)
         else:
             storage = dtype.itemtype.malloc(length * dtype.elsize, zero=zero)
         flags |= NPY.ARRAY_OWNDATA
     start = calc_start(shape, strides)
     ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides,
                                     storage, start=start)
     self.gcstruct = gcstruct
     if is_c_contiguous(self):
         flags |= NPY.ARRAY_C_CONTIGUOUS
     if is_f_contiguous(self):
         flags |= NPY.ARRAY_F_CONTIGUOUS
     self.flags = flags
Esempio n. 4
0
    def __init__(self, space, args):
        num_args = len(args)
        if not (2 <= num_args <= NPY.MAXARGS):
            raise oefmt(space.w_ValueError,
                                 "Need at least two and fewer than (%d) array objects.", NPY.MAXARGS)

        self.seq = [convert_to_array(space, w_elem)
                    for w_elem in args]

        self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
                                     len(self.seq), parse_op_flag)

        self.shape = shape_agreement_multiple(space, self.seq, shape=None)
        self.order = NPY.CORDER

        self.iters = []
        self.index = 0

        try:
            self.size = support.product_check(self.shape)
        except OverflowError as e:
            raise oefmt(space.w_ValueError, "broadcast dimensions too large.")
        for i in range(len(self.seq)):
            it = self.get_iter(space, i)
            it.contiguous = False
            self.iters.append((it, it.reset()))

        self.done = False
        pass
Esempio n. 5
0
    def __init__(self, space, args):
        num_args = len(args)
        if not (2 <= num_args <= NPY.MAXARGS):
            raise oefmt(
                space.w_ValueError,
                "Need at least two and fewer than (%d) array objects.",
                NPY.MAXARGS)

        self.seq = [convert_to_array(space, w_elem) for w_elem in args]

        self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
                                     len(self.seq), parse_op_flag)

        self.shape = shape_agreement_multiple(space, self.seq, shape=None)
        self.order = NPY.CORDER

        self.iters = []
        self.index = 0

        try:
            self.size = support.product_check(self.shape)
        except OverflowError as e:
            raise oefmt(space.w_ValueError, "broadcast dimensions too large.")
        for i in range(len(self.seq)):
            it = self.get_iter(space, i)
            it.contiguous = False
            self.iters.append((it, it.reset()))

        self.done = False
        pass
Esempio n. 6
0
 def from_shape(space,
                shape,
                dtype,
                order=NPY.CORDER,
                w_instance=None,
                zero=True):
     from pypy.module.micronumpy import concrete, descriptor, boxes
     from pypy.module.micronumpy.strides import calc_strides
     if len(shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
                     "sequence too large; cannot be greater than %d",
                     NPY.MAXDIMS)
     try:
         ovfcheck(support.product_check(shape) * dtype.elsize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     strides, backstrides = calc_strides(shape, dtype.base, order)
     impl = concrete.ConcreteArray(shape,
                                   dtype.base,
                                   order,
                                   strides,
                                   backstrides,
                                   zero=zero)
     if dtype == descriptor.get_dtype_cache(space).w_objectdtype:
         impl.fill(space, boxes.W_ObjectBox(space.w_None))
     if w_instance:
         return wrap_impl(space, space.type(w_instance), w_instance, impl)
     return W_NDimArray(impl)
Esempio n. 7
0
 def __init__(self, start, strides, backstrides, shape, parent, orig_arr,
              dtype=None):
     self.strides = strides
     self.backstrides = backstrides
     self.shape = shape
     if dtype is None:
         dtype = parent.dtype
     if isinstance(parent, SliceArray):
         parent = parent.parent # one level only
     self.parent = parent
     self.storage = parent.storage
     self.gcstruct = parent.gcstruct
     if parent.order not in (NPY.CORDER, NPY.FORTRANORDER):
         raise oefmt(dtype.itemtype.space.w_ValueError, "SliceArray but parent order is not 0,1 rather %d", parent.order)
     self.order = parent.order
     self.dtype = dtype
     try:
         self.size = ovfcheck(support.product_check(shape) * self.dtype.elsize)
     except OverflowError:
         raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big.")
     self.start = start
     self.orig_arr = orig_arr
     flags = parent.flags & NPY.ARRAY_ALIGNED
     flags |= parent.flags & NPY.ARRAY_WRITEABLE
     if is_c_contiguous(self):
         flags |= NPY.ARRAY_C_CONTIGUOUS
     if is_f_contiguous(self):
         flags |= NPY.ARRAY_F_CONTIGUOUS
     self.flags = flags
Esempio n. 8
0
 def __init__(self, shape, dtype, order, strides, backstrides,
              storage=lltype.nullptr(RAW_STORAGE), zero=True):
     gcstruct = V_OBJECTSTORE
     flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
     try:
         length = support.product_check(shape)
         self.size = ovfcheck(length * dtype.elsize)
     except OverflowError: 
         raise oefmt(dtype.itemtype.space.w_ValueError, "array is too big.")
     if storage == lltype.nullptr(RAW_STORAGE):
         if dtype.num == NPY.OBJECT:
             storage = dtype.itemtype.malloc(length * dtype.elsize, zero=True)
             gcstruct = _create_objectstore(storage, length, dtype.elsize)
         else:
             storage = dtype.itemtype.malloc(length * dtype.elsize, zero=zero)
         flags |= NPY.ARRAY_OWNDATA
     start = calc_start(shape, strides)
     ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides,
                                     storage, start=start)
     self.gcstruct = gcstruct
     if is_c_contiguous(self):
         flags |= NPY.ARRAY_C_CONTIGUOUS
     if is_f_contiguous(self):
         flags |= NPY.ARRAY_F_CONTIGUOUS
     self.flags = flags
Esempio n. 9
0
def _zeros_or_empty(space, w_shape, w_dtype, w_order, zero):
    # w_order can be None, str, or boolean
    order = order_converter(space, w_order, NPY.CORDER)
    dtype = space.interp_w(descriptor.W_Dtype,
        space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
    if dtype.is_str_or_unicode() and dtype.elsize < 1:
        dtype = descriptor.variable_dtype(space, dtype.char + '1')
    shape = shape_converter(space, w_shape, dtype)
    for dim in shape:
        if dim < 0:
            raise OperationError(space.w_ValueError, space.wrap(
                "negative dimensions are not allowed"))
    try:
        support.product_check(shape)
    except OverflowError:
        raise oefmt(space.w_ValueError, "array is too big.")
    return W_NDimArray.from_shape(space, shape, dtype, order, zero=zero)
Esempio n. 10
0
 def from_shape_and_storage(space, shape, storage, dtype, storage_bytes=-1,
                            order='C', owning=False, w_subtype=None,
                            w_base=None, writable=True, strides=None, start=0):
     from pypy.module.micronumpy import concrete
     from pypy.module.micronumpy.strides import (calc_strides,
                                                 calc_backstrides)
     isize = dtype.elsize
     if len(shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; must be smaller than %d", NPY.MAXDIMS)
     try:
         totalsize = ovfcheck(support.product_check(shape) * isize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     if storage_bytes > 0 :
         if totalsize > storage_bytes:
             raise OperationError(space.w_TypeError, space.wrap(
                 "buffer is too small for requested array"))
     else:
         storage_bytes = totalsize
     if strides is None:
         strides, backstrides = calc_strides(shape, dtype, order)
     else:
         if len(strides) != len(shape):
             raise oefmt(space.w_ValueError,
                 'strides, if given, must be the same length as shape')
         for i in range(len(strides)):
             if strides[i] < 0 or strides[i]*shape[i] > storage_bytes:
                 raise oefmt(space.w_ValueError,
                     'strides is incompatible with shape of requested '
                     'array and size of buffer')
         backstrides = calc_backstrides(strides, shape)
     if w_base is not None:
         if owning:
             raise OperationError(space.w_ValueError,
                     space.wrap("Cannot have owning=True when specifying a buffer"))
         if writable:
             impl = concrete.ConcreteArrayWithBase(shape, dtype, order,
                                 strides, backstrides, storage, w_base,
                                 start=start)
         else:
             impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, order,
                                                              strides, backstrides,
                                                              storage, w_base)
     elif owning:
         # Will free storage when GCd
         impl = concrete.ConcreteArray(shape, dtype, order, strides,
                                       backstrides, storage=storage)
     else:
         impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides,
                                                backstrides, storage)
     if w_subtype:
         w_ret = space.allocate_instance(W_NDimArray, w_subtype)
         W_NDimArray.__init__(w_ret, impl)
         space.call_method(w_ret, '__array_finalize__', w_subtype)
         return w_ret
     return W_NDimArray(impl)
Esempio n. 11
0
 def set_shape(self, space, orig_array, new_shape):
     if len(new_shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
     try:
         ovfcheck(support.product_check(new_shape) * self.dtype.elsize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     strides, backstrides = calc_strides(new_shape, self.dtype,
                                                 self.order)
     return SliceArray(self.start, strides, backstrides, new_shape, self,
                       orig_array)
Esempio n. 12
0
 def set_shape(self, space, orig_array, new_shape):
     if len(new_shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
     try:
         ovfcheck(support.product_check(new_shape) * self.dtype.elsize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     strides, backstrides = calc_strides(new_shape, self.dtype,
                                                 self.order)
     return SliceArray(self.start, strides, backstrides, new_shape, self,
                       orig_array)
Esempio n. 13
0
 def from_shape(space, shape, dtype, order='C', w_instance=None, zero=True):
     from pypy.module.micronumpy import concrete, descriptor, boxes
     from pypy.module.micronumpy.strides import calc_strides
     if len(shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; must be smaller than %d", NPY.MAXDIMS)
     try:
         ovfcheck(support.product_check(shape) * dtype.elsize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     strides, backstrides = calc_strides(shape, dtype.base, order)
     impl = concrete.ConcreteArray(shape, dtype.base, order, strides,
                                   backstrides, zero=zero)
     if dtype == descriptor.get_dtype_cache(space).w_objectdtype:
         impl.fill(space, boxes.W_ObjectBox(space.w_None))
     if w_instance:
         return wrap_impl(space, space.type(w_instance), w_instance, impl)
     return W_NDimArray(impl)
Esempio n. 14
0
 def set_shape(self, space, orig_array, new_shape):
     if len(new_shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
     try:
         ovfcheck(support.product_check(new_shape) * self.dtype.elsize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     if len(self.get_shape()) < 2 or self.size == 0:
         # TODO: this code could be refactored into calc_strides
         # but then calc_strides would have to accept a stepping factor
         strides = []
         backstrides = []
         dtype = self.dtype
         try:
             s = self.get_strides()[0] // dtype.elsize
         except IndexError:
             s = 1
         if self.order != NPY.FORTRANORDER:
             new_shape.reverse()
         for sh in new_shape:
             strides.append(s * dtype.elsize)
             backstrides.append(s * (sh - 1) * dtype.elsize)
             s *= max(1, sh)
         if self.order != NPY.FORTRANORDER:
             strides.reverse()
             backstrides.reverse()
             new_shape.reverse()
         return self.__class__(self.start, strides, backstrides, new_shape,
                           self, orig_array)
     new_strides = calc_new_strides(new_shape, self.get_shape(),
                                    self.get_strides(),
                                    self.order)
     if new_strides is None or len(new_strides) != len(new_shape):
         raise oefmt(space.w_AttributeError,
             "incompatible shape for a non-contiguous array")
     new_backstrides = [0] * len(new_shape)
     for nd in range(len(new_shape)):
         new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd]
     return self.__class__(self.start, new_strides, new_backstrides, new_shape,
                       self, orig_array)
Esempio n. 15
0
 def set_shape(self, space, orig_array, new_shape):
     if len(new_shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
     try:
         ovfcheck(support.product_check(new_shape) * self.dtype.elsize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     if len(self.get_shape()) < 2 or self.size == 0:
         # TODO: this code could be refactored into calc_strides
         # but then calc_strides would have to accept a stepping factor
         strides = []
         backstrides = []
         dtype = self.dtype
         try:
             s = self.get_strides()[0] // dtype.elsize
         except IndexError:
             s = 1
         if self.order != NPY.FORTRANORDER:
             new_shape.reverse()
         for sh in new_shape:
             strides.append(s * dtype.elsize)
             backstrides.append(s * (sh - 1) * dtype.elsize)
             s *= max(1, sh)
         if self.order != NPY.FORTRANORDER:
             strides.reverse()
             backstrides.reverse()
             new_shape.reverse()
         return self.__class__(self.start, strides, backstrides, new_shape,
                           self, orig_array)
     new_strides = calc_new_strides(new_shape, self.get_shape(),
                                    self.get_strides(),
                                    self.order)
     if new_strides is None or len(new_strides) != len(new_shape):
         raise oefmt(space.w_AttributeError,
             "incompatible shape for a non-contiguous array")
     new_backstrides = [0] * len(new_shape)
     for nd in range(len(new_shape)):
         new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd]
     return self.__class__(self.start, new_strides, new_backstrides, new_shape,
                       self, orig_array)
Esempio n. 16
0
 def from_shape_and_storage(space, shape, storage, dtype, storage_bytes=-1,
                            order=NPY.CORDER, owning=False, w_subtype=None,
                            w_base=None, writable=True, strides=None,
                            start=0):
     from pypy.module.micronumpy import concrete
     from pypy.module.micronumpy.strides import (calc_strides,
                                                 calc_backstrides)
     isize = dtype.elsize
     if len(shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
             "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
     try:
         totalsize = ovfcheck(support.product_check(shape) * isize)
     except OverflowError as e:
         raise oefmt(space.w_ValueError, "array is too big.")
     if storage_bytes > 0 :
         if totalsize > storage_bytes:
             raise oefmt(space.w_TypeError,
                         "buffer is too small for requested array")
     else:
         storage_bytes = totalsize
     if strides is None:
         strides, backstrides = calc_strides(shape, dtype, order)
     else:
         if len(strides) != len(shape):
             raise oefmt(space.w_ValueError,
                 'strides, if given, must be the same length as shape')
         last = 0
         for i in range(len(strides)):
             last += (shape[i] - 1) * strides[i]
         if last > storage_bytes or start < 0 or \
                 start + dtype.elsize > storage_bytes:
             raise oefmt(space.w_ValueError,
                 'strides is incompatible with shape of requested '
                 'array and size of buffer')
         backstrides = calc_backstrides(strides, shape)
     if w_base is not None:
         if owning:
             raise oefmt(space.w_ValueError,
                         "Cannot have owning=True when specifying a buffer")
         if writable:
             impl = concrete.ConcreteArrayWithBase(shape, dtype, order,
                                 strides, backstrides, storage, w_base,
                                 start=start)
         else:
             impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, order,
                                                              strides, backstrides,
                                                              storage, w_base)
     elif owning:
         # Will free storage when GCd
         impl = concrete.ConcreteArray(shape, dtype, order, strides,
                                       backstrides, storage=storage)
     else:
         impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides,
                                                backstrides, storage)
     if w_subtype:
         w_ret = space.allocate_instance(W_NDimArray, w_subtype)
         W_NDimArray.__init__(w_ret, impl)
         space.call_method(w_ret, '__array_finalize__', w_subtype)
         return w_ret
     return W_NDimArray(impl)