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 "numpy/npy_3kcompat.h"
#include "ctors.h"
#include "descriptor.h"
#include "scalartypes.h"
#include "common.h"

Defines

#define PY_SSIZE_T_CLEAN
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#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 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 _MULTIARRAYMODULE
#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_PREFIX
#define PY_SSIZE_T_CLEAN

Function Documentation

static PyArray_Descr* _descr_from_subtype ( PyObject *  type) [static]
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 PyArray_Descr* PyArray_DescrFromScalar ( PyObject *  sc)
Return descr object from array scalar. <blockquote> New reference</blockquote>

Timedelta
Add correct meta-data to the data-type
Assume this sets a new reference to cobj

Referenced by gentype_interface_get(), gentype_size_get(), PyArray_CastScalarDirect(), and scalar_value().

NPY_NO_EXPORT PyArray_Descr* PyArray_DescrFromTypeObject ( PyObject *  type)
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 PyArray_DescrFromType().

Referenced by _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 just the PyObject portion
cast if necessary to desired output typecode

Referenced by form(), gentype_dealloc(), gentype_richcompare(), PyArray_FromInterface(), and PyArray_GetArrayParamsFromObject().

NPY_NO_EXPORT PyObject* PyArray_Return ( PyArrayObject mp)
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.
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
FIXME There is no error handling here.
Need an extra slot and need to use Python memory manager
need aligned data buffer
Allocated enough for 2-characters per itemsize. Now convert from the data-buffer
Resize the unicode result
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.

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.

Referenced by gentype_interface_get().