numpy  2.0.0
src/multiarray/ctors.c File Reference
#include <Python.h>
#include "structmember.h"
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "numpy/npy_math.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "multiarraymodule.h"
#include "common.h"
#include "ctors.h"
#include "convert_datatype.h"
#include "shape.h"
#include "buffer.h"
#include "numpymemoryview.h"
#include "lowlevel_strided_loops.h"
#include "methods.h"
#include "_datetime.h"
#include "datetime_strings.h"
#include "array_assign.h"
#include "mapping.h"
#include "templ_common.h"
#include "alloc.h"
#include <assert.h>

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE
#define _COPY_N_SIZE(size)
#define PyIntOrLong_Check(obj)   (PyInt_Check(obj) || PyLong_Check(obj))
#define FROM_BUFFER_SIZE   4096

Typedefs

typedef int(* next_element )(void **, void *, PyArray_Descr *, void *)
typedef int(* skip_separator )(void **, const char *, void *)

Functions

static int fromstr_next_element (char **s, void *dptr, PyArray_Descr *dtype, const char *end)
static int fromfile_next_element (FILE **fp, void *dptr, PyArray_Descr *dtype, void *NPY_UNUSED(stream_data))
static char * swab_separator (const char *sep)
static int fromstr_skip_separator (char **s, const char *sep, const char *end)
static int fromfile_skip_separator (FILE **fp, const char *sep, void *NPY_UNUSED(stream_data))
static int _update_descr_and_dimensions (PyArray_Descr **des, npy_intp *newdims, npy_intp *newstrides, int oldnd)
NPY_NO_EXPORT void _unaligned_strided_byte_copy (char *dst, npy_intp outstrides, char *src, npy_intp instrides, npy_intp N, int elsize)
NPY_NO_EXPORT void _strided_byte_swap (void *p, npy_intp stride, npy_intp n, int size)
NPY_NO_EXPORT void byte_swap_vector (void *p, npy_intp n, int size)
NPY_NO_EXPORT void copy_and_swap (void *dst, void *src, int itemsize, npy_intp numitems, npy_intp srcstrides, int swap)
static int setArrayFromSequence (PyArrayObject *a, PyObject *s, int dim, PyArrayObject *dst)
NPY_NO_EXPORT int PyArray_AssignFromSequence (PyArrayObject *self, PyObject *v)
static int discover_itemsize (PyObject *s, int nd, int *itemsize, int string_type)
static int discover_dimensions (PyObject *obj, int *maxndim, npy_intp *d, int check_it, int stop_at_string, int stop_at_tuple, int *out_is_object)
NPY_NO_EXPORT PyObject * PyArray_NewFromDescr_int (PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj, int zeroed, int allow_emptystring)
NPY_NO_EXPORT PyObject * PyArray_NewFromDescr (PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj)
NPY_NO_EXPORT PyObject * PyArray_NewLikeArray (PyArrayObject *prototype, NPY_ORDER order, PyArray_Descr *dtype, int subok)
NPY_NO_EXPORT PyObject * PyArray_New (PyTypeObject *subtype, int nd, npy_intp *dims, int type_num, npy_intp *strides, void *data, int itemsize, int flags, PyObject *obj)
NPY_NO_EXPORT int _array_from_buffer_3118 (PyObject *obj, PyObject **out)
NPY_NO_EXPORT int PyArray_GetArrayParamsFromObject (PyObject *op, PyArray_Descr *requested_dtype, npy_bool writeable, PyArray_Descr **out_dtype, int *out_ndim, npy_intp *out_dims, PyArrayObject **out_arr, PyObject *context)
NPY_NO_EXPORT PyObject * PyArray_FromAny (PyObject *op, PyArray_Descr *newtype, int min_depth, int max_depth, int flags, PyObject *context)
NPY_NO_EXPORT PyObject * PyArray_CheckFromAny (PyObject *op, PyArray_Descr *descr, int min_depth, int max_depth, int requires, PyObject *context)
NPY_NO_EXPORT PyObject * PyArray_FromArray (PyArrayObject *arr, PyArray_Descr *newtype, int flags)
NPY_NO_EXPORT PyObject * PyArray_FromStructInterface (PyObject *input)
NPY_NO_EXPORT int _is_default_descr (PyObject *descr, PyObject *typestr)
NPY_NO_EXPORT PyObject * PyArray_FromInterface (PyObject *origin)
NPY_NO_EXPORT PyObject * PyArray_FromArrayAttr (PyObject *op, PyArray_Descr *typecode, PyObject *context)
NPY_NO_EXPORT PyArray_DescrPyArray_DescrFromObject (PyObject *op, PyArray_Descr *mintype)
NPY_NO_EXPORT PyObject * PyArray_FromDimsAndDataAndDescr (int nd, int *d, PyArray_Descr *descr, char *data)
NPY_NO_EXPORT PyObject * PyArray_FromDims (int nd, int *d, int type)
NPY_NO_EXPORT PyObject * PyArray_EnsureArray (PyObject *op)
NPY_NO_EXPORT PyObject * PyArray_EnsureAnyArray (PyObject *op)
NPY_NO_EXPORT int PyArray_CopyAsFlat (PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order)
NPY_NO_EXPORT int PyArray_CopyAnyInto (PyArrayObject *dst, PyArrayObject *src)
NPY_NO_EXPORT int PyArray_CopyInto (PyArrayObject *dst, PyArrayObject *src)
NPY_NO_EXPORT int PyArray_MoveInto (PyArrayObject *dst, PyArrayObject *src)
NPY_NO_EXPORT PyObject * PyArray_CheckAxis (PyArrayObject *arr, int *axis, int flags)
NPY_NO_EXPORT PyObject * PyArray_Zeros (int nd, npy_intp *dims, PyArray_Descr *type, int is_f_order)
NPY_NO_EXPORT PyObject * PyArray_Empty (int nd, npy_intp *dims, PyArray_Descr *type, int is_f_order)
static int _safe_ceil_to_intp (double value, npy_intp *ret)
NPY_NO_EXPORT PyObject * PyArray_Arange (double start, double stop, double step, int type_num)
static npy_intp _calc_length (PyObject *start, PyObject *stop, PyObject *step, PyObject **next, int cmplx)
NPY_NO_EXPORT PyObject * PyArray_ArangeObj (PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr *dtype)
static PyArrayObjectarray_fromfile_binary (FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nread)
static PyArrayObjectarray_from_text (PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread, void *stream, next_element next, skip_separator skip_sep, void *stream_data)
NPY_NO_EXPORT PyObject * PyArray_FromFile (FILE *fp, PyArray_Descr *dtype, npy_intp num, char *sep)
NPY_NO_EXPORT PyObject * PyArray_FromBuffer (PyObject *buf, PyArray_Descr *type, npy_intp count, npy_intp offset)
NPY_NO_EXPORT PyObject * PyArray_FromString (char *data, npy_intp slen, PyArray_Descr *dtype, npy_intp num, char *sep)
NPY_NO_EXPORT PyObject * PyArray_FromIter (PyObject *obj, PyArray_Descr *dtype, npy_intp count)
NPY_NO_EXPORT void _array_fill_strides (npy_intp *strides, npy_intp *dims, int nd, size_t itemsize, int inflag, int *objflags)
NPY_NO_EXPORT PyArrayObjectPyArray_SubclassWrap (PyArrayObject *arr_of_subclass, PyArrayObject *towrap)

