diff options
| -rw-r--r-- | numpy/core/include/numpy/experimental_dtype_api.h | 20 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/experimental_public_dtype_api.c | 16 |
2 files changed, 33 insertions, 3 deletions
diff --git a/numpy/core/include/numpy/experimental_dtype_api.h b/numpy/core/include/numpy/experimental_dtype_api.h index 69f799b92..7d289d730 100644 --- a/numpy/core/include/numpy/experimental_dtype_api.h +++ b/numpy/core/include/numpy/experimental_dtype_api.h @@ -355,6 +355,22 @@ typedef PyArray_DTypeMeta *__promote_dtype_sequence( ((__promote_dtype_sequence *)(__experimental_dtype_api_table[5])) +typedef PyArray_Descr *__get_default_descr( + PyArray_DTypeMeta *DType); +#define _PyArray_GetDefaultDescr \ + ((__get_default_descr *)(__experimental_dtype_api_table[6])) + +static NPY_INLINE PyArray_Descr * +PyArray_GetDefaultDescr(PyArray_DTypeMeta *DType) +{ + if (DType->singleton != NULL) { + Py_INCREF(DType->singleton); + return DType->singleton; + } + return _PyArray_GetDefaultDescr(DType); +} + + /* * ******************************** * Initialization @@ -367,7 +383,7 @@ typedef PyArray_DTypeMeta *__promote_dtype_sequence( */ #if !defined(NO_IMPORT) && !defined(NO_IMPORT_ARRAY) -#define __EXPERIMENTAL_DTYPE_VERSION 3 +#define __EXPERIMENTAL_DTYPE_VERSION 4 static int import_experimental_dtype_api(int version) @@ -395,7 +411,7 @@ import_experimental_dtype_api(int version) if (api == NULL) { return -1; } - __experimental_dtype_api_table = PyCapsule_GetPointer(api, + __experimental_dtype_api_table = (void **)PyCapsule_GetPointer(api, "experimental_dtype_api_table"); Py_DECREF(api); diff --git a/numpy/core/src/multiarray/experimental_public_dtype_api.c b/numpy/core/src/multiarray/experimental_public_dtype_api.c index e9d191002..a603ea529 100644 --- a/numpy/core/src/multiarray/experimental_public_dtype_api.c +++ b/numpy/core/src/multiarray/experimental_public_dtype_api.c @@ -16,7 +16,7 @@ #include "common_dtype.h" -#define EXPERIMENTAL_DTYPE_API_VERSION 3 +#define EXPERIMENTAL_DTYPE_API_VERSION 4 typedef struct{ @@ -358,6 +358,19 @@ PyUFunc_AddPromoter( } +/* + * Lightweight function fetch a default instance of a DType class. + * Note that this version is named `_PyArray_GetDefaultDescr` with an + * underscore. The `singleton` slot is public, so an inline version is + * provided that checks `singleton != NULL` first. + */ +static PyArray_Descr * +_PyArray_GetDefaultDescr(PyArray_DTypeMeta *DType) +{ + return NPY_DT_CALL_default_descr(DType); +} + + NPY_NO_EXPORT PyObject * _get_experimental_dtype_api(PyObject *NPY_UNUSED(mod), PyObject *arg) { @@ -368,6 +381,7 @@ _get_experimental_dtype_api(PyObject *NPY_UNUSED(mod), PyObject *arg) &PyArrayInitDTypeMeta_FromSpec, &PyArray_CommonDType, &PyArray_PromoteDTypeSequence, + &_PyArray_GetDefaultDescr, NULL, }; |
