numpy 2.0.0
src/multiarray/arrayobject.c File Reference
#include <Python.h>
#include "structmember.h"
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "npy_config.h"
#include "numpy/npy_3kcompat.h"
#include "common.h"
#include "number.h"
#include "usertypes.h"
#include "arraytypes.h"
#include "scalartypes.h"
#include "arrayobject.h"
#include "ctors.h"
#include "methods.h"
#include "descriptor.h"
#include "iterators.h"
#include "mapping.h"
#include "getset.h"
#include "sequence.h"
#include "buffer.h"

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#define CHECK_MEMORY
#define SMALL_STRING   2048
#define _rstrip_loop(CMP)
#define _reg_loop(CMP)
#define _loop(CMP)

Functions

NPY_NO_EXPORT npy_intp PyArray_Size (PyObject *op)
NPY_NO_EXPORT int PyArray_CopyObject (PyArrayObject *dest, PyObject *src_object)
NPY_NO_EXPORT int PyArray_TypeNumFromName (char *str)
static void array_dealloc (PyArrayObject *self)
static int dump_data (char **string, int *n, int *max_n, char *data, int nd, npy_intp *dimensions, npy_intp *strides, PyArrayObject *self)
static PyObject * array_repr_builtin (PyArrayObject *self, int repr)
NPY_NO_EXPORT void PyArray_SetStringFunction (PyObject *op, int repr)
NPY_NO_EXPORT void PyArray_SetDatetimeParseFunction (PyObject *op)
static PyObject * array_repr (PyArrayObject *self)
static PyObject * array_str (PyArrayObject *self)
NPY_NO_EXPORT int PyArray_CompareUCS4 (npy_ucs4 *s1, npy_ucs4 *s2, size_t len)
NPY_NO_EXPORT int PyArray_CompareString (char *s1, char *s2, size_t len)
static int _myunincmp (PyArray_UCS4 *s1, PyArray_UCS4 *s2, int len1, int len2)
static int _mystrncmp (char *s1, char *s2, int len1, int len2)
static void _rstripw (char *s, int n)
static void _unistripw (PyArray_UCS4 *s, int n)
static char * _char_copy_n_strip (char *original, char *temp, int nc)
static void _char_release (char *ptr, int nc)
static char * _uni_copy_n_strip (char *original, char *temp, int nc)
static void _uni_release (char *ptr, int nc)
static int _compare_strings (PyObject *result, PyArrayMultiIterObject *multi, int cmp_op, void *func, int rstrip)
NPY_NO_EXPORT PyObject * _strings_richcompare (PyArrayObject *self, PyArrayObject *other, int cmp_op, int rstrip)
static PyObject * _void_compare (PyArrayObject *self, PyArrayObject *other, int cmp_op)
NPY_NO_EXPORT PyObject * array_richcompare (PyArrayObject *self, PyObject *other, int cmp_op)
NPY_NO_EXPORT int PyArray_ElementStrides (PyObject *arr)
NPY_NO_EXPORT Bool PyArray_CheckStrides (int elsize, int nd, npy_intp numbytes, npy_intp offset, npy_intp *dims, npy_intp *newstrides)
static PyObject * array_new (PyTypeObject *subtype, PyObject *args, PyObject *kwds)
static PyObject * array_iter (PyArrayObject *arr)
static PyObject * array_alloc (PyTypeObject *type, Py_ssize_t NPY_UNUSED(nitems))

Variables

static PyObject * PyArray_StrFunction = NULL
static PyObject * PyArray_ReprFunction = NULL
NPY_NO_EXPORT PyTypeObject PyArray_Type

Define Documentation

#define _loop (   CMP)
Value:
if (rstrip) _rstrip_loop(CMP)        \
        else _reg_loop(CMP)

Referenced by _char_release().

#define _MULTIARRAYMODULE
#define _reg_loop (   CMP)
Value:
{                                \
        while(size--) {                                 \
            val = compfunc((void *)iself->dataptr,       \
                          (void *)iother->dataptr,      \
                          N1, N2);                      \
            *dptr = (val CMP 0);                        \
            PyArray_ITER_NEXT(iself);                   \
            PyArray_ITER_NEXT(iother);                  \
            dptr += 1;                                  \
        }                                               \
    }