Define Documentation

#define _COPY_N_SIZE (   size)
Value:
for(i=0; i<N; i++) { \
        memcpy(tout, tin, size); \
        tin += instrides; \
        tout += outstrides; \
    } \
    return
#define FROM_BUFFER_SIZE   4096
Create an array by reading from the given stream, using the passed next_element and skip_separator functions.

Referenced by PyArray_Zeros().

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define PyIntOrLong_Check (   obj)    (PyInt_Check(obj) || PyLong_Check(obj))

Typedef Documentation

typedef int(* next_element)(void **, void *, PyArray_Descr *, void *)

<

for array_item_asarray

<

for npy_mul_with_overflow_intp
Reading from a file or a string.
As much as possible, we try to use the same code for both files and strings, so the semantics for fromstring and fromfile are the same, especially with regards to the handling of text representations.
typedef int(* skip_separator)(void **, const char *, void *)

Function Documentation

NPY_NO_EXPORT void _array_fill_strides ( npy_intp strides,
npy_intp dims,
int  nd,
size_t  itemsize,
int  inflag,
int *  objflags 
)
This is the main array creation routine.
Flags argument has multiple related meanings depending on data and strides:
If data is given, then flags is flags associated with data. If strides is not given, then a contiguous strides array will be created and the NPY_ARRAY_C_CONTIGUOUS bit will be set. If the flags argument has the NPY_ARRAY_F_CONTIGUOUS bit set, then a FORTRAN-style strides array will be created (and of course the NPY_ARRAY_F_CONTIGUOUS flag bit will be set).
If data is not given but created here, then flags will be NPY_ARRAY_DEFAULT and a non-zero flags argument can be used to indicate a FORTRAN style array is desired.
Dimensions and itemsize must have been checked for validity.

<

NPY_RELAXED_STRIDES_CHECKING
Only make Fortran strides if not contiguous as well

<

not NPY_RELAXED_STRIDES_CHECKING

<

not NPY_RELAXED_STRIDES_CHECKING

