Example #1
0
def _PyArray_FromAny(space, w_obj, w_dtype, min_depth, max_depth, requirements,
                     context):
    """ This is the main function used to obtain an array from any nested
         sequence, or object that exposes the array interface, op. The
         parameters allow specification of the required dtype, the
         minimum (min_depth) and maximum (max_depth) number of dimensions
         acceptable, and other requirements for the array.

         The dtype argument needs to be a PyArray_Descr structure indicating
         the desired data-type (including required byteorder). The dtype
         argument may be NULL, indicating that any data-type (and byteorder)
         is acceptable.
         Unless FORCECAST is present in flags, this call will generate an error
         if the data type cannot be safely obtained from the object. If you
         want to use NULL for the dtype and ensure the array is notswapped then
         use PyArray_CheckFromAny.

         A value of 0 for either of the depth parameters causes the parameter
         to be ignored.

         Any of the following array flags can be added (e.g. using |) to get
         the requirements argument. If your code can handle general (e.g.
         strided, byte-swapped, or unaligned arrays) then requirements
         may be 0. Also, if op is not already an array (or does not expose
         the array interface), then a new array will be created (and filled
         from op using the sequence protocol). The new array will have
         NPY_DEFAULT as its flags member.

         The context argument is passed to the __array__ method of op and is
         only used if the array is constructed that way. Almost always this
         parameter is NULL.
    """
    if requirements not in (0, NPY_DEFAULT):
        raise OperationError(
            space.w_NotImplementedError,
            space.wrap(
                '_PyArray_FromAny called with not-implemented requirements argument'
            ))
    w_array = array(space, w_obj, w_dtype=w_dtype, copy=False)
    if min_depth != 0 and len(w_array.get_shape()) < min_depth:
        raise OperationError(
            space.w_ValueError,
            space.wrap('object of too small depth for desired array'))
    elif max_depth != 0 and len(w_array.get_shape()) > max_depth:
        raise OperationError(
            space.w_ValueError,
            space.wrap('object of too deep for desired array'))
    elif w_array.is_scalar():
        # since PyArray_DATA() fails on scalars, create a 1D array and set empty
        # shape. So the following combination works for *reading* scalars:
        #     PyObject *arr = PyArray_FromAny(obj);
        #     int nd = PyArray_NDIM(arr);
        #     void *data = PyArray_DATA(arr);
        impl = w_array.implementation
        w_array = W_NDimArray.from_shape(space, [1], impl.dtype)
        w_array.implementation.setitem(0, impl.getitem(impl.start + 0))
        w_array.implementation.shape = []
    return w_array
Example #2
0
 def descr__new__(space, w_subtype, w_value=None):
     from pypy.module.micronumpy.ctors import array
     dtype = _get_dtype(space)
     if not space.is_none(w_value):
         w_arr = array(space, w_value, dtype, copy=False)
         if len(w_arr.get_shape()) != 0:
             return w_arr
         w_value = w_arr.get_scalar_value().item(space)
     return dtype.itemtype.coerce_subtype(space, w_subtype, w_value)
Example #3
0
 def descr__new__(space, w_subtype, w_value=None):
     from pypy.module.micronumpy.ctors import array
     dtype = _get_dtype(space)
     if not space.is_none(w_value):
         w_arr = array(space, w_value, dtype, copy=False)
         if len(w_arr.get_shape()) != 0:
             return w_arr
         w_value = w_arr.get_scalar_value().item(space)
     return dtype.itemtype.coerce_subtype(space, w_subtype, w_value)
Example #4
0
def _PyArray_FromAny(space, w_obj, w_dtype, min_depth, max_depth, requirements, context):
    """ This is the main function used to obtain an array from any nested
         sequence, or object that exposes the array interface, op. The
         parameters allow specification of the required dtype, the
         minimum (min_depth) and maximum (max_depth) number of dimensions
         acceptable, and other requirements for the array.

         The dtype argument needs to be a PyArray_Descr structure indicating
         the desired data-type (including required byteorder). The dtype
         argument may be NULL, indicating that any data-type (and byteorder)
         is acceptable.
         Unless FORCECAST is present in flags, this call will generate an error
         if the data type cannot be safely obtained from the object. If you
         want to use NULL for the dtype and ensure the array is notswapped then
         use PyArray_CheckFromAny.

         A value of 0 for either of the depth parameters causes the parameter
         to be ignored.

         Any of the following array flags can be added (e.g. using |) to get
         the requirements argument. If your code can handle general (e.g.
         strided, byte-swapped, or unaligned arrays) then requirements
         may be 0. Also, if op is not already an array (or does not expose
         the array interface), then a new array will be created (and filled
         from op using the sequence protocol). The new array will have
         NPY_DEFAULT as its flags member.

         The context argument is passed to the __array__ method of op and is
         only used if the array is constructed that way. Almost always this
         parameter is NULL.
    """
    if requirements not in (0, NPY_DEFAULT):
        raise OperationError(space.w_NotImplementedError, space.wrap(
            '_PyArray_FromAny called with not-implemented requirements argument'))
    w_array = array(space, w_obj, w_dtype=w_dtype, copy=False)
    if min_depth !=0 and len(w_array.get_shape()) < min_depth:
        raise OperationError(space.w_ValueError, space.wrap(
            'object of too small depth for desired array'))
    elif max_depth !=0 and len(w_array.get_shape()) > max_depth:
        raise OperationError(space.w_ValueError, space.wrap(
            'object of too deep for desired array'))
    elif w_array.is_scalar():
        # since PyArray_DATA() fails on scalars, create a 1D array and set empty
        # shape. So the following combination works for *reading* scalars:
        #     PyObject *arr = PyArray_FromAny(obj);
        #     int nd = PyArray_NDIM(arr);
        #     void *data = PyArray_DATA(arr);
        impl = w_array.implementation
        w_array = W_NDimArray.from_shape(space, [1], impl.dtype)
        w_array.implementation.setitem(0, impl.getitem(impl.start + 0))
        w_array.implementation.shape = []
    return w_array
Example #5
0
def convert_to_array(space, w_obj):
    from pypy.module.micronumpy.ctors import array
    if isinstance(w_obj, W_NDimArray):
        return w_obj
    return array(space, w_obj)
Example #6
0
 def execute(self, interp):
     w_list = self.wrap(interp.space)
     return array(interp.space, w_list)
Example #7
0
 def execute(self, interp):
     w_list = interp.space.newlist(
         [interp.space.newfloat(float(i)) for i in range(self.v)])
     dtype = get_dtype_cache(interp.space).w_float64dtype
     return array(interp.space, w_list, w_dtype=dtype, w_order=None)
Example #8
0
def convert_to_array(space, w_obj):
    from pypy.module.micronumpy.ctors import array
    if isinstance(w_obj, W_NDimArray):
        return w_obj
    return array(space, w_obj)
Example #9
0
 def execute(self, interp):
     w_list = self.wrap(interp.space)
     return array(interp.space, w_list)
Example #10
0
 def execute(self, interp):
     w_list = interp.space.newlist(
         [interp.space.wrap(float(i)) for i in range(self.v)]
     )
     dtype = get_dtype_cache(interp.space).w_float64dtype
     return array(interp.space, w_list, w_dtype=dtype, w_order=None)