diff --git a/asciidtype/asciidtype/src/asciidtype_main.c b/asciidtype/asciidtype/src/asciidtype_main.c index 5e7676cb..fdd3c9bd 100644 --- a/asciidtype/asciidtype/src/asciidtype_main.c +++ b/asciidtype/asciidtype/src/asciidtype_main.c @@ -1,9 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#include "numpy/ndarraytypes.h" #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -18,13 +22,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__asciidtype_main(void) { - if (_import_array() < 0) { - return NULL; - } - - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -51,7 +50,7 @@ PyInit__asciidtype_main(void) goto error; } - if (init_ufuncs() < 0) { + if (init_ufuncs() == -1) { goto error; } diff --git a/asciidtype/asciidtype/src/casts.c b/asciidtype/asciidtype/src/casts.c index 8b4acfab..54c01d25 100644 --- a/asciidtype/asciidtype/src/casts.c +++ b/asciidtype/asciidtype/src/casts.c @@ -1,10 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "casts.h" @@ -46,7 +49,7 @@ ascii_to_ascii(PyArrayMethod_Context *context, char *const data[], npy_intp const dimensions[], npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) { - PyArray_Descr **descrs = context->descriptors; + PyArray_Descr *const *descrs = context->descriptors; long in_size = ((ASCIIDTypeObject *)descrs[0])->size; long out_size = ((ASCIIDTypeObject *)descrs[1])->size; long copy_size; @@ -111,7 +114,7 @@ unicode_to_ascii(PyArrayMethod_Context *context, char *const data[], npy_intp const dimensions[], npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) { - PyArray_Descr **descrs = context->descriptors; + PyArray_Descr *const *descrs = context->descriptors; long in_size = (descrs[0]->elsize) / 4; long out_size = ((ASCIIDTypeObject *)descrs[1])->size; long copy_size; @@ -162,7 +165,7 @@ ascii_to_unicode(PyArrayMethod_Context *context, char *const data[], npy_intp const dimensions[], npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) { - PyArray_Descr **descrs = context->descriptors; + PyArray_Descr *const *descrs = context->descriptors; long in_size = ((ASCIIDTypeObject *)descrs[0])->size; long out_size = (descrs[1]->elsize) / 4; long copy_size; diff --git a/asciidtype/asciidtype/src/casts.h b/asciidtype/asciidtype/src/casts.h index 8dc8605e..8f8539ca 100644 --- a/asciidtype/asciidtype/src/casts.h +++ b/asciidtype/asciidtype/src/casts.h @@ -1,15 +1,6 @@ #ifndef _NPY_CASTS_H #define _NPY_CASTS_H -#include - -#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/ndarraytypes.h" - PyArrayMethod_Spec ** get_casts(void); diff --git a/asciidtype/asciidtype/src/dtype.c b/asciidtype/asciidtype/src/dtype.c index d77bb2bb..8a94516f 100644 --- a/asciidtype/asciidtype/src/dtype.c +++ b/asciidtype/asciidtype/src/dtype.c @@ -1,3 +1,19 @@ +// clang-format off +#include +#include "structmember.h" +// clang-format on + +#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" + #include "dtype.h" #include "casts.h" diff --git a/asciidtype/asciidtype/src/dtype.h b/asciidtype/asciidtype/src/dtype.h index 47fcf8f1..f131de71 100644 --- a/asciidtype/asciidtype/src/dtype.h +++ b/asciidtype/asciidtype/src/dtype.h @@ -1,18 +1,6 @@ #ifndef _NPY_DTYPE_H #define _NPY_DTYPE_H -// clang-format off -#include -#include "structmember.h" -// clang-format on - -#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/ndarraytypes.h" - typedef struct { PyArray_Descr base; long size; diff --git a/asciidtype/asciidtype/src/umath.c b/asciidtype/asciidtype/src/umath.c index e05058aa..bc98a063 100644 --- a/asciidtype/asciidtype/src/umath.c +++ b/asciidtype/asciidtype/src/umath.c @@ -1,12 +1,15 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#define NO_IMPORT_UFUNC #include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" #include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "string.h" @@ -17,7 +20,7 @@ ascii_add_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_intp const dimensions[], npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) { - PyArray_Descr **descrs = context->descriptors; + PyArray_Descr *const *descrs = context->descriptors; long in1_size = ((ASCIIDTypeObject *)descrs[0])->size; long in2_size = ((ASCIIDTypeObject *)descrs[1])->size; long out_size = ((ASCIIDTypeObject *)descrs[2])->size; @@ -112,7 +115,7 @@ ascii_equal_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_intp const dimensions[], npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) { - PyArray_Descr **descrs = context->descriptors; + PyArray_Descr *const *descrs = context->descriptors; long in1_size = ((ASCIIDTypeObject *)descrs[0])->size; long in2_size = ((ASCIIDTypeObject *)descrs[1])->size; @@ -234,7 +237,6 @@ init_ufuncs(void) goto error; } - Py_DECREF(numpy); return 0; error: diff --git a/asciidtype/reinstall.sh b/asciidtype/reinstall.sh new file mode 100755 index 00000000..d91c389c --- /dev/null +++ b/asciidtype/reinstall.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -xeuo pipefail +IFS=$'\n\t' + +if [ -d "build/" ] +then + rm -r build +fi + +#meson setup build -Db_sanitize=address,undefined +meson setup build +python -m pip uninstall -y asciidtype +python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' -Csetup-args="-Dbuildtype=debug" +#python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' diff --git a/metadatadtype/metadatadtype/src/casts.c b/metadatadtype/metadatadtype/src/casts.c index 191785a1..58b119f0 100644 --- a/metadatadtype/metadatadtype/src/casts.c +++ b/metadatadtype/metadatadtype/src/casts.c @@ -1,10 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "casts.h" @@ -186,7 +189,7 @@ static PyArray_DTypeMeta *m2m_dtypes[2] = {NULL, NULL}; static PyType_Slot m2m_slots[] = { {NPY_METH_resolve_descriptors, &metadata_to_metadata_resolve_descriptors}, - {_NPY_METH_get_loop, &metadata_to_metadata_get_loop}, + {NPY_METH_get_loop, &metadata_to_metadata_get_loop}, {0, NULL}}; PyArrayMethod_Spec MetadataToMetadataCastSpec = { @@ -200,7 +203,7 @@ PyArrayMethod_Spec MetadataToMetadataCastSpec = { }; static PyType_Slot m2f_slots[] = { - {_NPY_METH_get_loop, &metadata_to_float64_get_loop}, {0, NULL}}; + {NPY_METH_get_loop, &metadata_to_float64_get_loop}, {0, NULL}}; static char *m2f_name = "cast_MetadataDType_to_Float64"; diff --git a/metadatadtype/metadatadtype/src/casts.h b/metadatadtype/metadatadtype/src/casts.h index 14aa716d..8f8539ca 100644 --- a/metadatadtype/metadatadtype/src/casts.h +++ b/metadatadtype/metadatadtype/src/casts.h @@ -1,15 +1,6 @@ #ifndef _NPY_CASTS_H #define _NPY_CASTS_H -#include - -#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/ndarraytypes.h" - PyArrayMethod_Spec ** get_casts(void); diff --git a/metadatadtype/metadatadtype/src/dtype.c b/metadatadtype/metadatadtype/src/dtype.c index 9d4c6399..c7b2d617 100644 --- a/metadatadtype/metadatadtype/src/dtype.c +++ b/metadatadtype/metadatadtype/src/dtype.c @@ -1,3 +1,18 @@ +// clang-format off +#include +#include "structmember.h" +// clang-format on + +#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/arrayobject.h" +#include "numpy/dtype_api.h" +#include "numpy/ndarraytypes.h" + #include "dtype.h" #include "casts.h" @@ -74,8 +89,9 @@ new_metadatadtype_instance(PyObject *metadata) } Py_INCREF(metadata); new->metadata = metadata; - new->base.elsize = sizeof(double); - new->base.alignment = _Alignof(double); /* is there a better spelling? */ + PyArray_Descr *base = (PyArray_Descr *)new; + base->elsize = sizeof(double); + base->alignment = _Alignof(double); /* is there a better spelling? */ /* do not support byte-order for now */ return new; diff --git a/metadatadtype/metadatadtype/src/dtype.h b/metadatadtype/metadatadtype/src/dtype.h index b3011a34..c37765f4 100644 --- a/metadatadtype/metadatadtype/src/dtype.h +++ b/metadatadtype/metadatadtype/src/dtype.h @@ -1,18 +1,6 @@ #ifndef _NPY_DTYPE_H #define _NPY_DTYPE_H -// clang-format off -#include -#include "structmember.h" -// clang-format on - -#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/ndarraytypes.h" - typedef struct { PyArray_Descr base; PyObject *metadata; diff --git a/metadatadtype/metadatadtype/src/metadatadtype_main.c b/metadatadtype/metadatadtype/src/metadatadtype_main.c index e508d812..8dffe46b 100644 --- a/metadatadtype/metadatadtype/src/metadatadtype_main.c +++ b/metadatadtype/metadatadtype/src/metadatadtype_main.c @@ -1,12 +1,15 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" -#include "dtype.h" #include "umath.h" +#include "dtype.h" static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, @@ -18,12 +21,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__metadatadtype_main(void) { - if (_import_array() < 0) { - return NULL; - } - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -51,7 +50,7 @@ PyInit__metadatadtype_main(void) goto error; } - if (init_ufuncs() < 0) { + if (init_ufuncs() == -1) { goto error; } diff --git a/metadatadtype/metadatadtype/src/umath.c b/metadatadtype/metadatadtype/src/umath.c index 1be362a5..60a1ee7e 100644 --- a/metadatadtype/metadatadtype/src/umath.c +++ b/metadatadtype/metadatadtype/src/umath.c @@ -1,20 +1,23 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#define NO_IMPORT_UFUNC #include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" #include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" static int translate_given_descrs(int nin, int nout, - PyArray_DTypeMeta *NPY_UNUSED(wrapped_dtypes[]), - PyArray_Descr *given_descrs[], + PyArray_DTypeMeta *const NPY_UNUSED(wrapped_dtypes[]), + PyArray_Descr *const given_descrs[], PyArray_Descr *new_descrs[]) { for (int i = 0; i < nin + nout; i++) { @@ -35,8 +38,8 @@ translate_given_descrs(int nin, int nout, static int translate_loop_descrs(int nin, int NPY_UNUSED(nout), - PyArray_DTypeMeta *NPY_UNUSED(new_dtypes[]), - PyArray_Descr *given_descrs[], + PyArray_DTypeMeta *const NPY_UNUSED(new_dtypes[]), + PyArray_Descr *const given_descrs[], PyArray_Descr *original_descrs[], PyArray_Descr *loop_descrs[]) { @@ -125,5 +128,6 @@ init_ufuncs(void) return 0; error: + return -1; } diff --git a/metadatadtype/reinstall.sh b/metadatadtype/reinstall.sh new file mode 100755 index 00000000..f82fa119 --- /dev/null +++ b/metadatadtype/reinstall.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -xeuo pipefail +IFS=$'\n\t' + +if [ -d "build/" ] +then + rm -r build +fi + +#meson setup build -Db_sanitize=address,undefined +meson setup build +python -m pip uninstall -y metadatadtype +python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' -Csetup-args="-Dbuildtype=debug" +#python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' diff --git a/mpfdtype/mpfdtype/src/casts.cpp b/mpfdtype/mpfdtype/src/casts.cpp index 026d9a42..e29c6f9a 100644 --- a/mpfdtype/mpfdtype/src/casts.cpp +++ b/mpfdtype/mpfdtype/src/casts.cpp @@ -1,14 +1,17 @@ #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC extern "C" { #include #include "numpy/arrayobject.h" #include "numpy/ndarraytypes.h" - #include "numpy/experimental_dtype_api.h" + #include "numpy/dtype_api.h" } #include diff --git a/mpfdtype/mpfdtype/src/dtype.c b/mpfdtype/mpfdtype/src/dtype.c index 90d4573a..9877d342 100644 --- a/mpfdtype/mpfdtype/src/dtype.c +++ b/mpfdtype/mpfdtype/src/dtype.c @@ -1,11 +1,14 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC #include "numpy/arrayobject.h" #include "numpy/ndarraytypes.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "mpfr.h" diff --git a/mpfdtype/mpfdtype/src/mpfdtype_main.c b/mpfdtype/mpfdtype/src/mpfdtype_main.c index 0a3ef398..9b921a21 100644 --- a/mpfdtype/mpfdtype/src/mpfdtype_main.c +++ b/mpfdtype/mpfdtype/src/mpfdtype_main.c @@ -1,9 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -19,12 +21,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__mpfdtype_main(void) { - if (_import_array() < 0) { - return NULL; - } - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -47,7 +45,7 @@ PyInit__mpfdtype_main(void) goto error; } - if (init_mpf_umath() < 0) { + if (init_mpf_umath() == -1) { goto error; } diff --git a/mpfdtype/mpfdtype/src/numbers.cpp b/mpfdtype/mpfdtype/src/numbers.cpp index f27144d8..1988759d 100644 --- a/mpfdtype/mpfdtype/src/numbers.cpp +++ b/mpfdtype/mpfdtype/src/numbers.cpp @@ -3,7 +3,8 @@ */ #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY extern "C" { @@ -13,7 +14,7 @@ extern "C" { #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" - #include "numpy/experimental_dtype_api.h" + #include "numpy/dtype_api.h" } #include diff --git a/mpfdtype/mpfdtype/src/scalar.c b/mpfdtype/mpfdtype/src/scalar.c index b22a5c92..a9b798a6 100644 --- a/mpfdtype/mpfdtype/src/scalar.c +++ b/mpfdtype/mpfdtype/src/scalar.c @@ -1,11 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY #include "numpy/arrayobject.h" #include "numpy/ndarraytypes.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "scalar.h" #include "numbers.h" diff --git a/mpfdtype/mpfdtype/src/terrible_hacks.c b/mpfdtype/mpfdtype/src/terrible_hacks.c index 05503478..48e4ab9d 100644 --- a/mpfdtype/mpfdtype/src/terrible_hacks.c +++ b/mpfdtype/mpfdtype/src/terrible_hacks.c @@ -1,11 +1,12 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY #include "numpy/arrayobject.h" #include "numpy/ndarraytypes.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "mpfr.h" @@ -68,8 +69,8 @@ init_terrible_hacks(void) { return -1; } /* ->f slots are the same for all instances (currently). */ - descr->base.f->copyswap = (PyArray_CopySwapFunc *)©swap_mpf; - descr->base.f->compare = (PyArray_CompareFunc *)&compare_mpf; + PyDataType_GetArrFuncs(&descr->base)->copyswap = (PyArray_CopySwapFunc *)©swap_mpf; + PyDataType_GetArrFuncs(&descr->base)->compare = (PyArray_CompareFunc *)&compare_mpf; Py_DECREF(descr); return 0; diff --git a/mpfdtype/mpfdtype/src/umath.cpp b/mpfdtype/mpfdtype/src/umath.cpp index c44f4389..65ee93c1 100644 --- a/mpfdtype/mpfdtype/src/umath.cpp +++ b/mpfdtype/mpfdtype/src/umath.cpp @@ -1,8 +1,11 @@ #include "scalar.h" #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC extern "C" { #include @@ -11,7 +14,7 @@ extern "C" { #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" - #include "numpy/experimental_dtype_api.h" + #include "numpy/dtype_api.h" } #include @@ -612,6 +615,7 @@ init_mpf_umath(void) goto err; } + Py_DECREF(numpy); return 0; err: diff --git a/mpfdtype/reinstall.sh b/mpfdtype/reinstall.sh new file mode 100755 index 00000000..f327cf91 --- /dev/null +++ b/mpfdtype/reinstall.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -xeuo pipefail +IFS=$'\n\t' + +if [ -d "build/" ] +then + rm -r build +fi + +#meson setup build -Db_sanitize=address,undefined +meson setup build +python -m pip uninstall -y mpfdtype +python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' -Csetup-args="-Dbuildtype=debug" +#python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' diff --git a/quaddtype/quaddtype/src/casts.c b/quaddtype/quaddtype/src/casts.c index d9e6b7c3..999344f5 100644 --- a/quaddtype/quaddtype/src/casts.c +++ b/quaddtype/quaddtype/src/casts.c @@ -1,5 +1,17 @@ -#include "casts.h" +#include + +#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL quaddtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" +#include "numpy/dtype_api.h" + #include "dtype.h" +#include "casts.h" // And now the actual cast code! Starting with the "resolver" which tells // us about cast safety. @@ -131,7 +143,7 @@ static PyArray_DTypeMeta *QuadToQuadDtypes[2] = {NULL, NULL}; static PyType_Slot QuadToQuadSlots[] = { {NPY_METH_resolve_descriptors, &quad_to_quad_resolve_descriptors}, - {_NPY_METH_get_loop, &quad_to_quad_get_loop}, + {NPY_METH_get_loop, &quad_to_quad_get_loop}, {0, NULL}}; PyArrayMethod_Spec QuadToQuadCastSpec = { diff --git a/quaddtype/quaddtype/src/casts.h b/quaddtype/quaddtype/src/casts.h index e01bba5a..de775617 100644 --- a/quaddtype/quaddtype/src/casts.h +++ b/quaddtype/quaddtype/src/casts.h @@ -1,7 +1,6 @@ #ifndef _NPY_CASTS_H #define _NPY_CASTS_H -#include "numpy/experimental_dtype_api.h" extern PyArrayMethod_Spec QuadToQuadCastSpec; extern PyArrayMethod_Spec QuadToFloat128CastSpec; diff --git a/quaddtype/quaddtype/src/dtype.c b/quaddtype/quaddtype/src/dtype.c index 9042364b..63a6840e 100644 --- a/quaddtype/quaddtype/src/dtype.c +++ b/quaddtype/quaddtype/src/dtype.c @@ -1,3 +1,15 @@ +#include + +#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL quaddtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" +#include "numpy/dtype_api.h" + #include "dtype.h" #include "abstract.h" #include "casts.h" diff --git a/quaddtype/quaddtype/src/dtype.h b/quaddtype/quaddtype/src/dtype.h index fdce9398..104ed3e3 100644 --- a/quaddtype/quaddtype/src/dtype.h +++ b/quaddtype/quaddtype/src/dtype.h @@ -1,15 +1,6 @@ #ifndef _NPY_DTYPE_H #define _NPY_DTYPE_H -#include - -#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/ndarraytypes.h" -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" - typedef struct { PyArray_Descr base; } QuadDTypeObject; diff --git a/quaddtype/quaddtype/src/quaddtype_main.c b/quaddtype/quaddtype/src/quaddtype_main.c index d883c1b2..d9adeff5 100644 --- a/quaddtype/quaddtype/src/quaddtype_main.c +++ b/quaddtype/quaddtype/src/quaddtype_main.c @@ -1,9 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL quaddtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -19,15 +21,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__quaddtype_main(void) { - if (_import_array() < 0) - return NULL; - - // Fail to init if the experimental DType API version 5 isn't supported - if (import_experimental_dtype_api(15) < 0) { - PyErr_SetString(PyExc_ImportError, - "Error encountered importing the experimental dtype API."); - return NULL; - } + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -57,7 +52,7 @@ PyInit__quaddtype_main(void) goto error; } - if (init_multiply_ufunc() < 0) { + if (init_multiply_ufunc() == -1) { PyErr_SetString(PyExc_TypeError, "Failed to initialize the quadscalar multiply ufunc."); goto error; } diff --git a/quaddtype/quaddtype/src/umath.c b/quaddtype/quaddtype/src/umath.c index ac223be9..6c9fece6 100644 --- a/quaddtype/quaddtype/src/umath.c +++ b/quaddtype/quaddtype/src/umath.c @@ -1,13 +1,15 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL quaddtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" +#define NO_IMPORT_UFUNC #include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" #include "numpy/ufuncobject.h" - -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -81,10 +83,10 @@ init_multiply_ufunc(void) if (numpy == NULL) { return -1; } - PyObject *multiply = PyObject_GetAttrString(numpy, "multiply"); - // Why decref here? + PyObject *multiply = PyObject_GetAttrString(numpy, "multiply"); Py_DECREF(numpy); + if (multiply == NULL) { return -1; } @@ -116,6 +118,7 @@ init_multiply_ufunc(void) Py_DECREF(multiply); return -1; } + Py_DECREF(multiply); return 0; } diff --git a/quaddtype/reinstall.sh b/quaddtype/reinstall.sh new file mode 100755 index 00000000..52125765 --- /dev/null +++ b/quaddtype/reinstall.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -xeuo pipefail +IFS=$'\n\t' + +if [ -d "build/" ] +then + rm -r build +fi + +#meson setup build -Db_sanitize=address,undefined +meson setup build +python -m pip uninstall -y quaddtype +python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' -Csetup-args="-Dbuildtype=debug" +#python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' diff --git a/stringdtype/README.md b/stringdtype/README.md index d2eae298..9ba9beca 100644 --- a/stringdtype/README.md +++ b/stringdtype/README.md @@ -3,8 +3,8 @@ This is the prototype implementation of the variable-width UTF-8 string DType described in [NEP 55](https://numpy.org/neps/nep-0055-string_dtype.html). -See the NEP for implementation details and usage examples. Full -documentation will be written as before this code is merged into NumPy. +See the NEP for implementation details and usage examples. See +`numpy.dtypes.StringDType` for the version that made it into NumPy. ## Building @@ -37,19 +37,3 @@ $ meson build $ python -m build --wheel -Cbuilddir=build $ python -m pip install dist/path-to-wheel-file.whl ``` - -## Usage - -The dtype will not import unless you run python executable with -the `NUMPY_EXPERIMENTAL_DTYPE_API` environment variable set: - -```bash -$ NUMPY_EXPERIMENTAL_DTYPE_API=1 python -Python 3.11.3 (main, May 2 2023, 11:36:22) [GCC 11.3.0] on linux -Type "help", "copyright", "credits" or "license" for more information. ->>> from stringdtype import StringDType ->>> import numpy as np ->>> arr = np.array(["hello", "world"], dtype=StringDType()) ->>> arr -array(['hello', 'world'], dtype=StringDType()) -``` diff --git a/stringdtype/reinstall.sh b/stringdtype/reinstall.sh new file mode 100755 index 00000000..ebffe312 --- /dev/null +++ b/stringdtype/reinstall.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -xeuo pipefail +IFS=$'\n\t' + +if [ -d "build/" ] +then + rm -r build +fi + +#meson setup build -Db_sanitize=address,undefined +meson setup build +python -m pip uninstall -y stringdtype +python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' -Csetup-args="-Dbuildtype=debug" +#python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' diff --git a/stringdtype/stringdtype/src/casts.c b/stringdtype/stringdtype/src/casts.c index 1f0f3506..58db76f3 100644 --- a/stringdtype/stringdtype/src/casts.c +++ b/stringdtype/stringdtype/src/casts.c @@ -1,3 +1,17 @@ +#include +#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL stringdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" +#include "numpy/halffloat.h" +#include "numpy/npy_math.h" + #include "casts.h" #include "dtype.h" @@ -75,7 +89,7 @@ string_to_string(PyArrayMethod_Context *context, char *const data[], StringDTypeObject *odescr = (StringDTypeObject *)context->descriptors[1]; int in_hasnull = idescr->na_object != NULL; int out_hasnull = odescr->na_object != NULL; - const npy_static_string *in_na_name = &idescr->na_name; + const _npy_static_string *in_na_name = &idescr->na_name; npy_intp N = dimensions[0]; char *in = data[0]; char *out = data[1]; @@ -84,15 +98,15 @@ string_to_string(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *iallocator = NULL; npy_string_allocator *oallocator = NULL; - NpyString_acquire_allocator2(idescr, odescr, &iallocator, &oallocator); + _NpyString_acquire_allocator2(idescr, odescr, &iallocator, &oallocator); while (N--) { const npy_packed_static_string *s = (npy_packed_static_string *)in; npy_packed_static_string *os = (npy_packed_static_string *)out; if (in != out) { - if (in_hasnull && !out_hasnull && NpyString_isnull(s)) { + if (in_hasnull && !out_hasnull && _NpyString_isnull(s)) { // lossy but this is an unsafe cast so this is OK - if (NpyString_pack(odescr->allocator, os, in_na_name->buf, + if (_NpyString_pack(odescr->allocator, os, in_na_name->buf, in_na_name->size) < 0) { gil_error(PyExc_MemoryError, "Failed to pack string in string to string " @@ -110,13 +124,13 @@ string_to_string(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator2(odescr, idescr); + _NpyString_release_allocator2(odescr, idescr); return 0; fail: - NpyString_release_allocator2(odescr, idescr); + _NpyString_release_allocator2(odescr, idescr); return -1; } @@ -219,10 +233,10 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[], npy_intp const dimensions[], npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) { - PyArray_Descr **descrs = context->descriptors; + PyArray_Descr *const *descrs = context->descriptors; StringDTypeObject *sdescr = (StringDTypeObject *)descrs[1]; - npy_string_allocator *allocator = NpyString_acquire_allocator(sdescr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(sdescr); long max_in_size = (descrs[0]->elsize) / 4; @@ -243,18 +257,18 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[], goto fail; } npy_packed_static_string *out_pss = (npy_packed_static_string *)out; - if (NpyString_free(out_pss, allocator) < 0) { + if (_NpyString_free(out_pss, allocator) < 0) { gil_error(PyExc_MemoryError, "Failed to deallocate string in unicode to string cast"); goto fail; } - if (NpyString_newemptysize(out_num_bytes, out_pss, allocator) < 0) { + if (_NpyString_newemptysize(out_num_bytes, out_pss, allocator) < 0) { gil_error(PyExc_MemoryError, "Failed to allocate string in unicode to string cast"); goto fail; } - npy_static_string out_ss = {0, NULL}; - int is_null = NpyString_load(allocator, out_pss, &out_ss); + _npy_static_string out_ss = {0, NULL}; + int is_null = _NpyString_load(allocator, out_pss, &out_ss); if (is_null == -1) { gil_error(PyExc_MemoryError, "Failed to load string in unicode to string cast"); @@ -287,13 +301,13 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(sdescr); + _NpyString_release_allocator(sdescr); return 0; fail: - NpyString_release_allocator(sdescr); + _NpyString_release_allocator(sdescr); return -1; } @@ -376,11 +390,11 @@ string_to_unicode(PyArrayMethod_Context *context, char *const data[], NpyAuxData *NPY_UNUSED(auxdata)) { StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[0]; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); int has_null = descr->na_object != NULL; int has_string_na = descr->has_string_na; - const npy_static_string *default_string = &descr->default_string; - const npy_static_string *na_name = &descr->na_name; + const _npy_static_string *default_string = &descr->default_string; + const _npy_static_string *na_name = &descr->na_name; npy_intp N = dimensions[0]; char *in = data[0]; Py_UCS4 *out = (Py_UCS4 *)data[1]; @@ -392,11 +406,11 @@ string_to_unicode(PyArrayMethod_Context *context, char *const data[], while (N--) { const npy_packed_static_string *ps = (npy_packed_static_string *)in; - npy_static_string s = {0, NULL}; - npy_static_string name = {0, NULL}; + _npy_static_string s = {0, NULL}; + _npy_static_string name = {0, NULL}; unsigned char *this_string = NULL; size_t n_bytes; - int is_null = NpyString_load(allocator, ps, &s); + int is_null = _NpyString_load(allocator, ps, &s); if (is_null == -1) { gil_error(PyExc_MemoryError, "Failed to load string in unicode to string cast"); @@ -443,12 +457,12 @@ string_to_unicode(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return 0; fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return -1; } @@ -489,10 +503,10 @@ string_to_bool(PyArrayMethod_Context *context, char *const data[], NpyAuxData *NPY_UNUSED(auxdata)) { StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[0]; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); int has_null = descr->na_object != NULL; int has_string_na = descr->has_string_na; - const npy_static_string *default_string = &descr->default_string; + const _npy_static_string *default_string = &descr->default_string; npy_intp N = dimensions[0]; char *in = data[0]; @@ -503,8 +517,8 @@ string_to_bool(PyArrayMethod_Context *context, char *const data[], while (N--) { const npy_packed_static_string *ps = (npy_packed_static_string *)in; - npy_static_string s = {0, NULL}; - int is_null = NpyString_load(allocator, ps, &s); + _npy_static_string s = {0, NULL}; + int is_null = _NpyString_load(allocator, ps, &s); if (is_null == -1) { gil_error(PyExc_MemoryError, "Failed to load string in unicode to string cast"); @@ -530,13 +544,13 @@ string_to_bool(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return 0; fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return -1; } @@ -563,7 +577,7 @@ bool_to_string(PyArrayMethod_Context *context, char *const data[], npy_intp out_stride = strides[1]; StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[1]; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); while (N--) { npy_packed_static_string *out_pss = (npy_packed_static_string *)out; @@ -582,7 +596,7 @@ bool_to_string(PyArrayMethod_Context *context, char *const data[], "invalid value encountered in bool to string cast"); goto fail; } - if (NpyString_pack(allocator, out_pss, ret_val, size) < 0) { + if (_NpyString_pack(allocator, out_pss, ret_val, size) < 0) { gil_error(PyExc_MemoryError, "Failed to pack string in bool to string cast"); goto fail; @@ -591,13 +605,13 @@ bool_to_string(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return 0; fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return -1; } @@ -613,12 +627,12 @@ static char *b2s_name = "cast_Bool_to_StringDType"; static PyObject * string_to_pylong(char *in, int hasnull, - const npy_static_string *default_string, + const _npy_static_string *default_string, npy_string_allocator *allocator) { const npy_packed_static_string *ps = (npy_packed_static_string *)in; - npy_static_string s = {0, NULL}; - int isnull = NpyString_load(allocator, ps, &s); + _npy_static_string s = {0, NULL}; + int isnull = _NpyString_load(allocator, ps, &s); if (isnull == -1) { PyErr_SetString(PyExc_MemoryError, "Failed to load string converting string to int"); @@ -645,7 +659,7 @@ string_to_pylong(char *in, int hasnull, static npy_longlong string_to_uint(char *in, npy_ulonglong *value, int hasnull, - const npy_static_string *default_string, + const _npy_static_string *default_string, npy_string_allocator *allocator) { PyObject *pylong_value = @@ -664,7 +678,7 @@ string_to_uint(char *in, npy_ulonglong *value, int hasnull, static npy_longlong string_to_int(char *in, npy_longlong *value, int hasnull, - const npy_static_string *default_string, + const _npy_static_string *default_string, npy_string_allocator *allocator) { PyObject *pylong_value = @@ -700,7 +714,7 @@ pyobj_to_string(PyObject *obj, char *out, npy_string_allocator *allocator) return -1; } npy_packed_static_string *out_ss = (npy_packed_static_string *)out; - if (NpyString_pack(allocator, out_ss, cstr_val, length) < 0) { + if (_NpyString_pack(allocator, out_ss, cstr_val, length) < 0) { gil_error(PyExc_MemoryError, "Failed to pack string while converting from python " "string"); @@ -756,9 +770,9 @@ uint_to_string(unsigned long long in, char *out, { \ StringDTypeObject *descr = \ ((StringDTypeObject *)context->descriptors[0]); \ - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); \ + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \ int hasnull = descr->na_object != NULL; \ - const npy_static_string *default_string = &descr->default_string; \ + const _npy_static_string *default_string = &descr->default_string; \ \ npy_intp N = dimensions[0]; \ char *in = data[0]; \ @@ -789,11 +803,11 @@ uint_to_string(unsigned long long in, char *out, out += out_stride; \ } \ \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return 0; \ \ fail: \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return -1; \ } \ \ @@ -819,7 +833,7 @@ uint_to_string(unsigned long long in, char *out, \ StringDTypeObject *descr = \ (StringDTypeObject *)context->descriptors[1]; \ - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); \ + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \ \ while (N--) { \ if (typekind##_to_string((longtype)*in, out, allocator) != 0) { \ @@ -830,11 +844,11 @@ uint_to_string(unsigned long long in, char *out, out += out_stride; \ } \ \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return 0; \ \ fail: \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return -1; \ } \ \ @@ -903,12 +917,12 @@ STRING_INT_CASTS(ulonglong, uint, ulonglong, NPY_ULONGLONG, llu, npy_ulonglong, static PyObject * string_to_pyfloat(char *in, int hasnull, - const npy_static_string *default_string, + const _npy_static_string *default_string, npy_string_allocator *allocator) { const npy_packed_static_string *ps = (npy_packed_static_string *)in; - npy_static_string s = {0, NULL}; - int isnull = NpyString_load(allocator, ps, &s); + _npy_static_string s = {0, NULL}; + int isnull = _NpyString_load(allocator, ps, &s); if (isnull == -1) { PyErr_SetString( PyExc_MemoryError, @@ -942,9 +956,9 @@ string_to_pyfloat(char *in, int hasnull, { \ StringDTypeObject *descr = \ (StringDTypeObject *)context->descriptors[0]; \ - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); \ + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \ int hasnull = (descr->na_object != NULL); \ - const npy_static_string *default_string = &descr->default_string; \ + const _npy_static_string *default_string = &descr->default_string; \ \ npy_intp N = dimensions[0]; \ char *in = data[0]; \ @@ -975,10 +989,10 @@ string_to_pyfloat(char *in, int hasnull, out += out_stride; \ } \ \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return 0; \ fail: \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return -1; \ } \ \ @@ -1027,7 +1041,7 @@ string_to_pyfloat(char *in, int hasnull, \ StringDTypeObject *descr = \ (StringDTypeObject *)context->descriptors[1]; \ - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); \ + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \ \ while (N--) { \ PyObject *scalar_val = PyArray_Scalar(in, float_descr, NULL); \ @@ -1039,10 +1053,10 @@ string_to_pyfloat(char *in, int hasnull, out += out_stride; \ } \ \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return 0; \ fail: \ - NpyString_release_allocator(descr); \ + _NpyString_release_allocator(descr); \ return -1; \ } \ \ @@ -1062,9 +1076,9 @@ string_to_float64(PyArrayMethod_Context *context, char *const data[], NpyAuxData *NPY_UNUSED(auxdata)) { StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[0]; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); int hasnull = descr->na_object != NULL; - const npy_static_string *default_string = &descr->default_string; + const _npy_static_string *default_string = &descr->default_string; npy_intp N = dimensions[0]; char *in = data[0]; npy_float64 *out = (npy_float64 *)data[1]; @@ -1085,11 +1099,11 @@ string_to_float64(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return 0; fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return -1; } @@ -1141,10 +1155,10 @@ string_to_datetime(PyArrayMethod_Context *context, char *const data[], NpyAuxData *NPY_UNUSED(auxdata)) { StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[0]; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); int has_null = descr->na_object != NULL; int has_string_na = descr->has_string_na; - const npy_static_string *default_string = &descr->default_string; + const _npy_static_string *default_string = &descr->default_string; npy_intp N = dimensions[0]; char *in = data[0]; @@ -1159,13 +1173,14 @@ string_to_datetime(PyArrayMethod_Context *context, char *const data[], npy_bool out_special; PyArray_Descr *dt_descr = context->descriptors[1]; + _PyArray_LegacyDescr *legacy_descr = (_PyArray_LegacyDescr *)dt_descr; PyArray_DatetimeMetaData *dt_meta = - &(((PyArray_DatetimeDTypeMetaData *)dt_descr->c_metadata)->meta); + &(((PyArray_DatetimeDTypeMetaData *)legacy_descr->c_metadata)->meta); while (N--) { const npy_packed_static_string *ps = (npy_packed_static_string *)in; - npy_static_string s = {0, NULL}; - int is_null = NpyString_load(allocator, ps, &s); + _npy_static_string s = {0, NULL}; + int is_null = _NpyString_load(allocator, ps, &s); if (is_null == -1) { // do we hold the gil in this cast? error handling below seems to // think we do @@ -1196,11 +1211,11 @@ string_to_datetime(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return 0; fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return -1; } @@ -1228,18 +1243,19 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[], npy_datetimestruct dts; PyArray_Descr *dt_descr = context->descriptors[0]; + _PyArray_LegacyDescr *legacy_descr = (_PyArray_LegacyDescr *)dt_descr; PyArray_DatetimeMetaData *dt_meta = - &(((PyArray_DatetimeDTypeMetaData *)dt_descr->c_metadata)->meta); + &(((PyArray_DatetimeDTypeMetaData *)legacy_descr->c_metadata)->meta); // buffer passed to numpy to build datetime string char datetime_buf[NPY_DATETIME_MAX_ISO8601_STRLEN]; StringDTypeObject *sdescr = (StringDTypeObject *)context->descriptors[1]; - npy_string_allocator *allocator = NpyString_acquire_allocator(sdescr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(sdescr); while (N--) { npy_packed_static_string *out_pss = (npy_packed_static_string *)out; if (*in == NPY_DATETIME_NAT && - NpyString_pack_null(allocator, out_pss) < 0) { + _NpyString_pack_null(allocator, out_pss) < 0) { gil_error( PyExc_MemoryError, "Failed to deallocate string in datetime to string cast"); @@ -1260,7 +1276,7 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[], goto fail; } - if (NpyString_pack(allocator, out_pss, datetime_buf, + if (_NpyString_pack(allocator, out_pss, datetime_buf, strlen(datetime_buf)) < 0) { PyErr_SetString(PyExc_MemoryError, "Failed to allocate string when converting " @@ -1273,11 +1289,11 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator(sdescr); + _NpyString_release_allocator(sdescr); return 0; fail: - NpyString_release_allocator(sdescr); + _NpyString_release_allocator(sdescr); return -1; } diff --git a/stringdtype/stringdtype/src/casts.h b/stringdtype/stringdtype/src/casts.h index 3e4a32e3..07e25d1f 100644 --- a/stringdtype/stringdtype/src/casts.h +++ b/stringdtype/stringdtype/src/casts.h @@ -1,16 +1,6 @@ #ifndef _NPY_CASTS_H #define _NPY_CASTS_H -// needed for Py_UCS4 -#include - -// need these defines and includes for PyArrayMethod_Spec -#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION -#define NPY_TARGET_VERSION NPY_2_0_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/experimental_dtype_api.h" - PyArrayMethod_Spec ** get_casts(); diff --git a/stringdtype/stringdtype/src/dtype.c b/stringdtype/stringdtype/src/dtype.c index 29a963da..23d50961 100644 --- a/stringdtype/stringdtype/src/dtype.c +++ b/stringdtype/stringdtype/src/dtype.c @@ -1,3 +1,17 @@ +#include +#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL stringdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" +#include "numpy/halffloat.h" +#include "numpy/npy_math.h" + #include "dtype.h" #include "casts.h" @@ -24,7 +38,7 @@ new_stringdtype_instance(PyObject *na_object, int coerce) char *default_string_buf = NULL; char *na_name_buf = NULL; - allocator = NpyString_new_allocator(PyMem_RawMalloc, PyMem_RawFree, + allocator = _NpyString_new_allocator(PyMem_RawMalloc, PyMem_RawFree, PyMem_RawRealloc); if (allocator == NULL) { PyErr_SetString(PyExc_MemoryError, @@ -38,8 +52,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce) goto fail; } - npy_static_string default_string = {0, NULL}; - npy_static_string na_name = {0, NULL}; + _npy_static_string default_string = {0, NULL}; + _npy_static_string na_name = {0, NULL}; Py_XINCREF(na_object); ((StringDTypeObject *)new)->na_object = na_object; @@ -127,7 +141,7 @@ new_stringdtype_instance(PyObject *na_object, int coerce) PyMem_RawFree(na_name_buf); } if (allocator != NULL) { - NpyString_free_allocator(allocator); + _NpyString_free_allocator(allocator); } if (allocator_lock != NULL) { PyThread_free_lock(allocator_lock); @@ -262,7 +276,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr) { npy_packed_static_string *sdata = (npy_packed_static_string *)dataptr; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); // borrow reference PyObject *na_object = descr->na_object; @@ -270,7 +284,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr) // setting NA *must* check pointer equality since NA types might not // allow equality if (na_object != NULL && obj == na_object) { - if (NpyString_pack_null(allocator, sdata) < 0) { + if (_NpyString_pack_null(allocator, sdata) < 0) { PyErr_SetString(PyExc_MemoryError, "Failed to pack null string during StringDType " "setitem"); @@ -291,7 +305,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr) goto fail; } - if (NpyString_pack(allocator, sdata, val, length) < 0) { + if (_NpyString_pack(allocator, sdata, val, length) < 0) { PyErr_SetString(PyExc_MemoryError, "Failed to pack string during StringDType " "setitem"); @@ -301,12 +315,12 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr) Py_DECREF(val_obj); } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return 0; fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return -1; } @@ -316,10 +330,10 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr) { PyObject *val_obj = NULL; npy_packed_static_string *psdata = (npy_packed_static_string *)dataptr; - npy_static_string sdata = {0, NULL}; + _npy_static_string sdata = {0, NULL}; int hasnull = descr->na_object != NULL; - npy_string_allocator *allocator = NpyString_acquire_allocator(descr); - int is_null = NpyString_load(allocator, psdata, &sdata); + npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); + int is_null = _NpyString_load(allocator, psdata, &sdata); if (is_null < 0) { PyErr_SetString(PyExc_MemoryError, @@ -344,7 +358,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr) } } - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); /* * In principle we should return a StringScalar instance here, but @@ -361,7 +375,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr) fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return NULL; } @@ -371,7 +385,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr) npy_bool nonzero(void *data, void *NPY_UNUSED(arr)) { - return NpyString_size((npy_packed_static_string *)data) != 0; + return _NpyString_size((npy_packed_static_string *)data) != 0; } // Implementation of PyArray_CompareFunc. @@ -382,9 +396,9 @@ compare(void *a, void *b, void *arr) StringDTypeObject *descr = (StringDTypeObject *)PyArray_DESCR(arr); // ignore the allocator returned by this function // since _compare needs the descr anyway - NpyString_acquire_allocator(descr); + _NpyString_acquire_allocator(descr); int ret = _compare(a, b, descr, descr); - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return ret; } @@ -400,13 +414,13 @@ _compare(void *a, void *b, StringDTypeObject *descr_a, int hasnull = descr_a->na_object != NULL; int has_string_na = descr_a->has_string_na; int has_nan_na = descr_a->has_nan_na; - npy_static_string *default_string = &descr_a->default_string; + _npy_static_string *default_string = &descr_a->default_string; const npy_packed_static_string *ps_a = (npy_packed_static_string *)a; - npy_static_string s_a = {0, NULL}; - int a_is_null = NpyString_load(allocator_a, ps_a, &s_a); + _npy_static_string s_a = {0, NULL}; + int a_is_null = _NpyString_load(allocator_a, ps_a, &s_a); const npy_packed_static_string *ps_b = (npy_packed_static_string *)b; - npy_static_string s_b = {0, NULL}; - int b_is_null = NpyString_load(allocator_b, ps_b, &s_b); + _npy_static_string s_b = {0, NULL}; + int b_is_null = _NpyString_load(allocator_b, ps_b, &s_b); if (NPY_UNLIKELY(a_is_null == -1 || b_is_null == -1)) { char *msg = "Failed to load string in string comparison"; if (hasnull && !(has_string_na && has_nan_na)) { @@ -450,7 +464,7 @@ _compare(void *a, void *b, StringDTypeObject *descr_a, } } } - return NpyString_cmp(&s_a, &s_b); + return _NpyString_cmp(&s_a, &s_b); } // PyArray_ArgFunc @@ -494,25 +508,25 @@ stringdtype_ensure_canonical(StringDTypeObject *self) static int stringdtype_clear_loop(void *NPY_UNUSED(traverse_context), - PyArray_Descr *descr, char *data, npy_intp size, + const PyArray_Descr *descr, char *data, npy_intp size, npy_intp stride, NpyAuxData *NPY_UNUSED(auxdata)) { StringDTypeObject *sdescr = (StringDTypeObject *)descr; - npy_string_allocator *allocator = NpyString_acquire_allocator(sdescr); + npy_string_allocator *allocator = _NpyString_acquire_allocator(sdescr); while (size--) { npy_packed_static_string *sdata = (npy_packed_static_string *)data; - if (data != NULL && NpyString_free(sdata, allocator) < 0) { + if (data != NULL && _NpyString_free(sdata, allocator) < 0) { gil_error(PyExc_MemoryError, "String deallocation failed in clear loop"); goto fail; } data += stride; } - NpyString_release_allocator(sdescr); + _NpyString_release_allocator(sdescr); return 0; fail: - NpyString_release_allocator(sdescr); + _NpyString_release_allocator(sdescr); return -1; } @@ -521,7 +535,7 @@ stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context), PyArray_Descr *NPY_UNUSED(descr), int NPY_UNUSED(aligned), npy_intp NPY_UNUSED(fixed_stride), - traverse_loop_function **out_loop, + PyArrayMethod_TraverseLoop **out_loop, NpyAuxData **NPY_UNUSED(out_auxdata), NPY_ARRAYMETHOD_FLAGS *flags) { @@ -676,7 +690,7 @@ stringdtype_dealloc(StringDTypeObject *self) if (self->allocator != NULL) { // can we assume the destructor for an instance will only get called // inside of one C thread? - NpyString_free_allocator(self->allocator); + _NpyString_free_allocator(self->allocator); PyThread_free_lock(self->allocator_lock); } PyMem_RawFree((char *)self->na_name.buf); @@ -931,14 +945,14 @@ free_and_copy(npy_string_allocator *in_allocator, const npy_packed_static_string *in, npy_packed_static_string *out, const char *location) { - if (NpyString_free(out, out_allocator) < 0) { + if (_NpyString_free(out, out_allocator) < 0) { char message[200]; snprintf(message, sizeof(message), "Failed to deallocate string in %s", location); gil_error(PyExc_MemoryError, message); return -1; } - if (NpyString_dup(in, out, in_allocator, out_allocator) < 0) { + if (_NpyString_dup(in, out, in_allocator, out_allocator) < 0) { char message[200]; snprintf(message, sizeof(message), "Failed to allocate string in %s", location); diff --git a/stringdtype/stringdtype/src/dtype.h b/stringdtype/stringdtype/src/dtype.h index 809c55a0..b1aa811c 100644 --- a/stringdtype/stringdtype/src/dtype.h +++ b/stringdtype/stringdtype/src/dtype.h @@ -8,17 +8,6 @@ #include "static_string.h" -#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION -#define NPY_TARGET_VERSION NPY_2_0_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/halffloat.h" -#include "numpy/ndarraytypes.h" -#include "numpy/npy_math.h" -#include "numpy/ufuncobject.h" - // not publicly exposed by the static string library so we need to define // this here so we can define `elsize` and `alignment` on the descr // @@ -34,8 +23,8 @@ typedef struct { int has_nan_na; int has_string_na; int array_owned; - npy_static_string default_string; - npy_static_string na_name; + _npy_static_string default_string; + _npy_static_string na_name; PyThread_type_lock *allocator_lock; // the allocator should only be directly accessed after // acquiring the allocator_lock and the lock should @@ -52,7 +41,7 @@ extern StringDType_type StringDType; extern PyTypeObject *StringScalar_Type; static inline npy_string_allocator * -NpyString_acquire_allocator(StringDTypeObject *descr) +_NpyString_acquire_allocator(StringDTypeObject *descr) { if (!PyThread_acquire_lock(descr->allocator_lock, NOWAIT_LOCK)) { PyThread_acquire_lock(descr->allocator_lock, WAIT_LOCK); @@ -61,14 +50,14 @@ NpyString_acquire_allocator(StringDTypeObject *descr) } static inline void -NpyString_acquire_allocator2(StringDTypeObject *descr1, +_NpyString_acquire_allocator2(StringDTypeObject *descr1, StringDTypeObject *descr2, npy_string_allocator **allocator1, npy_string_allocator **allocator2) { - *allocator1 = NpyString_acquire_allocator(descr1); + *allocator1 = _NpyString_acquire_allocator(descr1); if (descr1 != descr2) { - *allocator2 = NpyString_acquire_allocator(descr2); + *allocator2 = _NpyString_acquire_allocator(descr2); } else { *allocator2 = *allocator1; @@ -76,16 +65,16 @@ NpyString_acquire_allocator2(StringDTypeObject *descr1, } static inline void -NpyString_acquire_allocator3(StringDTypeObject *descr1, +_NpyString_acquire_allocator3(StringDTypeObject *descr1, StringDTypeObject *descr2, StringDTypeObject *descr3, npy_string_allocator **allocator1, npy_string_allocator **allocator2, npy_string_allocator **allocator3) { - NpyString_acquire_allocator2(descr1, descr2, allocator1, allocator2); + _NpyString_acquire_allocator2(descr1, descr2, allocator1, allocator2); if (descr1 != descr3 && descr2 != descr3) { - *allocator3 = NpyString_acquire_allocator(descr3); + *allocator3 = _NpyString_acquire_allocator(descr3); } else { *allocator3 = descr3->allocator; @@ -93,29 +82,29 @@ NpyString_acquire_allocator3(StringDTypeObject *descr1, } static inline void -NpyString_release_allocator(StringDTypeObject *descr) +_NpyString_release_allocator(StringDTypeObject *descr) { PyThread_release_lock(descr->allocator_lock); } static inline void -NpyString_release_allocator2(StringDTypeObject *descr1, +_NpyString_release_allocator2(StringDTypeObject *descr1, StringDTypeObject *descr2) { - NpyString_release_allocator(descr1); + _NpyString_release_allocator(descr1); if (descr1 != descr2) { - NpyString_release_allocator(descr2); + _NpyString_release_allocator(descr2); } } static inline void -NpyString_release_allocator3(StringDTypeObject *descr1, +_NpyString_release_allocator3(StringDTypeObject *descr1, StringDTypeObject *descr2, StringDTypeObject *descr3) { - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); if (descr1 != descr3 && descr2 != descr3) { - NpyString_release_allocator(descr3); + _NpyString_release_allocator(descr3); } } diff --git a/stringdtype/stringdtype/src/main.c b/stringdtype/stringdtype/src/main.c index 502cf4b8..07b6bdc4 100644 --- a/stringdtype/stringdtype/src/main.c +++ b/stringdtype/stringdtype/src/main.c @@ -1,10 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL stringdtype_UFUNC_API #define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#include "numpy/ndarraytypes.h" #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "static_string.h" @@ -58,9 +61,9 @@ _memory_usage(PyObject *NPY_UNUSED(self), PyObject *obj) npy_intp count = *innersizeptr; while (count--) { - size_t size = NpyString_size(((npy_packed_static_string *)in)); + size_t size = _NpyString_size(((npy_packed_static_string *)in)); // FIXME: add a way for a string to report its heap size usage - if (size > (sizeof(npy_static_string) - 1)) { + if (size > (sizeof(_npy_static_string) - 1)) { memory_usage += size; } in += stride; @@ -93,10 +96,7 @@ PyMODINIT_FUNC PyInit__main(void) { import_array(); - - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -127,7 +127,7 @@ PyInit__main(void) goto error; } - if (init_ufuncs() < 0) { + if (init_ufuncs() == -1) { goto error; } diff --git a/stringdtype/stringdtype/src/static_string.c b/stringdtype/stringdtype/src/static_string.c index bdba5d12..330a24f3 100644 --- a/stringdtype/stringdtype/src/static_string.c +++ b/stringdtype/stringdtype/src/static_string.c @@ -38,7 +38,7 @@ typedef struct _short_string_buffer { typedef union _npy_static_string_u { _npy_static_vstring_t vstring; _short_string_buffer direct_buffer; -} _npy_static_string_u; +} _npy_static_string_u;; #define NPY_STRING_MISSING 0x80 // 1000 0000 #define NPY_STRING_SHORT 0x40 // 0100 0000 @@ -50,7 +50,7 @@ typedef union _npy_static_string_u { // short string sizes fit in a 4-bit integer #define NPY_SHORT_STRING_SIZE_MASK 0x0F // 0000 1111 #define NPY_SHORT_STRING_MAX_SIZE \ - (sizeof(npy_static_string) - 1) // 15 or 7 depending on arch + (sizeof(_npy_static_string) - 1) // 15 or 7 depending on arch #define NPY_MEDIUM_STRING_MAX_SIZE 0xFF // 256 // Since this has no flags set, technically this is a heap-allocated string @@ -185,7 +185,7 @@ arena_free(npy_string_arena *arena, _npy_static_string_u *str) static npy_string_arena NEW_ARENA = {0, 0, NULL}; npy_string_allocator * -NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f, +_NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f, npy_string_realloc_func r) { npy_string_allocator *allocator = m(sizeof(npy_string_allocator)); @@ -201,7 +201,7 @@ NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f, } void -NpyString_free_allocator(npy_string_allocator *allocator) +_NpyString_free_allocator(npy_string_allocator *allocator) { npy_string_free_func f = allocator->free; @@ -232,7 +232,7 @@ is_medium_string(const _npy_static_string_u *s) } int -NpyString_isnull(const npy_packed_static_string *s) +_NpyString_isnull(const npy_packed_static_string *s) { unsigned char high_byte = ((_npy_static_string_u *)s)->direct_buffer.size_and_flags; @@ -242,7 +242,7 @@ NpyString_isnull(const npy_packed_static_string *s) int is_not_a_vstring(const npy_packed_static_string *s) { - return is_short_string(s) || NpyString_isnull(s); + return is_short_string(s) || _NpyString_isnull(s); } int @@ -252,11 +252,11 @@ is_a_vstring(const npy_packed_static_string *s) } int -NpyString_load(npy_string_allocator *allocator, +_NpyString_load(npy_string_allocator *allocator, const npy_packed_static_string *packed_string, - npy_static_string *unpacked_string) + _npy_static_string *unpacked_string) { - if (NpyString_isnull(packed_string)) { + if (_NpyString_isnull(packed_string)) { unpacked_string->size = 0; unpacked_string->buf = NULL; return 1; @@ -380,11 +380,11 @@ heap_or_arena_deallocate(npy_string_allocator *allocator, } int -NpyString_newsize(const char *init, size_t size, +_NpyString_newsize(const char *init, size_t size, npy_packed_static_string *to_init, npy_string_allocator *allocator) { - if (NpyString_newemptysize(size, to_init, allocator) < 0) { + if (_NpyString_newemptysize(size, to_init, allocator) < 0) { return -1; } @@ -409,7 +409,7 @@ NpyString_newsize(const char *init, size_t size, } int -NpyString_newemptysize(size_t size, npy_packed_static_string *out, +_NpyString_newemptysize(size_t size, npy_packed_static_string *out, npy_string_allocator *allocator) { if (size > NPY_MAX_STRING_SIZE) { @@ -459,7 +459,7 @@ NpyString_newemptysize(size_t size, npy_packed_static_string *out, } int -NpyString_free(npy_packed_static_string *str, npy_string_allocator *allocator) +_NpyString_free(npy_packed_static_string *str, npy_string_allocator *allocator) { _npy_static_string_u *str_u = (_npy_static_string_u *)str; if (is_not_a_vstring(str)) { @@ -482,13 +482,13 @@ NpyString_free(npy_packed_static_string *str, npy_string_allocator *allocator) } int -NpyString_dup(const npy_packed_static_string *in, +_NpyString_dup(const npy_packed_static_string *in, npy_packed_static_string *out, npy_string_allocator *in_allocator, npy_string_allocator *out_allocator) { - if (NpyString_isnull(in)) { - return NpyString_pack_null(out_allocator, out); + if (_NpyString_isnull(in)) { + return _NpyString_pack_null(out_allocator, out); } if (is_short_string(in)) { memcpy(out, in, sizeof(_npy_static_string_u)); @@ -519,7 +519,7 @@ NpyString_dup(const npy_packed_static_string *in, in_buf = vstring_buffer(arena, in_u); } int ret = - NpyString_newsize(in_buf, VSTRING_SIZE(in_u), out, out_allocator); + _NpyString_newsize(in_buf, VSTRING_SIZE(in_u), out, out_allocator); if (used_malloc) { in_allocator->free(in_buf); } @@ -527,7 +527,7 @@ NpyString_dup(const npy_packed_static_string *in, } int -NpyString_cmp(const npy_static_string *s1, const npy_static_string *s2) +_NpyString_cmp(const _npy_static_string *s1, const _npy_static_string *s2) { size_t minsize = s1->size < s2->size ? s1->size : s2->size; @@ -550,9 +550,9 @@ NpyString_cmp(const npy_static_string *s1, const npy_static_string *s2) } size_t -NpyString_size(const npy_packed_static_string *packed_string) +_NpyString_size(const npy_packed_static_string *packed_string) { - if (NpyString_isnull(packed_string)) { + if (_NpyString_isnull(packed_string)) { return 0; } @@ -567,24 +567,24 @@ NpyString_size(const npy_packed_static_string *packed_string) } int -NpyString_pack(npy_string_allocator *allocator, +_NpyString_pack(npy_string_allocator *allocator, npy_packed_static_string *packed_string, const char *buf, size_t size) { - if (NpyString_free(packed_string, allocator) < 0) { + if (_NpyString_free(packed_string, allocator) < 0) { return -1; } - return NpyString_newsize(buf, size, packed_string, allocator); + return _NpyString_newsize(buf, size, packed_string, allocator); } int -NpyString_pack_null(npy_string_allocator *allocator, +_NpyString_pack_null(npy_string_allocator *allocator, npy_packed_static_string *packed_string) { _npy_static_string_u *str_u = (_npy_static_string_u *)packed_string; unsigned char *flags = &str_u->direct_buffer.size_and_flags; unsigned char current_flags = *flags & ~NPY_SHORT_STRING_SIZE_MASK; - if (NpyString_free(packed_string, allocator) < 0) { + if (_NpyString_free(packed_string, allocator) < 0) { return -1; } memcpy(str_u, &empty_string_u, sizeof(_npy_static_string_u)); diff --git a/stringdtype/stringdtype/src/static_string.h b/stringdtype/stringdtype/src/static_string.h index 31b93f50..3860f105 100644 --- a/stringdtype/stringdtype/src/static_string.h +++ b/stringdtype/stringdtype/src/static_string.h @@ -1,15 +1,15 @@ -#ifndef _NPY_STATIC_STRING_H -#define _NPY_STATIC_STRING_H +#ifndef __NPY_STATIC_STRING_H +#define __NPY_STATIC_STRING_H #include "stdint.h" #include "stdlib.h" typedef struct npy_packed_static_string npy_packed_static_string; -typedef struct npy_unpacked_static_string { +typedef struct _npy_unpacked_static_string { size_t size; const char *buf; -} npy_static_string; +} _npy_static_string; // one byte in size is reserved for flags and small string optimization #define NPY_MAX_STRING_SIZE ((int64_t)1 << 8 * (sizeof(size_t) - 1)) - 1 @@ -26,17 +26,17 @@ typedef void *(*npy_string_realloc_func)(void *ptr, size_t size); // users won't use these directly and will use an allocator already // attached to a dtype instance npy_string_allocator * -NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f, +_NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f, npy_string_realloc_func r); // Deallocates the internal buffer and the allocator itself. void -NpyString_free_allocator(npy_string_allocator *allocator); +_NpyString_free_allocator(npy_string_allocator *allocator); // Allocates a new buffer for *to_init*, which must be set to NULL before // calling this function, filling the newly allocated buffer with the copied // contents of the first *size* entries in *init*, which must be valid and -// initialized beforehand. Calling NpyString_free on *to_init* before calling +// initialized beforehand. Calling _NpyString_free on *to_init* before calling // this function on an existing string or copying the contents of // NPY_EMPTY_STRING into *to_init* is sufficient to initialize it. Does not // check if *to_init* is NULL or if the internal buffer is non-NULL, undefined @@ -45,7 +45,7 @@ NpyString_free_allocator(npy_string_allocator *allocator); // string. Returns -1 if allocating the string would exceed the maximum // allowed string size or exhaust available memory. Returns 0 on success. int -NpyString_newsize(const char *init, size_t size, +_NpyString_newsize(const char *init, size_t size, npy_packed_static_string *to_init, npy_string_allocator *allocator); @@ -53,14 +53,14 @@ NpyString_newsize(const char *init, size_t size, // arena-allocated data, checks if the data are inside the arena and // will return -1 if not. Returns 0 on success. int -NpyString_free(npy_packed_static_string *str, npy_string_allocator *allocator); +_NpyString_free(npy_packed_static_string *str, npy_string_allocator *allocator); // Copies the contents of *in* into *out*. Allocates a new string buffer for -// *out*, NpyString_free *must* be called before this is called if *out* +// *out*, _NpyString_free *must* be called before this is called if *out* // points to an existing string. Returns -1 if malloc fails. Returns 0 on // success. int -NpyString_dup(const npy_packed_static_string *in, +_NpyString_dup(const npy_packed_static_string *in, npy_packed_static_string *out, npy_string_allocator *in_allocator, npy_string_allocator *out_allocator); @@ -68,7 +68,7 @@ NpyString_dup(const npy_packed_static_string *in, // Allocates a new string buffer for *out* with enough capacity to store // *size* bytes of text. Does not do any initialization, the caller must // initialize the string buffer after this function returns. Calling -// NpyString_free on *to_init* before calling this function on an existing +// _NpyString_free on *to_init* before calling this function on an existing // string or initializing a new string with the contents of NPY_EMPTY_STRING // is sufficient to initialize it. Does not check if *to_init* has already // been initialized or if the internal buffer is non-NULL, undefined behavior @@ -77,35 +77,35 @@ NpyString_dup(const npy_packed_static_string *in, // allocating the string would exceed the maximum allowed string size or // exhaust available memory. Returns 0 on success. int -NpyString_newemptysize(size_t size, npy_packed_static_string *out, +_NpyString_newemptysize(size_t size, npy_packed_static_string *out, npy_string_allocator *allocator); // Determine if *in* corresponds to a null string (e.g. an NA object). Returns // -1 if *in* cannot be unpacked. Returns 1 if *in* is a null string and // zero otherwise. int -NpyString_isnull(const npy_packed_static_string *in); +_NpyString_isnull(const npy_packed_static_string *in); // Compare two strings. Has the same semantics as if strcmp were passed // null-terminated C strings with the contents of *s1* and *s2*. int -NpyString_cmp(const npy_static_string *s1, const npy_static_string *s2); +_NpyString_cmp(const _npy_static_string *s1, const _npy_static_string *s2); // Copy and pack the first *size* entries of the buffer pointed to by *buf* // into the *packed_string*. Returns 0 on success and -1 on failure. int -NpyString_pack(npy_string_allocator *allocator, +_NpyString_pack(npy_string_allocator *allocator, npy_packed_static_string *packed_string, const char *buf, size_t size); // Pack the null string into the *packed_string*. Returns 0 on success and -1 // on failure. int -NpyString_pack_null(npy_string_allocator *allocator, +_NpyString_pack_null(npy_string_allocator *allocator, npy_packed_static_string *packed_string); // Extract the packed contents of *packed_string* into *unpacked_string*. A -// useful pattern is to define a stack-allocated npy_static_string instance +// useful pattern is to define a stack-allocated _npy_static_string instance // initialized to {0, NULL} and pass a pointer to the stack-allocated unpacked // string to this function to unpack the contents of a packed string. The // *unpacked_string* is a read-only view onto the *packed_string* data and @@ -115,14 +115,14 @@ NpyString_pack_null(npy_string_allocator *allocator, // string, and returns 0 otherwise. This function can be used to // simultaneously unpack a string and determine if it is a null string. int -NpyString_load(npy_string_allocator *allocator, +_NpyString_load(npy_string_allocator *allocator, const npy_packed_static_string *packed_string, - npy_static_string *unpacked_string); + _npy_static_string *unpacked_string); // Returns the size of the string data in the packed string. Useful in // situations where only the string size is needed and determining if it is a // null or unpacking the string is unnecessary. size_t -NpyString_size(const npy_packed_static_string *packed_string); +_NpyString_size(const npy_packed_static_string *packed_string); -#endif /*_NPY_STATIC_STRING_H */ +#endif /*__NPY_STATIC_STRING_H */ diff --git a/stringdtype/stringdtype/src/umath.c b/stringdtype/stringdtype/src/umath.c index 022db85c..8b2670ef 100644 --- a/stringdtype/stringdtype/src/umath.c +++ b/stringdtype/stringdtype/src/umath.c @@ -1,4 +1,16 @@ #include +#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL stringdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/ndarraytypes.h" +#include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" +#include "numpy/halffloat.h" +#include "numpy/npy_math.h" #include "umath.h" @@ -8,7 +20,7 @@ static NPY_CASTING multiply_resolve_descriptors( struct PyArrayMethodObject_tag *NPY_UNUSED(method), - PyArray_DTypeMeta *dtypes[], PyArray_Descr *given_descrs[], + PyArray_DTypeMeta *const dtypes[], PyArray_Descr *const given_descrs[], PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset)) { PyArray_Descr *ldescr = given_descrs[0]; @@ -49,19 +61,19 @@ multiply_resolve_descriptors( npy_intp N, char *sin, char *iin, char *out, npy_intp s_stride, \ npy_intp i_stride, npy_intp o_stride, int has_null, \ int has_nan_na, int has_string_na, \ - const npy_static_string *default_string, \ + const _npy_static_string *default_string, \ StringDTypeObject *idescr, StringDTypeObject *odescr) \ { \ npy_string_allocator *iallocator = NULL; \ npy_string_allocator *oallocator = NULL; \ - NpyString_acquire_allocator2(idescr, odescr, &iallocator, \ + _NpyString_acquire_allocator2(idescr, odescr, &iallocator, \ &oallocator); \ while (N--) { \ const npy_packed_static_string *ips = \ (npy_packed_static_string *)sin; \ - npy_static_string is = {0, NULL}; \ + _npy_static_string is = {0, NULL}; \ npy_packed_static_string *ops = (npy_packed_static_string *)out; \ - int is_isnull = NpyString_load(iallocator, ips, &is); \ + int is_isnull = _NpyString_load(iallocator, ips, &is); \ if (is_isnull == -1) { \ gil_error(PyExc_MemoryError, \ "Failed to load string in multiply"); \ @@ -69,7 +81,7 @@ multiply_resolve_descriptors( } \ else if (is_isnull) { \ if (has_nan_na) { \ - if (NpyString_pack_null(oallocator, ops) < 0) { \ + if (_NpyString_pack_null(oallocator, ops) < 0) { \ gil_error(PyExc_MemoryError, \ "Failed to deallocate string in multiply"); \ goto fail; \ @@ -81,7 +93,7 @@ multiply_resolve_descriptors( continue; \ } \ else if (has_string_na || !has_null) { \ - is = *(npy_static_string *)default_string; \ + is = *(_npy_static_string *)default_string; \ } \ else { \ gil_error(PyExc_TypeError, \ @@ -101,7 +113,7 @@ multiply_resolve_descriptors( } \ \ char *buf = NULL; \ - npy_static_string os = {0, NULL}; \ + _npy_static_string os = {0, NULL}; \ if (odescr == idescr) { \ buf = PyMem_RawMalloc(newsize); \ if (buf == NULL) { \ @@ -111,17 +123,17 @@ multiply_resolve_descriptors( } \ } \ else { \ - if (NpyString_free(ops, oallocator) < 0) { \ + if (_NpyString_free(ops, oallocator) < 0) { \ gil_error(PyExc_MemoryError, \ "Failed to deallocate string in multiply"); \ goto fail; \ } \ - if (NpyString_newemptysize(newsize, ops, oallocator) < 0) { \ + if (_NpyString_newemptysize(newsize, ops, oallocator) < 0) { \ gil_error(PyExc_MemoryError, \ "Failed to allocate string in multiply"); \ goto fail; \ } \ - if (NpyString_load(oallocator, ops, &os) < 0) { \ + if (_NpyString_load(oallocator, ops, &os) < 0) { \ gil_error(PyExc_MemoryError, \ "Failed to load string in multiply"); \ goto fail; \ @@ -137,7 +149,7 @@ multiply_resolve_descriptors( } \ \ if (idescr == odescr) { \ - if (NpyString_pack(oallocator, ops, buf, newsize) < 0) { \ + if (_NpyString_pack(oallocator, ops, buf, newsize) < 0) { \ gil_error(PyExc_MemoryError, \ "Failed to pack string in multiply"); \ goto fail; \ @@ -150,11 +162,11 @@ multiply_resolve_descriptors( iin += i_stride; \ out += o_stride; \ } \ - NpyString_release_allocator2(idescr, odescr); \ + _NpyString_release_allocator2(idescr, odescr); \ return 0; \ \ fail: \ - NpyString_release_allocator2(idescr, odescr); \ + _NpyString_release_allocator2(idescr, odescr); \ return -1; \ } \ \ @@ -170,7 +182,7 @@ multiply_resolve_descriptors( int has_null = odescr->na_object != NULL; \ int has_nan_na = odescr->has_nan_na; \ int has_string_na = odescr->has_string_na; \ - const npy_static_string *default_string = &idescr->default_string; \ + const _npy_static_string *default_string = &idescr->default_string; \ npy_intp N = dimensions[0]; \ char *in1 = data[0]; \ char *in2 = data[1]; \ @@ -197,7 +209,7 @@ multiply_resolve_descriptors( int has_null = idescr->na_object != NULL; \ int has_nan_na = idescr->has_nan_na; \ int has_string_na = idescr->has_string_na; \ - const npy_static_string *default_string = &idescr->default_string; \ + const _npy_static_string *default_string = &idescr->default_string; \ npy_intp N = dimensions[0]; \ char *in1 = data[0]; \ char *in2 = data[1]; \ @@ -239,8 +251,8 @@ MULTIPLY_IMPL(ulonglong); static NPY_CASTING binary_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method), - PyArray_DTypeMeta *NPY_UNUSED(dtypes[]), - PyArray_Descr *given_descrs[], + PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]), + PyArray_Descr *const given_descrs[], PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset)) { @@ -299,7 +311,7 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], int has_null = s1descr->na_object != NULL; int has_nan_na = s1descr->has_nan_na; int has_string_na = s1descr->has_string_na; - const npy_static_string *default_string = &s1descr->default_string; + const _npy_static_string *default_string = &s1descr->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -311,16 +323,16 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *s1allocator = NULL; npy_string_allocator *s2allocator = NULL; npy_string_allocator *oallocator = NULL; - NpyString_acquire_allocator3(s1descr, s2descr, odescr, &s1allocator, + _NpyString_acquire_allocator3(s1descr, s2descr, odescr, &s1allocator, &s2allocator, &oallocator); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(s1allocator, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(s1allocator, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(s2allocator, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(s2allocator, ps2, &s2); if (s1_isnull == -1 || s2_isnull == -1) { gil_error(PyExc_MemoryError, "Failed to load string in add"); goto fail; @@ -328,7 +340,7 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_packed_static_string *ops = (npy_packed_static_string *)out; if (NPY_UNLIKELY(s1_isnull || s2_isnull)) { if (has_nan_na) { - if (NpyString_pack_null(oallocator, ops) < 0) { + if (_NpyString_pack_null(oallocator, ops) < 0) { gil_error(PyExc_MemoryError, "Failed to deallocate string in add"); goto fail; @@ -358,7 +370,7 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], } char *buf = NULL; - npy_static_string os = {0, NULL}; + _npy_static_string os = {0, NULL}; if (odescr == s1descr || odescr == s2descr) { buf = PyMem_RawMalloc(newsize); @@ -369,17 +381,17 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], } } else { - if (NpyString_free(ops, oallocator) < 0) { + if (_NpyString_free(ops, oallocator) < 0) { gil_error(PyExc_MemoryError, "Failed to deallocate string in add"); goto fail; } - if (NpyString_newemptysize(newsize, ops, oallocator) < 0) { + if (_NpyString_newemptysize(newsize, ops, oallocator) < 0) { gil_error(PyExc_MemoryError, "Failed to allocate string in add"); goto fail; } - if (NpyString_load(oallocator, ops, &os) < 0) { + if (_NpyString_load(oallocator, ops, &os) < 0) { gil_error(PyExc_MemoryError, "Failed to load string in add"); goto fail; } @@ -391,7 +403,7 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], memcpy(buf + s1.size, s2.buf, s2.size); if (s1descr == odescr || s2descr == odescr) { - if (NpyString_pack(oallocator, ops, buf, newsize) < 0) { + if (_NpyString_pack(oallocator, ops, buf, newsize) < 0) { gil_error(PyExc_MemoryError, "Failed to pack output string in add"); goto fail; @@ -405,11 +417,11 @@ add_strided_loop(PyArrayMethod_Context *context, char *const data[], in2 += in2_stride; out += out_stride; } - NpyString_release_allocator3(s1descr, s2descr, odescr); + _NpyString_release_allocator3(s1descr, s2descr, odescr); return 0; fail: - NpyString_release_allocator3(s1descr, s2descr, odescr); + _NpyString_release_allocator3(s1descr, s2descr, odescr); return -1; } @@ -435,7 +447,7 @@ maximum_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *in1_allocator = NULL; npy_string_allocator *in2_allocator = NULL; npy_string_allocator *out_allocator = NULL; - NpyString_acquire_allocator3(in1_descr, in2_descr, out_descr, + _NpyString_acquire_allocator3(in1_descr, in2_descr, out_descr, &in1_allocator, &in2_allocator, &out_allocator); @@ -466,11 +478,11 @@ maximum_strided_loop(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator3(in1_descr, in2_descr, out_descr); + _NpyString_release_allocator3(in1_descr, in2_descr, out_descr); return 0; fail: - NpyString_release_allocator3(in1_descr, in2_descr, out_descr); + _NpyString_release_allocator3(in1_descr, in2_descr, out_descr); return -1; } @@ -496,7 +508,7 @@ minimum_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *in1_allocator = NULL; npy_string_allocator *in2_allocator = NULL; npy_string_allocator *out_allocator = NULL; - NpyString_acquire_allocator3(in1_descr, in2_descr, out_descr, + _NpyString_acquire_allocator3(in1_descr, in2_descr, out_descr, &in1_allocator, &in2_allocator, &out_allocator); @@ -527,11 +539,11 @@ minimum_strided_loop(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator3(in1_descr, in2_descr, out_descr); + _NpyString_release_allocator3(in1_descr, in2_descr, out_descr); return 0; fail: - NpyString_release_allocator3(in1_descr, in2_descr, out_descr); + _NpyString_release_allocator3(in1_descr, in2_descr, out_descr); return -1; } @@ -546,7 +558,7 @@ string_equal_strided_loop(PyArrayMethod_Context *context, char *const data[], int has_null = descr1->na_object != NULL; int has_nan_na = descr1->has_nan_na; int has_string_na = descr1->has_string_na; - const npy_static_string *default_string = &descr1->default_string; + const _npy_static_string *default_string = &descr1->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -557,15 +569,15 @@ string_equal_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *allocator1 = NULL; npy_string_allocator *allocator2 = NULL; - NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); + _NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(allocator1, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(allocator1, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(allocator2, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(allocator2, ps2, &s2); if (NPY_UNLIKELY(s1_isnull < 0 || s2_isnull < 0)) { gil_error(PyExc_MemoryError, "Failed to load string in equal"); goto fail; @@ -607,12 +619,12 @@ string_equal_strided_loop(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return 0; fail: - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return -1; } @@ -628,7 +640,7 @@ string_not_equal_strided_loop(PyArrayMethod_Context *context, int has_null = descr1->na_object != NULL; int has_nan_na = descr1->has_nan_na; int has_string_na = descr1->has_string_na; - const npy_static_string *default_string = &descr1->default_string; + const _npy_static_string *default_string = &descr1->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -639,15 +651,15 @@ string_not_equal_strided_loop(PyArrayMethod_Context *context, npy_string_allocator *allocator1 = NULL; npy_string_allocator *allocator2 = NULL; - NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); + _NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(allocator1, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(allocator1, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(allocator2, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(allocator2, ps2, &s2); if (NPY_UNLIKELY(s1_isnull < 0 || s2_isnull < 0)) { gil_error(PyExc_MemoryError, "Failed to load string in not equal"); goto fail; @@ -689,12 +701,12 @@ string_not_equal_strided_loop(PyArrayMethod_Context *context, out += out_stride; } - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return 0; fail: - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return -1; } @@ -710,7 +722,7 @@ string_greater_strided_loop(PyArrayMethod_Context *context, char *const data[], int has_null = descr1->na_object != NULL; int has_nan_na = descr1->has_nan_na; int has_string_na = descr1->has_string_na; - const npy_static_string *default_string = &descr1->default_string; + const _npy_static_string *default_string = &descr1->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -721,15 +733,15 @@ string_greater_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *allocator1 = NULL; npy_string_allocator *allocator2 = NULL; - NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); + _NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(allocator1, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(allocator1, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(allocator2, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(allocator2, ps2, &s2); if (NPY_UNLIKELY(s1_isnull < 0 || s2_isnull < 0)) { gil_error(PyExc_MemoryError, "Failed to load string in greater"); goto fail; @@ -755,7 +767,7 @@ string_greater_strided_loop(PyArrayMethod_Context *context, char *const data[], } } } - if (NpyString_cmp(&s1, &s2) > 0) { + if (_NpyString_cmp(&s1, &s2) > 0) { *out = (npy_bool)1; } else { @@ -768,12 +780,12 @@ string_greater_strided_loop(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return 0; fail: - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return -1; } @@ -790,7 +802,7 @@ string_greater_equal_strided_loop(PyArrayMethod_Context *context, int has_null = descr1->na_object != NULL; int has_nan_na = descr1->has_nan_na; int has_string_na = descr1->has_string_na; - const npy_static_string *default_string = &descr1->default_string; + const _npy_static_string *default_string = &descr1->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -801,15 +813,15 @@ string_greater_equal_strided_loop(PyArrayMethod_Context *context, npy_string_allocator *allocator1 = NULL; npy_string_allocator *allocator2 = NULL; - NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); + _NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(allocator1, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(allocator1, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(allocator2, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(allocator2, ps2, &s2); if (NPY_UNLIKELY(s1_isnull < 0 || s2_isnull < 0)) { gil_error(PyExc_MemoryError, "Failed to load string in greater equal"); @@ -836,7 +848,7 @@ string_greater_equal_strided_loop(PyArrayMethod_Context *context, } } } - if (NpyString_cmp(&s1, &s2) >= 0) { + if (_NpyString_cmp(&s1, &s2) >= 0) { *out = (npy_bool)1; } else { @@ -849,12 +861,12 @@ string_greater_equal_strided_loop(PyArrayMethod_Context *context, out += out_stride; } - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return 0; fail: - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return -1; } @@ -869,7 +881,7 @@ string_less_strided_loop(PyArrayMethod_Context *context, char *const data[], int has_null = descr1->na_object != NULL; int has_nan_na = descr1->has_nan_na; int has_string_na = descr1->has_string_na; - const npy_static_string *default_string = &descr1->default_string; + const _npy_static_string *default_string = &descr1->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -880,15 +892,15 @@ string_less_strided_loop(PyArrayMethod_Context *context, char *const data[], npy_string_allocator *allocator1 = NULL; npy_string_allocator *allocator2 = NULL; - NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); + _NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(allocator1, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(allocator1, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(allocator2, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(allocator2, ps2, &s2); if (NPY_UNLIKELY(s1_isnull < 0 || s2_isnull < 0)) { gil_error(PyExc_MemoryError, "Failed to load string in less"); goto fail; @@ -914,7 +926,7 @@ string_less_strided_loop(PyArrayMethod_Context *context, char *const data[], } } } - if (NpyString_cmp(&s1, &s2) < 0) { + if (_NpyString_cmp(&s1, &s2) < 0) { *out = (npy_bool)1; } else { @@ -927,12 +939,12 @@ string_less_strided_loop(PyArrayMethod_Context *context, char *const data[], out += out_stride; } - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return 0; fail: - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return -1; } @@ -948,7 +960,7 @@ string_less_equal_strided_loop(PyArrayMethod_Context *context, int has_null = descr1->na_object != NULL; int has_nan_na = descr1->has_nan_na; int has_string_na = descr1->has_string_na; - const npy_static_string *default_string = &descr1->default_string; + const _npy_static_string *default_string = &descr1->default_string; npy_intp N = dimensions[0]; char *in1 = data[0]; char *in2 = data[1]; @@ -959,15 +971,15 @@ string_less_equal_strided_loop(PyArrayMethod_Context *context, npy_string_allocator *allocator1 = NULL; npy_string_allocator *allocator2 = NULL; - NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); + _NpyString_acquire_allocator2(descr1, descr2, &allocator1, &allocator2); while (N--) { const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; - npy_static_string s1 = {0, NULL}; - int s1_isnull = NpyString_load(allocator1, ps1, &s1); + _npy_static_string s1 = {0, NULL}; + int s1_isnull = _NpyString_load(allocator1, ps1, &s1); const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; - npy_static_string s2 = {0, NULL}; - int s2_isnull = NpyString_load(allocator2, ps2, &s2); + _npy_static_string s2 = {0, NULL}; + int s2_isnull = _NpyString_load(allocator2, ps2, &s2); if (NPY_UNLIKELY(s1_isnull < 0 || s2_isnull < 0)) { gil_error(PyExc_MemoryError, "Failed to load string in less equal"); @@ -994,7 +1006,7 @@ string_less_equal_strided_loop(PyArrayMethod_Context *context, } } } - if (NpyString_cmp(&s1, &s2) <= 0) { + if (_NpyString_cmp(&s1, &s2) <= 0) { *out = (npy_bool)1; } else { @@ -1007,12 +1019,12 @@ string_less_equal_strided_loop(PyArrayMethod_Context *context, out += out_stride; } - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return 0; fail: - NpyString_release_allocator2(descr1, descr2); + _NpyString_release_allocator2(descr1, descr2); return -1; } @@ -1020,7 +1032,8 @@ string_less_equal_strided_loop(PyArrayMethod_Context *context, static NPY_CASTING string_comparison_resolve_descriptors( struct PyArrayMethodObject_tag *NPY_UNUSED(method), - PyArray_DTypeMeta *NPY_UNUSED(dtypes[]), PyArray_Descr *given_descrs[], + PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]), + PyArray_Descr *const given_descrs[], PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset)) { Py_INCREF(given_descrs[0]); @@ -1050,7 +1063,7 @@ string_isnan_strided_loop(PyArrayMethod_Context *context, char *const data[], while (N--) { const npy_packed_static_string *s = (npy_packed_static_string *)in; - if (has_nan_na && NpyString_isnull(s)) { + if (has_nan_na && _NpyString_isnull(s)) { *out = (npy_bool)1; } else { @@ -1067,7 +1080,8 @@ string_isnan_strided_loop(PyArrayMethod_Context *context, char *const data[], static NPY_CASTING string_isnan_resolve_descriptors( struct PyArrayMethodObject_tag *NPY_UNUSED(method), - PyArray_DTypeMeta *NPY_UNUSED(dtypes[]), PyArray_Descr *given_descrs[], + PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]), + PyArray_Descr *const given_descrs[], PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset)) { Py_INCREF(given_descrs[0]); @@ -1081,8 +1095,9 @@ string_isnan_resolve_descriptors( * Copied from NumPy, because NumPy doesn't always use it :) */ static int -string_inputs_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[], - PyArray_DTypeMeta *signature[], +string_inputs_promoter(PyUFuncObject *ufunc, + PyArray_DTypeMeta *const op_dtypes[], + PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[], PyArray_DTypeMeta *final_dtype) { @@ -1105,8 +1120,9 @@ string_inputs_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[], } static int -string_object_promoter(PyObject *ufunc, PyArray_DTypeMeta *op_dtypes[], - PyArray_DTypeMeta *signature[], +string_object_promoter(PyObject *ufunc, + PyArray_DTypeMeta *const op_dtypes[], + PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[]) { return string_inputs_promoter((PyUFuncObject *)ufunc, op_dtypes, signature, @@ -1115,8 +1131,9 @@ string_object_promoter(PyObject *ufunc, PyArray_DTypeMeta *op_dtypes[], } static int -string_unicode_promoter(PyObject *ufunc, PyArray_DTypeMeta *op_dtypes[], - PyArray_DTypeMeta *signature[], +string_unicode_promoter(PyObject *ufunc, + PyArray_DTypeMeta *const op_dtypes[], + PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[]) { return string_inputs_promoter((PyUFuncObject *)ufunc, op_dtypes, signature, @@ -1125,8 +1142,9 @@ string_unicode_promoter(PyObject *ufunc, PyArray_DTypeMeta *op_dtypes[], } static int -string_multiply_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *op_dtypes[], - PyArray_DTypeMeta *signature[], +string_multiply_promoter(PyObject *ufunc_obj, + PyArray_DTypeMeta *const op_dtypes[], + PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[]) { PyUFuncObject *ufunc = (PyUFuncObject *)ufunc_obj; @@ -1135,7 +1153,7 @@ string_multiply_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *op_dtypes[], if (signature[i]) { tmp = signature[i]; } - else if (op_dtypes[i] == &PyArray_PyIntAbstractDType) { + else if (op_dtypes[i] == &PyArray_IntAbstractDType || op_dtypes[i] == &PyArray_PyLongDType) { tmp = &PyArray_Int64DType; } else if (op_dtypes[i]) { @@ -1160,7 +1178,7 @@ string_multiply_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *op_dtypes[], // Pass NULL for resolve_func to use the default_resolve_descriptors. int init_ufunc(PyObject *numpy, const char *ufunc_name, PyArray_DTypeMeta **dtypes, - resolve_descriptors_function *resolve_func, + PyArrayMethod_ResolveDescriptors *resolve_func, PyArrayMethod_StridedLoop *loop_func, int nin, int nout, NPY_CASTING casting, NPY_ARRAYMETHOD_FLAGS flags) { @@ -1210,7 +1228,7 @@ init_ufunc(PyObject *numpy, const char *ufunc_name, PyArray_DTypeMeta **dtypes, int add_promoter(PyObject *numpy, const char *ufunc_name, PyArray_DTypeMeta *ldtype, PyArray_DTypeMeta *rdtype, - PyArray_DTypeMeta *edtype, promoter_function *promoter_impl) + PyArray_DTypeMeta *edtype, PyArrayMethod_PromoterFunction *promoter_impl) { PyObject *ufunc = PyObject_GetAttrString((PyObject *)numpy, ufunc_name); @@ -1384,18 +1402,23 @@ init_ufuncs(void) INIT_MULTIPLY(ULongLong, ulonglong); #endif - if (add_promoter(numpy, "multiply", (PyArray_DTypeMeta *)&StringDType, - &PyArray_PyIntAbstractDType, - (PyArray_DTypeMeta *)&StringDType, - string_multiply_promoter) < 0) { - goto error; - } + PyArray_DTypeMeta *int_dtypes[2] = {&PyArray_IntAbstractDType, &PyArray_PyLongDType}; - if (add_promoter(numpy, "multiply", &PyArray_PyIntAbstractDType, - (PyArray_DTypeMeta *)&StringDType, - (PyArray_DTypeMeta *)&StringDType, - string_multiply_promoter) < 0) { - goto error; + for (int i=0; i<2; i++) { + + if (add_promoter(numpy, "multiply", (PyArray_DTypeMeta *)&StringDType, + int_dtypes[i], + (PyArray_DTypeMeta *)&StringDType, + string_multiply_promoter) < 0) { + goto error; + } + + if (add_promoter(numpy, "multiply", int_dtypes[i], + (PyArray_DTypeMeta *)&StringDType, + (PyArray_DTypeMeta *)&StringDType, + string_multiply_promoter) < 0) { + goto error; + } } Py_DECREF(numpy); diff --git a/stringdtype/tests/test_char.py b/stringdtype/tests/test_char.py deleted file mode 100644 index 74c5b289..00000000 --- a/stringdtype/tests/test_char.py +++ /dev/null @@ -1,126 +0,0 @@ -import numpy as np -import pytest -from numpy.testing import assert_array_equal - -from stringdtype import StringDType - -TEST_DATA = [ - "hello" * 10, - "Ae¢☃€ 😊" * 100, - "entry\nwith\nnewlines", - "entry\twith\ttabs", -] - - -@pytest.fixture -def string_array(): - return np.array(TEST_DATA, dtype=StringDType()) - - -@pytest.fixture -def unicode_array(): - return np.array(TEST_DATA, dtype=np.str_) - - -UNARY_FUNCTIONS = [ - # "str_len", - "capitalize", - "expandtabs", - "isalnum", - # "isalpha", - # "isdigit", - "islower", - # "isspace", - "istitle", - "isupper", - "lower", - "splitlines", - "swapcase", - "title", - "upper", - # "isnumeric", - # "isdecimal", -] - - -@pytest.mark.parametrize("function_name", UNARY_FUNCTIONS) -def test_unary(string_array, unicode_array, function_name): - func = getattr(np.char, function_name) - sres = func(string_array) - ures = func(unicode_array) - if sres.dtype == StringDType(): - ures = ures.astype(StringDType()) - assert_array_equal(sres, ures) - - -# None means that the argument is a string array -BINARY_FUNCTIONS = [ - ("add", (None, None)), - ("multiply", (None, 2)), - ("mod", ("format: %s", None)), - ("center", (None, 25)), - # ("count", (None, "A")), - ("encode", (None, "UTF-8")), - # ("endswith", (None, "lo")), - # ("find", (None, "A")), - ("index", (None, "e")), - ("join", ("-", None)), - ("ljust", (None, 12)), - ("partition", (None, "A")), - ("replace", (None, "A", "B")), - # ("rfind", (None, "A")), - ("rindex", (None, "e")), - ("rjust", (None, 12)), - ("rpartition", (None, "A")), - ("split", (None, "A")), - # ("startswith", (None, "A")), - ("zfill", (None, 12)), -] - - -@pytest.mark.parametrize("function_name, args", BINARY_FUNCTIONS) -def test_binary(string_array, unicode_array, function_name, args): - func = getattr(np.char, function_name) - if args == (None, None): - sres = func(string_array, string_array) - ures = func(unicode_array, unicode_array) - elif args[0] is None: - sres = func(string_array, *args[1:]) - ures = func(string_array, *args[1:]) - elif args[1] is None: - sres = func(args[0], string_array) - ures = func(args[0], string_array) - else: - # shouldn't ever happen - raise RuntimeError - if sres.dtype == StringDType(): - ures = ures.astype(StringDType()) - assert_array_equal(sres, ures) - - -def test_strip(string_array, unicode_array): - rjs = np.char.rjust(string_array, 1000) - rju = np.char.rjust(unicode_array, 1000) - - ljs = np.char.ljust(string_array, 1000) - lju = np.char.ljust(unicode_array, 1000) - - assert_array_equal( - np.char.lstrip(rjs), - np.char.lstrip(rju).astype(StringDType()), - ) - - assert_array_equal( - np.char.rstrip(ljs), - np.char.rstrip(lju).astype(StringDType()), - ) - - assert_array_equal( - np.char.strip(ljs), - np.char.strip(lju).astype(StringDType()), - ) - - assert_array_equal( - np.char.strip(rjs), - np.char.strip(rju).astype(StringDType()), - ) diff --git a/unytdtype/reinstall.sh b/unytdtype/reinstall.sh new file mode 100755 index 00000000..cac04b28 --- /dev/null +++ b/unytdtype/reinstall.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -xeuo pipefail +IFS=$'\n\t' + +if [ -d "build/" ] +then + rm -r build +fi + +#meson setup build -Db_sanitize=address,undefined +meson setup build +python -m pip uninstall -y unytdtype +python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' -Csetup-args="-Dbuildtype=debug" +#python -m pip install . -v --no-build-isolation -Cbuilddir=build -C'compile-args=-v' diff --git a/unytdtype/unytdtype/src/casts.c b/unytdtype/unytdtype/src/casts.c index 03925d23..be0060c7 100644 --- a/unytdtype/unytdtype/src/casts.c +++ b/unytdtype/unytdtype/src/casts.c @@ -1,10 +1,15 @@ #include +#include + #define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL unytdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "casts.h" @@ -442,7 +447,7 @@ static PyArray_DTypeMeta *u2u_dtypes[2] = {NULL, NULL}; static PyType_Slot u2u_slots[] = { {NPY_METH_resolve_descriptors, &unit_to_unit_resolve_descriptors}, - {_NPY_METH_get_loop, &unit_to_unit_get_loop}, + {NPY_METH_get_loop, &unit_to_unit_get_loop}, {0, NULL}}; static PyArrayMethod_Spec UnitToUnitCastSpec = { @@ -456,7 +461,7 @@ static PyArrayMethod_Spec UnitToUnitCastSpec = { }; static PyType_Slot u2f_slots[] = { - {_NPY_METH_get_loop, &unit_to_float64_get_loop}, {0, NULL}}; + {NPY_METH_get_loop, &unit_to_float64_get_loop}, {0, NULL}}; static char *u2f_name = "cast_UnytDType_to_Float64"; diff --git a/unytdtype/unytdtype/src/casts.h b/unytdtype/unytdtype/src/casts.h index 3c8176c0..386a869f 100644 --- a/unytdtype/unytdtype/src/casts.h +++ b/unytdtype/unytdtype/src/casts.h @@ -1,15 +1,6 @@ #ifndef _NPY_CASTS_H #define _NPY_CASTS_H -#include - -#define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/ndarraytypes.h" - /* Gets the conversion between two units: */ int get_conversion_factor(PyObject *from_unit, PyObject *to_unit, double *factor, diff --git a/unytdtype/unytdtype/src/dtype.c b/unytdtype/unytdtype/src/dtype.c index de96ccaf..6049e053 100644 --- a/unytdtype/unytdtype/src/dtype.c +++ b/unytdtype/unytdtype/src/dtype.c @@ -1,3 +1,18 @@ +// clang-format off +#include +#include "structmember.h" +// clang-format on + +#define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL unytdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#include "numpy/arrayobject.h" +#include "numpy/dtype_api.h" +#include "numpy/ndarraytypes.h" + #include "dtype.h" #include "casts.h" diff --git a/unytdtype/unytdtype/src/dtype.h b/unytdtype/unytdtype/src/dtype.h index 1f4f4c5e..9330000b 100644 --- a/unytdtype/unytdtype/src/dtype.h +++ b/unytdtype/unytdtype/src/dtype.h @@ -1,18 +1,6 @@ #ifndef _NPY_DTYPE_H #define _NPY_DTYPE_H -// clang-format off -#include -#include "structmember.h" -// clang-format on - -#define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#define NO_IMPORT_ARRAY -#include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" -#include "numpy/ndarraytypes.h" - typedef struct { PyArray_Descr base; PyObject *unit; diff --git a/unytdtype/unytdtype/src/umath.c b/unytdtype/unytdtype/src/umath.c index 0ded58c3..4f5c1ffb 100644 --- a/unytdtype/unytdtype/src/umath.c +++ b/unytdtype/unytdtype/src/umath.c @@ -1,10 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL unytdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION +#define NPY_TARGET_VERSION NPY_2_0_API_VERSION #define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" diff --git a/unytdtype/unytdtype/src/unytdtype_main.c b/unytdtype/unytdtype/src/unytdtype_main.c index 4c660792..75627f4a 100644 --- a/unytdtype/unytdtype/src/unytdtype_main.c +++ b/unytdtype/unytdtype/src/unytdtype_main.c @@ -1,9 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define PY_UFUNC_UNIQUE_SYMBOL unytdtype_UFUNC_API +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/ufuncobject.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -18,12 +20,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__unytdtype_main(void) { - if (_import_array() < 0) { - return NULL; - } - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -50,7 +48,7 @@ PyInit__unytdtype_main(void) goto error; } - if (init_multiply_ufunc() < 0) { + if (init_multiply_ufunc() == -1) { goto error; }