Referenced by array_setstate().

NPY_NO_EXPORT int _array_from_buffer_3118 ( PyObject *  obj,
PyObject **  out 
)
PEP 3118
Sanity check

References PyArray_FailUnlessWriteable().

static npy_intp _calc_length ( PyObject *  start,
PyObject *  stop,
PyObject *  step,
PyObject **  next,
int  cmplx 
) [static]
the formula is len = (intp) ceil((start - stop) / step);
NPY_NO_EXPORT int _is_default_descr ( PyObject *  descr,
PyObject *  typestr 
)
Checks if the object in descr is the default 'descr' member for the __array_interface__ dictionary with 'typestr' member typestr.

References NPY_DEFAULT_TYPE, PyArray_DescrFromType(), and PyArray_DTypeFromObject().

static int _safe_ceil_to_intp ( double  value,
npy_intp ret 
) [static]
Like ceil(value), but check for overflow.
Return 0 on success, -1 on failure. In case of failure, set a PyExc_Overflow exception
NPY_NO_EXPORT void _strided_byte_swap ( void *  p,
npy_intp  stride,
npy_intp  n,
int  size 
)

<

no byteswap necessary

Referenced by to().

NPY_NO_EXPORT void _unaligned_strided_byte_copy ( char *  dst,
npy_intp  outstrides,
char *  src,
npy_intp  instrides,
npy_intp  N,
int  elsize 
)

References npy_bswap4().

Referenced by _scan(), LONGDOUBLE_scan(), and to().

static int _update_descr_and_dimensions ( PyArray_Descr **  des,
npy_intp newdims,
npy_intp newstrides,
int  oldnd 
) [static]
Change a sub-array field to the base descriptor and update the dimensions and strides appropriately. Dimensions and strides are added to the end.
Strides are only added if given (because data is given).
Make new strides -- alwasy C-contiguous
static PyArrayObject* array_from_text ( PyArray_Descr dtype,
npy_intp  num,
char *  sep,
size_t *  nread,
void *  stream,
next_element  next,
skip_separator  skip_sep,
void *  stream_data 
) [static]
When dtype->subarray is true, PyArray_NewFromDescr will decref dtype even on success, so make sure it stays around until exit.
EOF

Referenced by PyArray_Arange().

static PyArrayObject* array_fromfile_binary ( FILE *  fp,
PyArray_Descr dtype,
npy_intp  num,
size_t *  nread 
) [static]
When dtype->subarray is true, PyArray_NewFromDescr will decref dtype even on success, so make sure it stays around until exit.

Referenced by PyArray_Arange().

NPY_NO_EXPORT void byte_swap_vector ( void *  p,
npy_intp  n,
int  size 
)
NPY_NO_EXPORT void copy_and_swap ( void *  dst,
void *  src,
int  itemsize,
npy_intp  numitems,
npy_intp  srcstrides,
int  swap 
)
If numitems > 1, then dst must be contiguous
static int discover_dimensions ( PyObject *  obj,
int *  maxndim,
npy_intp d,
int  check_it,
int  stop_at_string,
int  stop_at_tuple,
int *  out_is_object 
) [static]
Take an arbitrary object and discover how many dimensions it has, filling in the dimensions as we go.
obj is an Array
obj is a Scalar
obj is not a Sequence
obj is a String
obj is a Tuple, but tuples aren't expanded
obj is a PEP 3118 buffer
PEP 3118 buffer interface
obj has the __array_struct__ interface
obj has the __array_interface__ interface
PySequence_Check detects whether an old type object is a sequence by the presence of the __getitem__ attribute, and for new type objects that aren't dictionaries by the presence of the __len__ attribute as well. In either case it is possible to have an object that tests as a sequence but doesn't behave as a sequence and consequently, the PySequence_GetItem call can fail. When that happens and the object looks like a dictionary, we truncate the dimensions and set the object creation flag, otherwise we pass the error back up the call chain.
1-dimensional sequence
For the dimension truncation check below
Get the dimensions of the first item
Reduce max_ndim_m1 to just items which match
If the dimensions are truncated, need to produce an object array.
static int discover_itemsize ( PyObject *  s,
int  nd,
int *  itemsize,
int  string_type 
) [static]
The rest of this code is to build the right kind of array from a python object.
If an object has no length, leave it be

References PyArray_Check, PyArray_DIM, PyArray_IsScalar, and PyArray_NDIM.

static int fromfile_next_element ( FILE **  fp,
void *  dptr,
PyArray_Descr dtype,
void *  NPY_UNUSEDstream_data 
) [static]
the NULL argument is for backwards-compatibility
r can be EOF or the number of items read (0 or 1)

Referenced by PyArray_Arange().