#define _rstrip_loop (   CMP)
Value:
{                                     \
        void *aptr, *bptr;                                      \
        char atemp[SMALL_STRING], btemp[SMALL_STRING];          \
        while(size--) {                                         \
            aptr = stripfunc(iself->dataptr, atemp, N1);        \
            if (!aptr) return -1;                               \
            bptr = stripfunc(iother->dataptr, btemp, N2);       \
            if (!bptr) {                                        \
                relfunc(aptr, N1);                              \
                return -1;                                      \
            }                                                   \
            val = compfunc(aptr, bptr, N1, N2);                  \
            *dptr = (val CMP 0);                                \
            PyArray_ITER_NEXT(iself);                           \
            PyArray_ITER_NEXT(iother);                          \
            dptr += 1;                                          \
            relfunc(aptr, N1);                                  \
            relfunc(bptr, N2);                                  \
        }                                                       \
    }
End borrowed from numarray
#define CHECK_MEMORY
Value:
do { if (*n >= *max_n-16) {         \
        *max_n *= 2;                                     \
        *string = (char *)_pya_realloc(*string, *max_n); \
    }} while (0)
#define NPY_NO_DEPRECATED_API
#include <stdio.h>
#define NPY_NO_PREFIX
#define PY_SSIZE_T_CLEAN
#define SMALL_STRING   2048
Borrowed from Numarray

Function Documentation

static char* _char_copy_n_strip ( char *  original,
char *  temp,
int  nc 
) [static]
static void _char_release ( char *  ptr,
int  nc 
) [static]

References _loop.

static int _compare_strings ( PyObject *  result,
PyArrayMultiIterObject multi,
int  cmp_op,
void *  func,
int  rstrip 
) [static]
static int _mystrncmp ( char *  s1,
char *  s2,
int  len1,
int  len2 
) [static]
Compare s1 and s2 which are not necessarily NULL-terminated. s1 is of length len1 s2 is of length len2 If they are NULL terminated, then stop comparison.

<

Only happens if NULLs are everywhere

static int _myunincmp ( PyArray_UCS4 *  s1,
PyArray_UCS4 *  s2,
int  len1,
int  len2 
) [static]
This also handles possibly mis-aligned data
Compare s1 and s2 which are not necessarily NULL-terminated.
s1 is of length len1 s2 is of length len2 If they are NULL terminated, then stop comparison.
static void _rstripw ( char *  s,
int  n 
) [static]

<

Never strip to length 0.

static char* _uni_copy_n_strip ( char *  original,
char *  temp,
int  nc 
) [static]

Referenced by _char_copy_n_strip().

static void _uni_release ( char *  ptr,
int  nc 
) [static]

Referenced by _char_copy_n_strip().

static void _unistripw ( PyArray_UCS4 *  s,
int  n 
) [static]

<

Never strip to length 0.

References Bool, and PyArray_DATA.

static PyObject* _void_compare ( PyArrayObject self,
PyArrayObject other,
int  cmp_op 
) [static]
VOID-type arrays can only be compared equal and not-equal in which case the fields are all compared by extracting the fields and testing one at a time... equality testing is performed using logical_ands on all the fields. in-equality testing is performed using logical_ors on all the fields.
VOID-type arrays without fields are compared for equality by comparing their memory at each location directly (using string-code).

If the field type has a non-trivial shape, additional dimensions will have been appended to <cite>a</cite> and <cite>b</cite>. In that case, reduce them using <cite>op</cite>.
If the type was multidimensional, collapse that part to 1-D
Reduce the extra dimension of <cite>temp</cite> using <cite>op</cite>
compare as a string. Assumes self and other have same descr->type

References NumericOps::equal, NumericOps::less, NumericOps::less_equal, n_ops, NumericOps::not_equal, PyArray_Check, PyArray_DESCR, PyArray_FromObject, PyArray_GenericBinaryFunction(), PyArray_NOTYPE, PyArray_OBJECT, and PyArray_VOID.

static PyObject* array_alloc ( PyTypeObject *  type,
Py_ssize_t   NPY_UNUSEDnitems 
) [static]

nitems will always be 0

static void array_dealloc ( PyArrayObject self) [static]
***************** end C-API functions *****************
array object functions

UPDATEIFCOPY means that base points to an array that should be updated with the contents of this array upon destruction. self->base->flags must have been WRITEABLE (checked previously) and it was locked here thus, unlock it.

<

hold on to self in next call
<blockquote> Don't need to DECREF -- because we are deleting</blockquote>

