numpy  2.0.0
src/multiarray/common.c File Reference
#include <Python.h>
#include "numpy/arrayobject.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "common.h"
#include "usertypes.h"
#include "buffer.h"

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE
#define RETRY_WITH_STRING   1
#define RETRY_WITH_UNICODE   2

Functions

PyObject * PyArray_GetAttrString_SuppressException (PyObject *obj, char *name)
NPY_NO_EXPORT PyArray_Descr_array_find_python_scalar_type (PyObject *op)
NPY_NO_EXPORT int PyArray_DTypeFromObject (PyObject *obj, int maxdims, PyArray_Descr **out_dtype)
NPY_NO_EXPORT int PyArray_DTypeFromObjectHelper (PyObject *obj, int maxdims, PyArray_Descr **out_dtype, int string_type)
NPY_NO_EXPORT PyArray_Descr_array_typedescr_fromstr (char *c_str)
NPY_NO_EXPORT char * index2ptr (PyArrayObject *mp, npy_intp i)
NPY_NO_EXPORT int _zerofill (PyArrayObject *ret)
NPY_NO_EXPORT int _IsAligned (PyArrayObject *ap)
NPY_NO_EXPORT npy_bool _IsWriteable (PyArrayObject *ap)
NPY_NO_EXPORT PyObject * convert_shape_to_string (npy_intp n, npy_intp *vals, char *ending)
NPY_NO_EXPORT void dot_alignment_error (PyArrayObject *a, int i, PyArrayObject *b, int j)
NPY_NO_EXPORT int _unpack_field (PyObject *value, PyArray_Descr **descr, npy_intp *offset)
NPY_NO_EXPORT int _may_have_objects (PyArray_Descr *dtype)

Variables

NPY_NO_EXPORT NPY_CASTING NPY_DEFAULT_ASSIGN_CASTING = NPY_SAME_KIND_CASTING

Define Documentation

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define RETRY_WITH_STRING   1
These constants are used to signal that the recursive dtype determination in PyArray_DTypeFromObject encountered a string type, and that the recursive search must be restarted so that string representation lengths can be computed for all scalar types.
#define RETRY_WITH_UNICODE   2

Function Documentation

Returns NULL without setting an exception if no scalar is matched, a new dtype reference otherwise.
bools are a subclass of int
check to see if integer can fit into a longlong or ulonglong
and return that --- otherwise return object

References NPY_LONG, and PyArray_DescrFromType().

new reference

References PyArray_STRIDES.

alignment 1 types should have a efficient alignment for copy loops
power of two sizes may be loaded in larger moves
if not power of two it will be accessed bytewise

<

not NPY_RELAXED_STRIDES_CHECKING

<

not NPY_RELAXED_STRIDES_CHECKING

Referenced by PyArray_UpdateFlags().

If we own our own data, then no-problem
Get to the final base object If it is a writeable array, then return TRUE If we can find an array object or a writeable buffer object as the final base object or a string object (for pickling support memory savings).

  • this last could be removed if a proper pickleable buffer was added to Python.
    MW: I think it would better to disallow switching from READONLY
    to WRITEABLE like this...
here so pickle support works seamlessly and unpickled array can be set and reset writeable -- could be abused --

References convert_shape_to_string(), PyArray_DIM, PyArray_DIMS, PyArray_NDIM, PyUString_Format, and PyUString_FromString.

Referenced by PyArray_UpdateFlags().

check whether arrays with datatype dtype might have object fields. This will only happen for structured dtypes (which may have hidden objects even if the HASOBJECT flag is false), object dtypes, or subarray dtypes whose base type is either of these.

Referenced by array_nbytes_get().

NPY_NO_EXPORT int _unpack_field ( PyObject *  value,
PyArray_Descr **  descr,
npy_intp offset 
)
unpack tuple of dtype->fields (descr, offset, title[not-needed])
&#64;param "value" should be the tuple.
&#64;return "descr" will be set to the field's dtype &#64;return "offset" will be set to the field's offset
returns -1 on failure, 0 on success.

Referenced by STRING_copyswapn().