static int fromfile_skip_separator ( FILE **  fp,
const char *  sep,
void *  NPY_UNUSEDstream_data 
) [static]
matched separator
separator was whitespace wildcard that didn't match
whitespace wildcard

Referenced by PyArray_Arange().

static int fromstr_next_element ( char **  s,
void *  dptr,
PyArray_Descr dtype,
const char *  end 
) [static]
fromstr always returns 0 for basic dtypes s points to the end of the parsed string if an error occurs s is not changed
Nothing read
static int fromstr_skip_separator ( char **  s,
const char *  sep,
const char *  end 
) [static]
Assuming that the separator is the next bit in the string (file), skip it.
Single spaces in the separator are matched to arbitrary-long sequences of whitespace in the input. If the separator consists only of spaces, it matches one or more whitespace characters.
If we can't match the separator, return -2. If we hit the end of the string (file), return -1. Otherwise, return 0.
matched separator
separator was whitespace wildcard that didn't match
whitespace wildcard
NPY_NO_EXPORT PyObject* PyArray_Arange ( double  start,
double  stop,
double  step,
int  type_num 
)
Arange,
place start in the buffer and the next value in the second position if length > 2, then call the inner loop, otherwise stop

References array_from_text(), array_fromfile_binary(), _PyArray_Descr::elsize, _PyArray_Descr::f, fromfile_next_element(), fromfile_skip_separator(), PyArray_DATA, PyArray_DESCR, PyArray_DIMS, PyArray_MAX, PyArray_NewFromDescr_int(), PyDataMem_RENEW(), PyDataType_REFCHK, and PyArray_ArrFuncs::scanfunc.

NPY_NO_EXPORT PyObject* PyArray_ArangeObj ( PyObject *  start,
PyObject *  stop,
PyObject *  step,
PyArray_Descr dtype 
)
ArangeObj, <blockquote> this doesn't change the references</blockquote>
Datetime arange is handled specially
intentionally made to be at least NPY_LONG
calculate the length and next = start + step
If dtype is not in native byte-order then get native-byte order version. And then swap on the way out.
place start in the buffer and the next value in the second position if length > 2, then call the inner loop, otherwise stop
TODO: This swapping could be handled on the fly by the nditer
steals the reference
NPY_NO_EXPORT int PyArray_AssignFromSequence ( PyArrayObject self,
PyObject *  v 
)
NPY_NO_EXPORT PyObject* PyArray_CheckAxis ( PyArrayObject arr,
int *  axis,
int  flags 
)
PyArray_CheckAxis <blockquote> check that axis is valid convert 0-d arrays to 1-d arrays</blockquote>

Referenced by PyArray_ArgMax(), PyArray_Max(), PyArray_Mean(), PyArray_Min(), PyArray_Prod(), PyArray_Sum(), and PyArray_TakeFrom().

NPY_NO_EXPORT PyObject* PyArray_CheckFromAny ( PyObject *  op,
PyArray_Descr descr,
int  min_depth,
int  max_depth,
int  requires,
PyObject *  context 
)
flags is any of NPY_ARRAY_C_CONTIGUOUS (formerly CONTIGUOUS), NPY_ARRAY_F_CONTIGUOUS (formerly FORTRAN), NPY_ARRAY_ALIGNED, NPY_ARRAY_WRITEABLE, NPY_ARRAY_NOTSWAPPED, NPY_ARRAY_ENSURECOPY, NPY_ARRAY_UPDATEIFCOPY, NPY_ARRAY_FORCECAST, NPY_ARRAY_ENSUREARRAY, NPY_ARRAY_ELEMENTSTRIDES
or'd (|) together
Any of these flags present means that the returned array should guarantee that aspect of the array. Otherwise the returned array won't guarantee it -- it will depend on the object as to whether or not it has such features.
Note that NPY_ARRAY_ENSURECOPY is enough to guarantee NPY_ARRAY_C_CONTIGUOUS, NPY_ARRAY_ALIGNED and NPY_ARRAY_WRITEABLE and therefore it is redundant to include those as well.
NPY_ARRAY_BEHAVED == NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE NPY_ARRAY_CARRAY = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_BEHAVED NPY_ARRAY_FARRAY = NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_BEHAVED
NPY_ARRAY_F_CONTIGUOUS can be set in the FLAGS to request a FORTRAN array. Fortran arrays are always behaved (aligned, notswapped, and writeable) and not (C) CONTIGUOUS (if > 1d).
NPY_ARRAY_UPDATEIFCOPY flag sets this flag in the returned array if a copy is made and the base argument points to the (possibly) misbehaved array. When the new array is deallocated, the original array held in base is updated with the contents of the new array.
NPY_ARRAY_FORCECAST will cause a cast to occur regardless of whether or not it is safe.
steals a reference to descr -- accepts NULL
Copy an Array into another array -- memory must not overlap

