numpy  2.0.0
src/multiarray/scalarapi.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 "ctors.h"
#include "descriptor.h"
#include "scalartypes.h"
#include "common.h"

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE
#define CASE(ut, lt)   case NPY_##ut: return &(((Py##lt##ScalarObject *)scalar)->obval)
#define _CHK(cls)
#define _OBJ(lt)   &(((Py##lt##ScalarObject *)scalar)->obval)
#define _IFCASE(cls)   if _CHK(cls) return _OBJ(cls)

Functions

static PyArray_Descr_descr_from_subtype (PyObject *type)
NPY_NO_EXPORT void * scalar_value (PyObject *scalar, PyArray_Descr *descr)
NPY_NO_EXPORT int PyArray_CheckAnyScalarExact (PyObject *obj)
NPY_NO_EXPORT void PyArray_ScalarAsCtype (PyObject *scalar, void *ctypeptr)
NPY_NO_EXPORT int PyArray_CastScalarToCtype (PyObject *scalar, void *ctypeptr, PyArray_Descr *outcode)
NPY_NO_EXPORT int PyArray_CastScalarDirect (PyObject *scalar, PyArray_Descr *indescr, void *ctypeptr, int outtype)
NPY_NO_EXPORT PyObject * PyArray_FromScalar (PyObject *scalar, PyArray_Descr *outcode)
NPY_NO_EXPORT PyObject * PyArray_ScalarFromObject (PyObject *object)
NPY_NO_EXPORT PyArray_DescrPyArray_DescrFromTypeObject (PyObject *type)
NPY_NO_EXPORT PyObject * PyArray_FieldNames (PyObject *fields)
NPY_NO_EXPORT PyArray_DescrPyArray_DescrFromScalar (PyObject *sc)
NPY_NO_EXPORT PyObject * PyArray_TypeObjectFromType (int type)
NPY_NO_EXPORT PyObject * PyArray_Scalar (void *data, PyArray_Descr *descr, PyObject *base)
NPY_NO_EXPORT PyObject * PyArray_Return (PyArrayObject *mp)

Define Documentation

#define _CHK (   cls)
Value:
(PyObject_IsInstance(scalar, \
            (PyObject *)&Py##cls##ArrType_Type))

Referenced by scalar_value().

#define _IFCASE (   cls)    if _CHK(cls) return _OBJ(cls)

Referenced by scalar_value().

#define _OBJ (   lt)    &(((Py##lt##ScalarObject *)scalar)->obval)

Referenced by scalar_value().

#define CASE (   ut,
  lt 
)    case NPY_##ut: return &(((Py##lt##ScalarObject *)scalar)->obval)

Referenced by scalar_value().

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION

Function Documentation

NPY_NO_EXPORT int PyArray_CastScalarDirect ( PyObject *  scalar,
PyArray_Descr indescr,
void *  ctypeptr,
int  outtype 
)
NPY_NO_EXPORT int PyArray_CastScalarToCtype ( PyObject *  scalar,
void *  ctypeptr,
PyArray_Descr outcode 
)
Cast Scalar to c-type <blockquote>

The output buffer must be large-enough to receive the value
Even for flexible types which is different from ScalarAsCtype where only a reference for flexible types is returned
This may not work right on narrow builds for NumPy unicode scalars. </blockquote>
NPY_NO_EXPORT int PyArray_CheckAnyScalarExact ( PyObject *  obj)
return true an object is exactly a numpy scalar
Return descr object from array scalar. <blockquote> New reference</blockquote>
Timedelta

Referenced by PyArray_CastScalarDirect(), and scalar_value().

New reference
if it's a builtin type, then use the typenumber
Check the generic types
Otherwise --- type is a sub-type of an array scalar not corresponding to a registered data-type object.
Do special thing for VOID sub-types

References _arraydescr_fromobj(), _PyArray_Descr::elsize, _PyArray_Descr::fields, _PyArray_Descr::names, NPY_VOID, PyArray_DescrNewFromType(), and _PyArray_Descr::subarray.

Referenced by _ctype_negative(), and _descr_from_subtype().

NPY_NO_EXPORT PyObject* PyArray_FieldNames ( PyObject *  fields)
Return the tuple of ordered field names from a dictionary.
NPY_NO_EXPORT PyObject* PyArray_FromScalar ( PyObject *  scalar,
PyArray_Descr outcode 
)
Get 0-dim array from scalar <blockquote>
0-dim array from array-scalar object always contains a copy of the data unless outcode is NULL, it is of void type and the referrer does not own it either.
steals reference to outcode </blockquote>
convert to 0-dim array of scalar typecode
Need to INCREF typecode because PyArray_NewFromDescr steals a reference below and we still need to access typecode afterwards.
Need to INCREF just the PyObject portion
cast if necessary to desired output typecode

Referenced by timedeltatype_str().

Return Array Scalar if 0-d array object is encountered
Return either an array or the appropriate Python object if the array
is 0d and matches a Python type. steals reference to mp

Referenced by array_cumprod().

NPY_NO_EXPORT PyObject* PyArray_Scalar ( void *  data,
PyArray_Descr descr,
PyObject *  base 
)
Does nothing with descr (cannot be NULL)
Get scalar-equivalent to a region of memory described by a descriptor.
Eliminate NULL bytes
make sure itemsize is a multiple of 4 so round up to nearest multiple
String type
We need to copy the resolution information over to the scalar Get the void * from the metadata dictionary
tp_alloc inherited from Python PyBaseObject_Type
Set uni->str so that object can be deallocated on failure
need aligned data buffer
Allocated enough for 2-characters per itemsize. Now convert from the data-buffer
Resize the unicode result
No base available for copyswp and no swap required. Copy data directly into dest.
copyswap for OBJECT increments the reference count
NPY_NO_EXPORT void PyArray_ScalarAsCtype ( PyObject *  scalar,
void *  ctypeptr 
)
Convert to c-type <blockquote> no error checking is performed -- ctypeptr must be same type as scalar in case of flexible type, the data is not copied into ctypeptr which is expected to be a pointer to pointer</blockquote>
NPY_NO_EXPORT PyObject* PyArray_ScalarFromObject ( PyObject *  object)
Get an Array Scalar From a Python Object <blockquote> Returns NULL if unsuccessful but error is only set if another error occurred. Currently only Numeric-like object supported.</blockquote>
Booleans in Python are implemented as a subclass of integers, so PyBool_Check must be called before PyInt_Check.

Referenced by _ctype_negative().

NPY_NO_EXPORT PyObject* PyArray_TypeObjectFromType ( int  type)
Get a typeobject from a type-number -- can return NULL. <blockquote> New reference</blockquote>
NPY_NO_EXPORT void* scalar_value ( PyObject *  scalar,
PyArray_Descr descr 
)
Must be a user-defined type --- check to see which scalar it inherits from.
Unsigned Integer
Inexact
ComplexFloating
Use the alignment flag to figure out where the data begins after a PyObject_HEAD
now round-up to the nearest alignment value

References _CHK, _IFCASE, _OBJ, _PyArray_Descr::alignment, Bool, CASE, NPY_STRING, NPY_UNICODE, NPY_VOID, PyArray_DescrFromScalar(), and _PyArray_Descr::type_num.