Skip to content

Commit

Permalink
API: expose the dtype types in the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Feb 6, 2024
1 parent 431116d commit dfb8332
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 346 deletions.
8 changes: 3 additions & 5 deletions numpy/_core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5685,11 +5685,9 @@
} ufunc_call_info;
Note that the first call only fills in the ``context``. The call to
``_get_strided_loop`` fills in all other data.
Please see the ``numpy/experimental_dtype_api.h`` header for exact
call information; the main thing to note is that the new-style loops
return 0 on success, -1 on failure. They are passed context as new
first input and ``auxdata`` as (replaced) last.
``_get_strided_loop`` fills in all other data. The main thing to note is
that the new-style loops return 0 on success, -1 on failure. They are
passed context as new first input and ``auxdata`` as (replaced) last.
Only the ``strided_loop``signature is considered guaranteed stable
for NumPy bug-fix releases. All other API is tied to the experimental
Expand Down
95 changes: 93 additions & 2 deletions numpy/_core/code_generators/generate_numpy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,90 @@
_NPY_VERSION_CONCAT_HELPER2(arg, PyArray_RUNTIME_VERSION)
#define PyArray_RUNTIME_VERSION \
_NPY_VERSION_CONCAT_HELPER(PY_ARRAY_UNIQUE_SYMBOL)
#define __dtype_api_table \
_NPY_VERSION_CONCAT_HELPER2(PY_ARRAY_UNIQUE_SYMBOL, \
__dtype_api_table)
#else
#define __dtype_api_table __dtype_api_table
#endif
#if defined(NO_IMPORT) || defined(NO_IMPORT_ARRAY)
extern void **PyArray_API;
extern int PyArray_RUNTIME_VERSION;
extern void **__dtype_api_table;
#else
static void *__uninitialized_table[] = {NULL};
#if defined(PY_ARRAY_UNIQUE_SYMBOL)
void **PyArray_API;
void **__dtype_api_table = __uninitialized_table;
int PyArray_RUNTIME_VERSION;
#else
static void **PyArray_API = NULL;
static int PyArray_RUNTIME_VERSION = 0;
static void **__dtype_api_table = __uninitialized_table;
#endif
#endif
%s
#ifndef NPY_INTERNAL_BUILD
/*
* The type of the DType metaclass
*/
#define PyArrayDTypeMeta_Type (*(PyTypeObject *)__dtype_api_table[0])
/*
* NumPy's builtin DTypes:
*/
#define PyArray_BoolDType (*(PyArray_DTypeMeta *)__dtype_api_table[1])
/* Integers */
#define PyArray_ByteDType (*(PyArray_DTypeMeta *)__dtype_api_table[2])
#define PyArray_UByteDType (*(PyArray_DTypeMeta *)__dtype_api_table[3])
#define PyArray_ShortDType (*(PyArray_DTypeMeta *)__dtype_api_table[4])
#define PyArray_UShortDType (*(PyArray_DTypeMeta *)__dtype_api_table[5])
#define PyArray_IntDType (*(PyArray_DTypeMeta *)__dtype_api_table[6])
#define PyArray_UIntDType (*(PyArray_DTypeMeta *)__dtype_api_table[7])
#define PyArray_LongDType (*(PyArray_DTypeMeta *)__dtype_api_table[8])
#define PyArray_ULongDType (*(PyArray_DTypeMeta *)__dtype_api_table[9])
#define PyArray_LongLongDType (*(PyArray_DTypeMeta *)__dtype_api_table[10])
#define PyArray_ULongLongDType (*(PyArray_DTypeMeta *)__dtype_api_table[11])
/* Integer aliases */
#define PyArray_Int8DType (*(PyArray_DTypeMeta *)__dtype_api_table[12])
#define PyArray_UInt8DType (*(PyArray_DTypeMeta *)__dtype_api_table[13])
#define PyArray_Int16DType (*(PyArray_DTypeMeta *)__dtype_api_table[14])
#define PyArray_UInt16DType (*(PyArray_DTypeMeta *)__dtype_api_table[15])
#define PyArray_Int32DType (*(PyArray_DTypeMeta *)__dtype_api_table[16])
#define PyArray_UInt32DType (*(PyArray_DTypeMeta *)__dtype_api_table[17])
#define PyArray_Int64DType (*(PyArray_DTypeMeta *)__dtype_api_table[18])
#define PyArray_UInt64DType (*(PyArray_DTypeMeta *)__dtype_api_table[19])
#define PyArray_IntpDType (*(PyArray_DTypeMeta *)__dtype_api_table[20])
#define PyArray_UIntpDType (*(PyArray_DTypeMeta *)__dtype_api_table[21])
/* Floats */
#define PyArray_HalfDType (*(PyArray_DTypeMeta *)__dtype_api_table[22])
#define PyArray_FloatDType (*(PyArray_DTypeMeta *)__dtype_api_table[23])
#define PyArray_DoubleDType (*(PyArray_DTypeMeta *)__dtype_api_table[24])
#define PyArray_LongDoubleDType (*(PyArray_DTypeMeta *)__dtype_api_table[25])
/* Complex */
#define PyArray_CFloatDType (*(PyArray_DTypeMeta *)__dtype_api_table[26])
#define PyArray_CDoubleDType (*(PyArray_DTypeMeta *)__dtype_api_table[27])
#define PyArray_CLongDoubleDType (*(PyArray_DTypeMeta *)__dtype_api_table[28])
/* String/Bytes */
#define PyArray_StringDType (*(PyArray_DTypeMeta *)__dtype_api_table[29])
#define PyArray_UnicodeDType (*(PyArray_DTypeMeta *)__dtype_api_table[30])
/* Datetime/Timedelta */
#define PyArray_DatetimeDType (*(PyArray_DTypeMeta *)__dtype_api_table[31])
#define PyArray_TimedeltaDType (*(PyArray_DTypeMeta *)__dtype_api_table[32])
/* Object/Void */
#define PyArray_ObjectDType (*(PyArray_DTypeMeta *)__dtype_api_table[33])
#define PyArray_VoidDType (*(PyArray_DTypeMeta *)__dtype_api_table[34])
/* Abstract */
#define PyArray_PyIntAbstractDType (*(PyArray_DTypeMeta *)__dtype_api_table[35])
#define PyArray_PyFloatAbstractDType (*(PyArray_DTypeMeta *)__dtype_api_table[36])
#define PyArray_PyComplexAbstractDType (*(PyArray_DTypeMeta *)__dtype_api_table[37])
#define PyArray_DefaultIntDType (*(PyArray_DTypeMeta *)__dtype_api_table[38])
#endif /* NPY_INTERNAL_BUILD */
#if !defined(NO_IMPORT_ARRAY) && !defined(NO_IMPORT)
static int
_import_array(void)
Expand All @@ -64,11 +131,27 @@
}
PyObject *c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
Py_DECREF(numpy);
if (c_api == NULL) {
Py_DECREF(numpy);
return -1;
}
PyObject *dtype_api = PyObject_CallMethod(numpy, "_get_dtype_api", NULL);
if (dtype_api == NULL) {
Py_DECREF(numpy);
return -1;
}
if (!PyCapsule_CheckExact(dtype_api)) {
PyErr_SetString(PyExc_RuntimeError, "dtype API is not PyCapsule "
"object");
Py_DECREF(c_api);
return -1;
}
Py_DECREF(numpy);
if (!PyCapsule_CheckExact(c_api)) {
PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is not PyCapsule object");
Py_DECREF(c_api);
Expand All @@ -81,12 +164,20 @@
return -1;
}
__dtype_api_table = (void **)PyCapsule_GetPointer(
dtype_api, "dtype_api_table");
Py_DECREF(dtype_api);
if (__dtype_api_table == NULL) {
__dtype_api_table = __uninitialized_table;
return -1;
}
/*
* On exceedingly few platforms these sizes may not match, in which case
* We do not support older NumPy versions at all.
*/
if (sizeof(Py_ssize_t) != sizeof(Py_intptr_t) &&
PyArray_GetNDArrayCFeatureVersion() < NPY_2_0_API_VERSION) {
PyArray_RUNTIME_VERSION < NPY_2_0_API_VERSION) {
PyErr_Format(PyExc_RuntimeError,
"module compiled against NumPy 2.0 but running on NumPy 1.x. "
"Unfortunately, this is not supported on niche platforms where "
Expand Down
1 change: 0 additions & 1 deletion numpy/_core/include/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ installed_headers = [
'numpy/arrayobject.h',
'numpy/arrayscalars.h',
'numpy/dtype_api.h',
'numpy/experimental_dtype_api.h',
'numpy/halffloat.h',
'numpy/ndarrayobject.h',
'numpy/ndarraytypes.h',
Expand Down
229 changes: 0 additions & 229 deletions numpy/_core/include/numpy/experimental_dtype_api.h

This file was deleted.

0 comments on commit dfb8332

Please sign in to comment.