Does not require src and dest to have "broadcastable" shapes (only the same number of elements).

TODO: For NumPy 2.0, this could accept an order parameter which
only allows NPY_CORDER and NPY_FORDER. Could also rename this to CopyAsFlat to make the name more intuitive.

Returns 0 on success, -1 on error.

TODO: Put the order parameter in PyArray_CopyAnyInto and remove this
If the shapes match and a particular order is forced for both, use the more efficient CopyInto
Zero-sized arrays require nothing be done
This copy is based on matching C-order traversals of src and dst. By using two iterators, we can find maximal sub-chunks that can be processed at once.
Get all the values needed for the inner loop
Since buffering is disabled, we can cache the stride
Since buffering is disabled, we can cache the stride
Because buffering is disabled in the iterator, the inner loop strides will be the same throughout the iteration loop. Thus, we can pass them to this function to take advantage of contiguous strides, etc.
Transfer the biggest amount that fits both
If we exhausted the dst block, refresh it
If we exhausted the src block, refresh it
Copy an Array into another array.

Broadcast to the destination shape if necessary.

Returns 0 on success, -1 on failure.

new reference -- accepts NULL for mintype
NPY_NO_EXPORT PyObject* PyArray_Empty ( int  nd,
npy_intp dims,
PyArray_Descr type,
int  is_f_order 
)
Empty <blockquote> accepts NULL type steals referenct to type</blockquote>
PyArray_NewFromDescr steals a ref, but we need to look at type later.

References PyArray_DATA, and PyDataMem_RENEW().

NPY_NO_EXPORT PyObject* PyArray_EnsureAnyArray ( PyObject *  op)
NPY_NO_EXPORT PyObject* PyArray_EnsureArray ( PyObject *  op)
end old calls
This is a quick wrapper around PyArray_FromAny(op, NULL, 0, 0, ENSUREARRAY)
that special cases Arrays and PyArray_Scalars up front It steals a reference to the object It also guarantees that the result is PyArray_Type Because it decrefs op if any conversion needs to take place so it can be used like PyArray_EnsureArray(some_function(...))
NPY_NO_EXPORT PyObject* PyArray_FromAny ( PyObject *  op,
PyArray_Descr newtype,
int  min_depth,
int  max_depth,
int  flags,
PyObject *  context 
)
Does not check for NPY_ARRAY_ENSURECOPY and NPY_ARRAY_NOTSWAPPED in flags
Steals a reference to newtype --- which can be NULL
This is the main code to make a NumPy array from a Python Object. It is called from many different places.
Get either the array or its parameters if it isn't an array
If the requested dtype is flexible, adapt it
If we got dimensions and dtype instead of an array
TODO: would be nice to do this too, but it's
a behavior change. It's also a bit tricky for downcasting to small integer and float types, and might be better to modify PyArray_AssignFromSequence and descr->f->setitem to have a 'casting' parameter and to check each value with scalar rules like in PyArray_MinScalarType.
if (!(flags&NPY_ARRAY_FORCECAST) && ndim > 0 &&

<blockquote class="first"> !PyArray_CanCastTo(dtype, newtype)) {</blockquote>

System Message: WARNING/2 (<string>, line 4) Block quote ends without a blank line; unexpected unindent.

Py_DECREF(dtype); Py_XDECREF(newtype); PyErr_SetString(PyExc_TypeError,

System Message: ERROR/3 (<string>, line 7) Unexpected indentation.

<blockquote> "object cannot be safely cast to array " "of required type");</blockquote>

System Message: WARNING/2 (<string>, line 9) Block quote ends without a blank line; unexpected unindent.

return NULL;

System Message: WARNING/2 (<string>, line 10) Definition list ends without a blank line; unexpected unindent.
}
Create an array and copy the data

Referenced by _array_fromobject(), _pyarray_correlate(), array_imag_get(), PyArray_CanCoerceScalar(), PyArray_CompareLists(), and PyUFunc_FromFuncAndDataAndSignature().

NPY_NO_EXPORT PyObject* PyArray_FromArray ( PyArrayObject arr,
PyArray_Descr newtype,
int  flags 
)
steals reference to newtype --- acc. NULL
Check if object is of array with Null newtype. If so return it directly instead of checking for casting.
If the casting if forced, use the 'unsafe' casting rule
Raise an error if the casting rule isn't followed
If a guaranteed copy was requested
If C contiguous was requested, and arr is not
If an aligned array was requested, and arr is not
If a Fortran contiguous array was requested, and arr is not
If a writeable array was requested, and arr is not
Set the order for the copy being made based on the flags
If no copy then take an appropriate view if necessary, or just return a reference to ret itself.