System Message: WARNING/2 (<string>, line 2) Block quote ends without a blank line; unexpected unindent.
self already...
In any case base is pointing to something that we need to DECREF -- either a view or a buffer object
Free internal references if an Object array

<

hold on to self
Don't need to DECREF -- because we are deleting self already...

static PyObject* array_iter ( PyArrayObject arr) [static]
static PyObject* array_new ( PyTypeObject *  subtype,
PyObject *  args,
PyObject *  kwds 
) [static]

Usually called with shape and type but can also be called with buffer, strides, and swapped info For now, let's just use this to create an empty, contiguous array of a specific type and shape.
place Py_None in object positions
buffer given -- use it
get writeable and aligned

static PyObject* array_repr ( PyArrayObject self) [static]

References PyArray_UCS4.

static PyObject* array_repr_builtin ( PyArrayObject self,
int  repr 
) [static]
NPY_NO_EXPORT PyObject* array_richcompare ( PyArrayObject self,
PyObject *  other,
int  cmp_op 
)

Try to convert other to an array
If not successful, indicate that the items cannot be compared this way.
If the comparison results in NULL, then the two array objects can not be compared together; indicate that
Try to convert other to an array
If not successful, then objects cannot be compared this way
Try to handle string comparisons

Referenced by _strings_richcompare().

static PyObject* array_str ( PyArrayObject self) [static]
static int dump_data ( char **  string,
int *  n,
int *  max_n,
char *  data,
int  nd,
npy_intp dimensions,
npy_intp strides,
PyArrayObject self 
) [static]
NPY_NO_EXPORT Bool PyArray_CheckStrides ( int  elsize,
int  nd,
npy_intp  numbytes,
npy_intp  offset,
npy_intp dims,
npy_intp newstrides 
)
This routine checks to see if newstrides (of length nd) will not ever be able to walk outside of the memory implied numbytes and offset.
The available memory is assumed to start at -offset and proceed to numbytes-offset. The strides are checked to ensure that accessing memory using striding will not try to reach beyond this memory for any of the axes.
If numbytes is 0 it will be calculated using the dimensions and element-size.
This function checks for walking beyond the beginning and right-end of the buffer and therefore works for any integer stride (positive or negative).
NPY_NO_EXPORT int PyArray_CompareString ( char *  s1,
char *  s2,
size_t  len 
)

References MIN.

NPY_NO_EXPORT int PyArray_CompareUCS4 ( npy_ucs4 *  s1,
npy_ucs4 *  s2,
size_t  len 
)
NPY_NO_EXPORT int PyArray_CopyObject ( PyArrayObject dest,
PyObject *  src_object 
)

Special code to mimic Numeric behavior for character arrays.
Get either an array object we can copy from, or its parameters if there isn't a convenient array available.
If it's not an array, either assign from a sequence or as a scalar
If the input is scalar
If there's one dest element and src is a Python scalar
If there are more than enough dims, use AssignFromSequence because it can handle this style of broadcasting.
Otherwise convert to an array and do an array-based copy
If it's an array, do a move (handling possible overlapping data)

NPY_NO_EXPORT int PyArray_ElementStrides ( PyObject *  arr)
NPY_NO_EXPORT void PyArray_SetDatetimeParseFunction ( PyObject *  op)
This function is scheduled to be removed <blockquote> TO BE REMOVED - NOT USED INTERNALLY.</blockquote>

References PyArray_UCS4.

NPY_NO_EXPORT void PyArray_SetStringFunction ( PyObject *  op,
int  repr 
)
Set the array print function to be a Python function.

Dispose of previous callback
Add a reference to new callback
Remember new callback
Dispose of previous callback
Add a reference to new callback
Remember new callback

NPY_NO_EXPORT npy_intp PyArray_Size ( PyObject *  op)
Compute the size of an array (in number of items)
NPY_NO_EXPORT int PyArray_TypeNumFromName ( char *  str)
returns an Array-Scalar Object of the type of arr
from the given pointer to memory -- main Scalar creation function default new method calls this.
Ideally, here the descriptor would contain all the information needed.
So, that we simply need the data and the descriptor, and perhaps a flag
Given a string return the type-number for the data-type with that string as the type-object name. Returns PyArray_NOTYPE without setting an error if no type can be found. Only works for user-defined data-types.

References NPY_ARRAY_WRITEABLE, and PyArray_CopyAnyInto().


Variable Documentation

PyObject* PyArray_ReprFunction = NULL [static]
PyObject* PyArray_StrFunction = NULL [static]