NPY_NO_EXPORT PyObject* convert_shape_to_string ( npy_intp  n,
npy_intp vals,
char *  ending 
)
Convert an array shape to a string such as "(1, 2)".
&#64;param Dimensionality of the shape &#64;param npy_intp pointer to shape array &#64;param String to append after the shape <cite>(1, 2)s</cite>.
&#64;return Python unicode string
Negative dimension indicates "newaxis", which can be discarded for printing if it's a leading dimension. Find the first non-"newaxis" dimension.

Referenced by _IsWriteable().

NPY_NO_EXPORT void dot_alignment_error ( PyArrayObject a,
int  i,
PyArrayObject b,
int  j 
)
Sets ValueError with "matrices not aligned" message for np.dot and friends when a.shape[i] should match b.shape[j], but doesn't.

Referenced by array_vdot().

NPY_NO_EXPORT char* index2ptr ( PyArrayObject mp,
npy_intp  i 
)
NPY_NO_EXPORT int PyArray_DTypeFromObject ( PyObject *  obj,
int  maxdims,
PyArray_Descr **  out_dtype 
)
Recursively examines the object to determine an appropriate dtype to use for converting to an ndarray.
'obj' is the object to be converted to an ndarray.
'maxdims' is the maximum recursion depth.
'out_dtype' should be either NULL or a minimal starting dtype when the function is called. It is updated with the results of type promotion. This dtype does not get updated when processing NA objects. This is reset to NULL on failure.
Returns 0 on success, -1 on failure.

References promote_types(), PyArray_Check, and PyArray_DESCR.

Referenced by _is_default_descr().

NPY_NO_EXPORT int PyArray_DTypeFromObjectHelper ( PyObject *  obj,
int  maxdims,
PyArray_Descr **  out_dtype,
int  string_type 
)
types for sequence handling
Check if it's an ndarray
See if it's a python None
Check if it's a NumPy scalar
Check if it's a Python scalar
Check if it's an ASCII string
If it's already a big enough string, don't bother type promoting
Check if it's a Unicode string
If it's already a big enough unicode object, don't bother type promoting
PEP 3118 buffer interface
The array interface
The array struct interface
The old buffer interface
The __array__ attribute
If we reached the maximum recursion depth without hitting one of the above cases, and obj isn't a sequence-like object, the output dtype should be either OBJECT or a user-defined type.
Note that some libraries define sequence-like classes but want them to be treated as objects, and they expect numpy to treat it as an object if __len__ is not defined.
Recursive case, first check the sequence contains only one type
all types are the same and scalar, one recursive call is enough
TODO: we could add longs if we add a range check
Recursive call for each sequence item
Set 'out_dtype' if it's NULL
Do type promotion with 'out_dtype'

References NPY_OBJECT, promote_types(), and PyArray_DescrFromType().

PyObject* PyArray_GetAttrString_SuppressException ( PyObject *  obj,
char *  name 
)
The casting to use for implicit assignment operations resulting from in-place operations (like +=) and out= arguments. (Notice that this variable is misnamed, but it's part of the public API so I'm not sure we can just change it. Maybe someone should try and see if anyone notices.
In numpy 1.6 and earlier, this was NPY_UNSAFE_CASTING. In a future release, it will become NPY_SAME_KIND_CASTING. Right now, during the transitional period, we continue to follow the NPY_UNSAFE_CASTING rules (to avoid breaking people's code), but we also check for whether the cast would be allowed under the NPY_SAME_KIND_CASTING rules, and if not we issue a warning (that people's code will be broken in a future release.)
PyArray_GetAttrString_SuppressException:
Stripped down version of PyObject_GetAttrString, avoids lookups for None, tuple, and List objects, and doesn't create a PyErr since this code ignores it.
This can be much faster then PyObject_GetAttrString where exceptions are not used by caller.
'obj' is the object to search for attribute.
'name' is the attribute to search for.
Returns attribute value on success, 0 on failure.
We do not need to check for special attributes on trivial types
Attribute referenced by (char *)name

System Message: WARNING/2 (<string>, line 1); backlink Inline emphasis start-string without end-string.
Attribute referenced by (PyObject *)name

System Message: WARNING/2 (<string>, line 1); backlink Inline emphasis start-string without end-string.

References _is_basic_python_type().


Variable Documentation