Referenced by PyArray_ArgMax(), and PyArray_TakeFrom().

NPY_NO_EXPORT PyObject* PyArray_FromArrayAttr ( PyObject *  op,
PyArray_Descr typecode,
PyObject *  context 
)
NPY_NO_EXPORT PyObject* PyArray_FromBuffer ( PyObject *  buf,
PyArray_Descr type,
npy_intp  count,
npy_intp  offset 
)
Store a reference for decref on deallocation
NPY_NO_EXPORT PyObject* PyArray_FromDims ( int  nd,
int *  d,
int  type 
)
Construct an empty array from dimensions and typenum
2009-04-30, 1.5
Old FromDims set memory to zero --- some algorithms relied on that. Better keep it the same. If Object type, then it's already been set to zero, though.

References PyArray_FillObjectArray().

NPY_NO_EXPORT PyObject* PyArray_FromDimsAndDataAndDescr ( int  nd,
int *  d,
PyArray_Descr descr,
char *  data 
)
These are also old calls (should use PyArray_NewFromDescr)
They all zero-out the memory as previously done
steals reference to descr -- and enforces native byteorder on it.
Like FromDimsAndData but uses the Descr structure instead of typecode
as input.
2009-04-30, 1.5
NPY_NO_EXPORT PyObject* PyArray_FromFile ( FILE *  fp,
PyArray_Descr dtype,
npy_intp  num,
char *  sep 
)
Given a FILE * pointer fp, and a PyArray_Descr, return an

array corresponding to the data encoded in that file.

If the dtype is NULL, the default array type is used (double). If non-null, the reference is stolen and if dtype->subarray is true dtype will be decrefed even on success.

The number of elements to read is given as num; if it is < 0, then then as many as possible are read.

If sep is NULL or empty, then binary data is assumed, else text data, with sep as the separator between elements. Whitespace in the separator matches any length of whitespace in the text, and a match for whitespace around the separator is added.

For memory-mapped files, use the buffer interface. No more data than necessary is read by this routine.

Nothing to read, just create an empty array of the requested type
Realloc memory for smaller number of elements

References PyArray_Check.

NPY_NO_EXPORT PyObject* PyArray_FromInterface ( PyObject *  origin)
Get type string from interface specification
Get dtype from type string
If the dtype is NPY_VOID, see if there is extra information in the 'descr' attribute.
Get shape tuple from interface specification
Shape must be specified when 'data' is specified
Assume shape as scalar otherwise
NOTE: pointers to data and base should be NULL
Make sure 'shape' is a tuple
Get dimensions from shape tuple
Get data buffer from interface specification
Case for data access through pointer
Case for data access through buffer
Get offset number from interface specification
NPY_NO_EXPORT PyObject* PyArray_FromIter ( PyObject *  obj,
PyArray_Descr dtype,
npy_intp  count 
)
steals a reference to dtype (which cannot be NULL)
We would need to alter the memory RENEW code to decrement any reference counts before throwing away any memory.
Grow PyArray_DATA(ret): this is similar for the strategy for PyListObject, but we use 50% overallocation => 0, 4, 8, 14, 23, 36, 56, 86 ...
Realloc the data so that don't keep extra memory tied up (assuming realloc is reasonably good about reusing space...)
The size cannot be zero for PyDataMem_RENEW.

Referenced by array_scalar().

NPY_NO_EXPORT PyObject* PyArray_FromString ( char *  data,
npy_intp  slen,
PyArray_Descr dtype,
npy_intp  num,
char *  sep 
)
Given a pointer to a string data, a string length slen, and

a PyArray_Descr, return an array corresponding to the data encoded in that string.

If the dtype is NULL, the default array type is used (double). If non-null, the reference is stolen.

If slen is < 0, then the end of string is used for text data. It is an error for slen to be < 0 for binary data (since embedded NULLs would be the norm).

The number of elements to read is given as num; if it is < 0, then then as many as possible are read.

If sep is NULL or empty, then binary data is assumed, else text data, with sep as the separator between elements. Whitespace in the separator matches any length of whitespace in the text, and a match for whitespace around the separator is added.

read from character-based string
NPY_NO_EXPORT PyObject* PyArray_FromStructInterface ( PyObject *  input)
NPY_NO_EXPORT int PyArray_GetArrayParamsFromObject ( PyObject *  op,
PyArray_Descr requested_dtype,
npy_bool  writeable,
PyArray_Descr **  out_dtype,
int *  out_ndim,
npy_intp out_dims,
PyArrayObject **  out_arr,
PyObject *  context 
)
Retrieves the array parameters for viewing/converting an arbitrary

PyObject* to a NumPy array. This allows the "innate type and shape" of Python list-of-lists to be discovered without actually converting to an array.

In some cases, such as structured arrays and the __array__ interface, a data type needs to be used to make sense of the object. When this is needed, provide a Descr for 'requested_dtype', otherwise provide NULL. This reference is not stolen. Also, if the requested dtype doesn't modify the interpretation of the input, out_dtype will still get the "innate" dtype of the object, not the dtype passed in 'requested_dtype'.

If writing to the value in 'op' is desired, set the boolean 'writeable' to 1. This raises an error when 'op' is a scalar, list of lists, or other non-writeable 'op'.

Result: When success (0 return value) is returned, either out_arr
is filled with a non-NULL PyArrayObject and the rest of the parameters are untouched, or out_arr is filled with NULL, and the rest of the parameters are filled.

Typical usage: <blockquote class="last">

PyArrayObject *arr = NULL; PyArray_Descr *dtype = NULL; int ndim = 0; npy_intp dims[NPY_MAXDIMS];

System Message: WARNING/2 (<string>, line 26); backlink Inline emphasis start-string without end-string.
System Message: WARNING/2 (<string>, line 26); backlink Inline emphasis start-string without end-string.
if (PyArray_GetArrayParamsFromObject(op, NULL, 1, &dtype,

<blockquote class="first"> &ndim, dims, &arr, NULL) < 0) {</blockquote>

System Message: WARNING/2 (<string>, line 33) Block quote ends without a blank line; unexpected unindent.

return NULL;

System Message: WARNING/2 (<string>, line 34) Definition list ends without a blank line; unexpected unindent.

} if (arr == NULL) {

System Message: ERROR/3 (<string>, line 36) Unexpected indentation.

<blockquote>

... validate/change dtype, validate flags, ndim, etc ... Could make custom strides here too arr = PyArray_NewFromDescr(&PyArray_Type, dtype, ndim,

System Message: ERROR/3 (<string>, line 39) Unexpected indentation.

<blockquote> dims, NULL, is_f_order ? NPY_ARRAY_F_CONTIGUOUS : 0, NULL);</blockquote>

System Message: WARNING/2 (<string>, line 42) Block quote ends without a blank line; unexpected unindent.
if (arr == NULL) {
return NULL;
System Message: WARNING/2 (<string>, line 44) Definition list ends without a blank line; unexpected unindent.

} if (PyArray_CopyObject(arr, op) < 0) {

System Message: ERROR/3 (<string>, line 46) Unexpected indentation.

<blockquote> Py_DECREF(arr); return NULL;</blockquote>

System Message: WARNING/2 (<string>, line 48) Block quote ends without a blank line; unexpected unindent.

} </blockquote>

System Message: WARNING/2 (<string>, line 49) Block quote ends without a blank line; unexpected unindent.

} else {

System Message: ERROR/3 (<string>, line 51) Unexpected indentation.

<blockquote>

... in this case the other parameters weren't filled, just
validate and possibly copy arr itself ...

</blockquote>

System Message: WARNING/2 (<string>, line 53) Block quote ends without a blank line; unexpected unindent.

} ... use arr ... </blockquote>

If op is an array
If op is a NumPy scalar
If op is a Python scalar
If op supports the PEP 3118 buffer interface
If op supports the __array_struct__ or __array_interface__ interface
If op supplies the __array__ function. The documentation says this should produce a copy, so we skip this method if writeable is true, because the intent of writeable is to modify the operand. XXX: If the implementation is wrong, and/or if actual

System Message: ERROR/3 (<string>, line 6) Unexpected indentation.

<blockquote> usage requires this behave differently, this should be changed!</blockquote>

Try to treat op as a list of lists
Determine the type, using the requested data type if it will affect how the array is retrieved
Return NPY_OBJECT for most exceptions
If object arrays are forced
If the type is flexible, determine its size
Say it's an OBJECT scalar if there's an error
Anything can be viewed as an object, unless it needs to be writeable
Move the memory of one array into another, allowing for overlapping data. <blockquote> Returns 0 on success, negative on failure.</blockquote>

References PyArray_Byteswap(), and PyArray_DESCR.

NPY_NO_EXPORT PyObject* PyArray_New ( PyTypeObject *  subtype,
int  nd,
npy_intp dims,
int  type_num,
npy_intp strides,
void *  data,
int  itemsize,
int  flags,
PyObject *  obj 
)
Generic new array creation routine.

Referenced by PyArray_ArgMax().

NPY_NO_EXPORT PyObject* PyArray_NewFromDescr ( PyTypeObject *  subtype,
PyArray_Descr descr,
int  nd,
npy_intp dims,
npy_intp strides,
void *  data,
int  flags,
PyObject *  obj 
)
Generic new array creation routine. <blockquote> steals a reference to descr. On failure or when dtype->subarray is true, dtype will be decrefed.</blockquote>

Referenced by allocate_reduce_result(), NpyIter_GetShape(), prepare_ufunc_output(), PyArray_CastScalarDirect(), PyArray_TakeFrom(), and PyArray_Zeros().

NPY_NO_EXPORT PyObject* PyArray_NewFromDescr_int ( PyTypeObject *  subtype,
PyArray_Descr descr,
int  nd,
npy_intp dims,
npy_intp strides,
void *  data,
int  flags,
PyObject *  obj,
int  zeroed,
int  allow_emptystring 
)
Generic new array creation routine. Internal variant with calloc argument for PyArray_Zeros.
steals a reference to descr. On failure or descr->subarray, descr will be decrefed.
Check datatype element size
Check dimensions and multiply them to nbytes
Compare to PyArray_OverflowMultiplyList that returns 0 in this case.
Care needs to be taken to avoid integer overflow when multiplying the dimensions together to get the total size of the array.

<

fill it in
we allow strides even when we create the memory, but be careful with this...
Allocate something even for zero-space arrays e.g. shape=(0,) -- otherwise buffer exposure (a.data) doesn't work as it should. Could probably just allocate a few bytes here. -- Chuck
It is bad to have uninitialized OBJECT pointers which could also be sub-fields of a VOID array
If data is passed in, this object won't own it by default. Caller must arrange for this to be reset if truly desired
always update the flags to get the right CONTIGUOUS, ALIGN properties not owned data and input strides may not be aligned and on some platforms (debian sparc) malloc does not provide enough alignment for long double types
call the __array_finalize__ method if a subtype. If obj is NULL, then call method with Py_None
A C-function is stored here

Referenced by PyArray_Arange().

NPY_NO_EXPORT PyObject* PyArray_NewLikeArray ( PyArrayObject prototype,
NPY_ORDER  order,
PyArray_Descr dtype,
int  subok 
)
Creates a new array with the same shape as the provided one,

with possible memory layout order and data type changes.

prototype - The array the new one should be like. order - NPY_CORDER - C-contiguous result.

System Message: ERROR/3 (<string>, line 6) Unexpected indentation.

<blockquote> NPY_FORTRANORDER - Fortran-contiguous result. NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise. NPY_KEEPORDER - Keeps the axis ordering of prototype.</blockquote>

System Message: WARNING/2 (<string>, line 9) Block quote ends without a blank line; unexpected unindent.

dtype - If not NULL, overrides the data type of the result. subok - If 1, use the prototype's array subtype, otherwise

System Message: ERROR/3 (<string>, line 11) Unexpected indentation.

<blockquote> always create a base-class array.</blockquote>

NOTE: If dtype is not NULL, steals the dtype reference. On failure or when dtype->subarray is true, dtype will be decrefed.

If no override data type, use the one from the prototype
Handle ANYORDER and simple KEEPORDER cases
If it's not KEEPORDER, this is simple
KEEPORDER needs some analysis of the strides
Build the new strides
Finally, allocate the array

References d().

Calls arr_of_subclass.__array_wrap__(towrap), in order to make 'towrap' have the same ndarray subclass as 'arr_of_subclass'.
NPY_NO_EXPORT PyObject* PyArray_Zeros ( int  nd,
npy_intp dims,
PyArray_Descr type,
int  is_f_order 
)
Zeros <blockquote> steals a reference to type. On failure or when dtype->subarray is true, dtype will be decrefed. accepts NULL type</blockquote>
handle objects

References _PyArray_Descr::elsize, FROM_BUFFER_SIZE, NPY_BEGIN_ALLOW_THREADS, PyArray_DATA, PyArray_NewFromDescr(), and swab_separator().

static int setArrayFromSequence ( PyArrayObject a,
PyObject *  s,
int  dim,
PyArrayObject dst 
) [static]
adapted from Numarray, a: destination array s: source object, array or sequence dim: current recursion dimension, must be 0 on first call dst: must be NULL on first call it is a view on the destination array viewing the place where to put the data of the current recursion
first recursion, view equal destination
This code is to ensure that the sequence access below will return a lower-dimensional sequence.
INCREF on entry DECREF on exit
make sure a base-class array is used so that the dimensionality reduction assumption is correct.
This will DECREF(s) if replaced
dst points to correct array subsection
Either the dimensions match, or the sequence has length 1 and can be broadcast to the destination.
Broadcast the one element from the sequence to all the outputs
Copy element by element

References PyArray_DIMS.

static char* swab_separator ( const char *  sep) [static]
Remove multiple whitespace from the separator, and add a space to the beginning and end. This simplifies the separator-skipping code below.
add space to front if there isn't one
add space to end if there isn't one

Referenced by PyArray_Zeros().