From 08214d1e87db84ff854a077ef7d7587bc2b1a105 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 10:48:49 -0600 Subject: [PATCH 01/11] Update stringdtype for NumPy 2.0 --- stringdtype/stringdtype/src/casts.c | 158 ++++++------ stringdtype/stringdtype/src/casts.h | 3 +- stringdtype/stringdtype/src/dtype.c | 76 +++--- stringdtype/stringdtype/src/dtype.h | 43 ++-- stringdtype/stringdtype/src/main.c | 18 +- stringdtype/stringdtype/src/static_string.c | 50 ++-- stringdtype/stringdtype/src/static_string.h | 44 ++-- stringdtype/stringdtype/src/umath.c | 262 +++++++++++--------- stringdtype/stringdtype/src/umath.h | 2 +- stringdtype/tests/test_char.py | 126 ---------- 10 files changed, 348 insertions(+), 434 deletions(-) delete mode 100644 stringdtype/tests/test_char.py diff --git a/stringdtype/stringdtype/src/casts.c b/stringdtype/stringdtype/src/casts.c index 1f0f3506..e5e1da4a 100644 --- a/stringdtype/stringdtype/src/casts.c +++ b/stringdtype/stringdtype/src/casts.c @@ -1,3 +1,15 @@ +#include +#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/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 +87,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 +96,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 +122,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 +231,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 +255,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 +299,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 +388,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 +404,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 +455,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 +501,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 +515,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 +542,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 +575,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 +594,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 +603,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 +625,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 +657,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 +676,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 +712,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 +768,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 +801,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 +831,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 +842,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 +915,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 +954,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 +987,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 +1039,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 +1051,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 +1074,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 +1097,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 +1153,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 +1171,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 +1209,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 +1241,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 +1274,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 +1287,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..c95cd3b3 100644 --- a/stringdtype/stringdtype/src/casts.h +++ b/stringdtype/stringdtype/src/casts.h @@ -9,7 +9,8 @@ #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" +#include "numpy/ndarraytypes.h" +#include "numpy/dtype_api.h" PyArrayMethod_Spec ** get_casts(); diff --git a/stringdtype/stringdtype/src/dtype.c b/stringdtype/stringdtype/src/dtype.c index 29a963da..c080b3b4 100644 --- a/stringdtype/stringdtype/src/dtype.c +++ b/stringdtype/stringdtype/src/dtype.c @@ -1,3 +1,15 @@ +#include +#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/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 +36,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 +50,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 +139,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 +274,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 +282,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 +303,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 +313,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 +328,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 +356,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 +373,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr) fail: - NpyString_release_allocator(descr); + _NpyString_release_allocator(descr); return NULL; } @@ -371,7 +383,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 +394,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 +412,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 +462,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 +506,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 +533,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 +688,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 +943,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..097f3542 100644 --- a/stringdtype/stringdtype/src/main.c +++ b/stringdtype/stringdtype/src/main.c @@ -3,8 +3,10 @@ #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 +#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 +60,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; @@ -94,10 +96,6 @@ PyInit__main(void) { import_array(); - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } - PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { return NULL; @@ -127,10 +125,14 @@ PyInit__main(void) goto error; } - if (init_ufuncs() < 0) { + PyObject *numpy = init_ufuncs(); + + if (numpy == NULL) { goto error; } + Py_DECREF(numpy); + return m; 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..4136bdae 100644 --- a/stringdtype/stringdtype/src/umath.c +++ b/stringdtype/stringdtype/src/umath.c @@ -1,4 +1,14 @@ #include +#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/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 +18,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 +59,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 +79,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 +91,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 +111,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 +121,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 +147,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 +160,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 +180,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 +207,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 +249,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 +309,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 +321,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 +338,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 +368,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 +379,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 +401,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 +415,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 +445,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 +476,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 +506,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 +537,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 +556,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 +567,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 +617,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 +638,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 +649,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 +699,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 +720,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 +731,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 +765,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 +778,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 +800,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 +811,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 +846,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 +859,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 +879,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 +890,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 +924,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 +937,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 +958,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 +969,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 +1004,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 +1017,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 +1030,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 +1061,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 +1078,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 +1093,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 +1118,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 +1129,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 +1140,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 +1151,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 +1176,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 +1226,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); @@ -1271,14 +1287,16 @@ add_promoter(PyObject *numpy, const char *ufunc_name, goto error; \ } -int +PyObject * init_ufuncs(void) { PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return -1; + return NULL; } + import_umath(); + static char *comparison_ufunc_names[6] = {"equal", "not_equal", "greater", "greater_equal", "less", "less_equal"}; @@ -1384,24 +1402,28 @@ 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); - return 0; + return numpy; error: Py_DECREF(numpy); - return -1; + return NULL; } diff --git a/stringdtype/stringdtype/src/umath.h b/stringdtype/stringdtype/src/umath.h index 0a7e5e92..616364be 100644 --- a/stringdtype/stringdtype/src/umath.h +++ b/stringdtype/stringdtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -int +PyObject * init_ufuncs(void); #endif /*_NPY_UFUNC_H */ 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()), - ) From 910c027a7b1e1ce2612da4ece94efe5d465e2527 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 11:17:51 -0600 Subject: [PATCH 02/11] update metadatadtype for numpy 2.0 --- metadatadtype/metadatadtype/src/casts.c | 9 ++++--- metadatadtype/metadatadtype/src/casts.h | 9 ------- metadatadtype/metadatadtype/src/dtype.c | 13 +++++++-- metadatadtype/metadatadtype/src/dtype.h | 7 ----- .../metadatadtype/src/metadatadtype_main.c | 16 +++++------ metadatadtype/metadatadtype/src/umath.c | 27 +++++++++++-------- metadatadtype/metadatadtype/src/umath.h | 4 +-- 7 files changed, 40 insertions(+), 45 deletions(-) diff --git a/metadatadtype/metadatadtype/src/casts.c b/metadatadtype/metadatadtype/src/casts.c index 191785a1..917097c5 100644 --- a/metadatadtype/metadatadtype/src/casts.c +++ b/metadatadtype/metadatadtype/src/casts.c @@ -1,10 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_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/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "casts.h" @@ -186,7 +187,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 +201,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..b20f751b 100644 --- a/metadatadtype/metadatadtype/src/dtype.c +++ b/metadatadtype/metadatadtype/src/dtype.c @@ -1,3 +1,11 @@ +#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_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/dtype_api.h" +#include "numpy/ndarraytypes.h" + #include "dtype.h" #include "casts.h" @@ -74,8 +82,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..da384ad1 100644 --- a/metadatadtype/metadatadtype/src/dtype.h +++ b/metadatadtype/metadatadtype/src/dtype.h @@ -6,13 +6,6 @@ #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..2aad1c4f 100644 --- a/metadatadtype/metadatadtype/src/metadatadtype_main.c +++ b/metadatadtype/metadatadtype/src/metadatadtype_main.c @@ -1,12 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_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 #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" -#include "dtype.h" #include "umath.h" +#include "dtype.h" static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, @@ -18,12 +19,7 @@ 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(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -51,7 +47,7 @@ PyInit__metadatadtype_main(void) goto error; } - if (init_ufuncs() < 0) { + if (init_ufuncs(m) == NULL) { goto error; } diff --git a/metadatadtype/metadatadtype/src/umath.c b/metadatadtype/metadatadtype/src/umath.c index 1be362a5..fc256074 100644 --- a/metadatadtype/metadatadtype/src/umath.c +++ b/metadatadtype/metadatadtype/src/umath.c @@ -1,20 +1,21 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_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/experimental_dtype_api.h" #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 +36,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[]) { @@ -102,9 +103,11 @@ add_wrapping_loop(const char *ufunc_name, PyArray_DTypeMeta **dtypes, return res; } -int -init_ufuncs(void) +PyObject * +init_ufuncs(PyObject *module) { + import_umath(); + PyArray_DTypeMeta *binary_orig_dtypes[3] = {&MetadataDType, &MetadataDType, &MetadataDType}; PyArray_DTypeMeta *binary_wrapped_dtypes[3] = { @@ -123,7 +126,9 @@ init_ufuncs(void) goto error; } - return 0; + return module; error: - return -1; + + Py_DECREF(module); + return NULL; } diff --git a/metadatadtype/metadatadtype/src/umath.h b/metadatadtype/metadatadtype/src/umath.h index 0a7e5e92..aee62841 100644 --- a/metadatadtype/metadatadtype/src/umath.h +++ b/metadatadtype/metadatadtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -int -init_ufuncs(void); +PyObject * +init_ufuncs(PyObject *module); #endif /*_NPY_UFUNC_H */ From efe33e27b204e07c1f693437fe324b27689621d3 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 11:32:53 -0600 Subject: [PATCH 03/11] Update asciidtype for numpy 2.0 --- asciidtype/asciidtype/src/asciidtype_main.c | 20 +++++++++---------- asciidtype/asciidtype/src/casts.c | 5 +++-- asciidtype/asciidtype/src/casts.h | 9 --------- asciidtype/asciidtype/src/dtype.c | 14 +++++++++++++ asciidtype/asciidtype/src/dtype.h | 12 ----------- asciidtype/asciidtype/src/umath.c | 22 +++++++++++---------- asciidtype/asciidtype/src/umath.h | 2 +- 7 files changed, 40 insertions(+), 44 deletions(-) diff --git a/asciidtype/asciidtype/src/asciidtype_main.c b/asciidtype/asciidtype/src/asciidtype_main.c index 5e7676cb..b6b29029 100644 --- a/asciidtype/asciidtype/src/asciidtype_main.c +++ b/asciidtype/asciidtype/src/asciidtype_main.c @@ -1,9 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL asciidtype_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 +#include "numpy/ndarraytypes.h" #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -18,13 +20,7 @@ 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(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -51,10 +47,14 @@ PyInit__asciidtype_main(void) goto error; } - if (init_ufuncs() < 0) { + PyObject *numpy = init_ufuncs(); + + if (numpy == NULL) { goto error; } + Py_DECREF(numpy); + return m; error: diff --git a/asciidtype/asciidtype/src/casts.c b/asciidtype/asciidtype/src/casts.c index 8b4acfab..4dde783b 100644 --- a/asciidtype/asciidtype/src/casts.c +++ b/asciidtype/asciidtype/src/casts.c @@ -1,10 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL asciidtype_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/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "casts.h" 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..dd4f3fde 100644 --- a/asciidtype/asciidtype/src/dtype.c +++ b/asciidtype/asciidtype/src/dtype.c @@ -1,3 +1,17 @@ +// clang-format off +#include +#include "structmember.h" +// clang-format on + +#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_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/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..b2dd7483 100644 --- a/asciidtype/asciidtype/src/umath.c +++ b/asciidtype/asciidtype/src/umath.c @@ -1,12 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL asciidtype_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/experimental_dtype_api.h" #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 +18,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 +113,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; @@ -218,12 +219,14 @@ init_equal_ufunc(PyObject *numpy) return 0; } -int +PyObject * init_ufuncs(void) { + import_umath(); + PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return -1; + return NULL; } if (init_add_ufunc(numpy) < 0) { @@ -234,10 +237,9 @@ init_ufuncs(void) goto error; } - Py_DECREF(numpy); - return 0; + return numpy; error: Py_DECREF(numpy); - return -1; + return NULL; } diff --git a/asciidtype/asciidtype/src/umath.h b/asciidtype/asciidtype/src/umath.h index 0a7e5e92..616364be 100644 --- a/asciidtype/asciidtype/src/umath.h +++ b/asciidtype/asciidtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -int +PyObject * init_ufuncs(void); #endif /*_NPY_UFUNC_H */ From 13f56725797a5059141bbe689b20dfd94ab19681 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 11:51:58 -0600 Subject: [PATCH 04/11] update unytdtype to work with numpy 2.0 --- unytdtype/unytdtype/src/casts.c | 11 +++++++---- unytdtype/unytdtype/src/casts.h | 9 --------- unytdtype/unytdtype/src/dtype.c | 13 +++++++++++++ unytdtype/unytdtype/src/dtype.h | 12 ------------ unytdtype/unytdtype/src/umath.c | 20 ++++++++++++-------- unytdtype/unytdtype/src/umath.h | 2 +- unytdtype/unytdtype/src/unytdtype_main.c | 17 ++++++++--------- 7 files changed, 41 insertions(+), 43 deletions(-) diff --git a/unytdtype/unytdtype/src/casts.c b/unytdtype/unytdtype/src/casts.c index 03925d23..d6e98f46 100644 --- a/unytdtype/unytdtype/src/casts.c +++ b/unytdtype/unytdtype/src/casts.c @@ -1,10 +1,13 @@ #include +#include + #define PY_ARRAY_UNIQUE_SYMBOL unytdtype_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/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "casts.h" @@ -442,7 +445,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 +459,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..93a2cc8d 100644 --- a/unytdtype/unytdtype/src/dtype.c +++ b/unytdtype/unytdtype/src/dtype.c @@ -1,3 +1,16 @@ +// clang-format off +#include +#include "structmember.h" +// clang-format on + +#define PY_ARRAY_UNIQUE_SYMBOL unytdtype_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/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..7a4ed4d8 100644 --- a/unytdtype/unytdtype/src/umath.c +++ b/unytdtype/unytdtype/src/umath.c @@ -1,10 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL unytdtype_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/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" @@ -64,20 +65,22 @@ unit_multiply_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *dtypes[], /* * Function that adds our multiply loop to NumPy's multiply ufunc. */ -int +PyObject * init_multiply_ufunc(void) { + import_umath(); + /* * Get the multiply ufunc: */ PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return -1; + return NULL; } PyObject *multiply = PyObject_GetAttrString(numpy, "multiply"); - Py_DECREF(numpy); if (multiply == NULL) { - return -1; + Py_DECREF(numpy); + return NULL; } /* @@ -103,8 +106,9 @@ init_multiply_ufunc(void) /* Register */ if (PyUFunc_AddLoopFromSpec(multiply, &MultiplySpec) < 0) { Py_DECREF(multiply); - return -1; + Py_DECREF(numpy); + return NULL; } Py_DECREF(multiply); - return 0; + return numpy; } diff --git a/unytdtype/unytdtype/src/umath.h b/unytdtype/unytdtype/src/umath.h index b883f57a..aa458f61 100644 --- a/unytdtype/unytdtype/src/umath.h +++ b/unytdtype/unytdtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -int +PyObject * init_multiply_ufunc(void); #endif /*_NPY_UFUNC_H */ diff --git a/unytdtype/unytdtype/src/unytdtype_main.c b/unytdtype/unytdtype/src/unytdtype_main.c index 4c660792..71b66d70 100644 --- a/unytdtype/unytdtype/src/unytdtype_main.c +++ b/unytdtype/unytdtype/src/unytdtype_main.c @@ -1,9 +1,9 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL unytdtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -18,12 +18,7 @@ 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(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -50,10 +45,14 @@ PyInit__unytdtype_main(void) goto error; } - if (init_multiply_ufunc() < 0) { + PyObject *numpy = init_multiply_ufunc(); + + if (numpy == NULL) { goto error; } + Py_DECREF(numpy); + return m; error: From 1bc3f00b25f3c5517170266465e29484c39e1b79 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 11:57:23 -0600 Subject: [PATCH 05/11] update quaddtype to work on numpy 2.0 --- quaddtype/quaddtype/src/casts.c | 14 +++++++++++-- quaddtype/quaddtype/src/casts.h | 1 - quaddtype/quaddtype/src/dtype.c | 10 +++++++++ quaddtype/quaddtype/src/dtype.h | 9 -------- quaddtype/quaddtype/src/quaddtype_main.c | 19 ++++++++--------- quaddtype/quaddtype/src/umath.c | 26 ++++++++++++++---------- quaddtype/quaddtype/src/umath.h | 2 +- 7 files changed, 46 insertions(+), 35 deletions(-) diff --git a/quaddtype/quaddtype/src/casts.c b/quaddtype/quaddtype/src/casts.c index d9e6b7c3..fb3fe28e 100644 --- a/quaddtype/quaddtype/src/casts.c +++ b/quaddtype/quaddtype/src/casts.c @@ -1,5 +1,15 @@ -#include "casts.h" +#include + +#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_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/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 +141,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..9722c5e7 100644 --- a/quaddtype/quaddtype/src/dtype.c +++ b/quaddtype/quaddtype/src/dtype.c @@ -1,3 +1,13 @@ +#include + +#define PY_ARRAY_UNIQUE_SYMBOL quaddtype_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/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..b9ccc035 100644 --- a/quaddtype/quaddtype/src/quaddtype_main.c +++ b/quaddtype/quaddtype/src/quaddtype_main.c @@ -1,9 +1,9 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL quaddtype_ARRAY_API -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -19,16 +19,9 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__quaddtype_main(void) { - if (_import_array() < 0) + 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; - } - PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { PyErr_SetString(PyExc_ImportError, "Unable to create the quaddtype_main module."); @@ -57,11 +50,15 @@ PyInit__quaddtype_main(void) goto error; } - if (init_multiply_ufunc() < 0) { + PyObject *numpy = init_multiply_ufunc(); + + if (numpy == NULL) { PyErr_SetString(PyExc_TypeError, "Failed to initialize the quadscalar multiply ufunc."); goto error; } + Py_DECREF(numpy); + return m; error: diff --git a/quaddtype/quaddtype/src/umath.c b/quaddtype/quaddtype/src/umath.c index ac223be9..6d519770 100644 --- a/quaddtype/quaddtype/src/umath.c +++ b/quaddtype/quaddtype/src/umath.c @@ -1,13 +1,13 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL quaddtype_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/arrayobject.h" #include "numpy/ufuncobject.h" - -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -73,20 +73,22 @@ quad_multiply_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *dtypes[], } // Function that adds our multiply loop to NumPy's multiply ufunc. -int +PyObject* init_multiply_ufunc(void) { + import_umath(); + // Get the multiply ufunc: PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return -1; + return NULL; } + PyObject *multiply = PyObject_GetAttrString(numpy, "multiply"); - // Why decref here? - Py_DECREF(numpy); if (multiply == NULL) { - return -1; + Py_DECREF(numpy); + return NULL; } // The initializing "wrap up" code from the slides (plus one error check) @@ -114,8 +116,10 @@ init_multiply_ufunc(void) /* Register */ if (PyUFunc_AddLoopFromSpec(multiply, &MultiplySpec) < 0) { Py_DECREF(multiply); - return -1; + Py_DECREF(numpy); + return NULL; } + Py_DECREF(multiply); - return 0; + return numpy; } diff --git a/quaddtype/quaddtype/src/umath.h b/quaddtype/quaddtype/src/umath.h index b883f57a..aa458f61 100644 --- a/quaddtype/quaddtype/src/umath.h +++ b/quaddtype/quaddtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -int +PyObject * init_multiply_ufunc(void); #endif /*_NPY_UFUNC_H */ From 0e89ac539770baadb5546966a9c9c5563b2cdb7f Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 12:15:34 -0600 Subject: [PATCH 06/11] Update mpfdtype for numpy 2.0 --- mpfdtype/mpfdtype/src/casts.cpp | 5 +++-- mpfdtype/mpfdtype/src/dtype.c | 5 +++-- mpfdtype/mpfdtype/src/mpfdtype_main.c | 13 +++++++------ mpfdtype/mpfdtype/src/numbers.cpp | 5 +++-- mpfdtype/mpfdtype/src/scalar.c | 4 ++-- mpfdtype/mpfdtype/src/terrible_hacks.c | 9 +++++---- mpfdtype/mpfdtype/src/umath.cpp | 15 +++++++++------ mpfdtype/mpfdtype/src/umath.h | 2 +- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/mpfdtype/mpfdtype/src/casts.cpp b/mpfdtype/mpfdtype/src/casts.cpp index 026d9a42..6a5aef44 100644 --- a/mpfdtype/mpfdtype/src/casts.cpp +++ b/mpfdtype/mpfdtype/src/casts.cpp @@ -1,6 +1,7 @@ #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" { @@ -8,7 +9,7 @@ extern "C" { #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..f4e7b1a5 100644 --- a/mpfdtype/mpfdtype/src/dtype.c +++ b/mpfdtype/mpfdtype/src/dtype.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" diff --git a/mpfdtype/mpfdtype/src/mpfdtype_main.c b/mpfdtype/mpfdtype/src/mpfdtype_main.c index 0a3ef398..b3452ba4 100644 --- a/mpfdtype/mpfdtype/src/mpfdtype_main.c +++ b/mpfdtype/mpfdtype/src/mpfdtype_main.c @@ -1,9 +1,9 @@ #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 #include "numpy/arrayobject.h" -#include "numpy/experimental_dtype_api.h" +#include "numpy/dtype_api.h" #include "dtype.h" #include "umath.h" @@ -22,9 +22,6 @@ PyInit__mpfdtype_main(void) if (_import_array() < 0) { return NULL; } - if (import_experimental_dtype_api(15) < 0) { - return NULL; - } PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -47,10 +44,14 @@ PyInit__mpfdtype_main(void) goto error; } - if (init_mpf_umath() < 0) { + PyObject *numpy = init_mpf_umath(); + + if (numpy == NULL) { goto error; } + Py_DECREF(numpy); + if (init_terrible_hacks() < 0) { 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..d6d98086 100644 --- a/mpfdtype/mpfdtype/src/umath.cpp +++ b/mpfdtype/mpfdtype/src/umath.cpp @@ -1,7 +1,8 @@ #include "scalar.h" #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" { @@ -11,7 +12,7 @@ extern "C" { #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" - #include "numpy/experimental_dtype_api.h" + #include "numpy/dtype_api.h" } #include @@ -591,15 +592,17 @@ init_comps(PyObject *numpy) /* * Function that adds our multiply loop to NumPy's multiply ufunc. */ -int +PyObject * init_mpf_umath(void) { + import_umath(); + /* * Get the multiply ufunc: */ PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return -1; + return NULL; } if (init_unary_ops(numpy) < 0) { @@ -612,10 +615,10 @@ init_mpf_umath(void) goto err; } - return 0; + return numpy; err: Py_DECREF(numpy); - return -1; + return NULL; } diff --git a/mpfdtype/mpfdtype/src/umath.h b/mpfdtype/mpfdtype/src/umath.h index 7db1b69e..3726c537 100644 --- a/mpfdtype/mpfdtype/src/umath.h +++ b/mpfdtype/mpfdtype/src/umath.h @@ -5,7 +5,7 @@ extern "C" { #endif -int +PyObject * init_mpf_umath(void); #ifdef __cplusplus From 75c05cd37d65ab2857e6679651119cfe776514a8 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 12:16:18 -0600 Subject: [PATCH 07/11] add scripts to help building and debugging --- asciidtype/reinstall.sh | 14 ++++++++++++++ metadatadtype/reinstall.sh | 14 ++++++++++++++ mpfdtype/reinstall.sh | 14 ++++++++++++++ quaddtype/reinstall.sh | 14 ++++++++++++++ stringdtype/reinstall.sh | 14 ++++++++++++++ unytdtype/reinstall.sh | 14 ++++++++++++++ 6 files changed, 84 insertions(+) create mode 100755 asciidtype/reinstall.sh create mode 100755 metadatadtype/reinstall.sh create mode 100755 mpfdtype/reinstall.sh create mode 100755 quaddtype/reinstall.sh create mode 100755 stringdtype/reinstall.sh create mode 100755 unytdtype/reinstall.sh 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/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/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/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/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/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' From b6edc011b8ee14ed2b5c9676ffe5ef1e7d423a94 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 12:17:29 -0600 Subject: [PATCH 08/11] update stringdtype readme --- stringdtype/README.md | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) 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()) -``` From bab245df699063b58f2bbdcb9109e305c23d98a1 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 12:53:01 -0600 Subject: [PATCH 09/11] Use unique symbol for ufunc API --- asciidtype/asciidtype/src/asciidtype_main.c | 9 ++++----- asciidtype/asciidtype/src/casts.c | 2 ++ asciidtype/asciidtype/src/dtype.c | 2 ++ asciidtype/asciidtype/src/umath.c | 12 ++++++------ asciidtype/asciidtype/src/umath.h | 2 +- metadatadtype/metadatadtype/src/casts.c | 2 ++ metadatadtype/metadatadtype/src/dtype.c | 7 +++++++ metadatadtype/metadatadtype/src/dtype.h | 5 ----- .../metadatadtype/src/metadatadtype_main.c | 5 ++++- metadatadtype/metadatadtype/src/umath.c | 13 ++++++------- metadatadtype/metadatadtype/src/umath.h | 4 ++-- mpfdtype/mpfdtype/src/casts.cpp | 2 ++ mpfdtype/mpfdtype/src/dtype.c | 2 ++ mpfdtype/mpfdtype/src/mpfdtype_main.c | 13 +++++-------- mpfdtype/mpfdtype/src/umath.cpp | 13 +++++++------ mpfdtype/mpfdtype/src/umath.h | 2 +- quaddtype/quaddtype/src/quaddtype_main.c | 6 +----- quaddtype/quaddtype/src/umath.c | 15 ++++++--------- quaddtype/quaddtype/src/umath.h | 2 +- stringdtype/stringdtype/src/casts.c | 2 ++ stringdtype/stringdtype/src/casts.h | 11 ----------- stringdtype/stringdtype/src/dtype.c | 2 ++ stringdtype/stringdtype/src/main.c | 8 +++----- stringdtype/stringdtype/src/umath.c | 13 +++++++------ stringdtype/stringdtype/src/umath.h | 2 +- unytdtype/unytdtype/src/casts.c | 2 ++ unytdtype/unytdtype/src/dtype.c | 2 ++ unytdtype/unytdtype/src/umath.c | 17 ++++++++--------- unytdtype/unytdtype/src/umath.h | 2 +- unytdtype/unytdtype/src/unytdtype_main.c | 9 ++++----- 30 files changed, 93 insertions(+), 95 deletions(-) diff --git a/asciidtype/asciidtype/src/asciidtype_main.c b/asciidtype/asciidtype/src/asciidtype_main.c index b6b29029..fdd3c9bd 100644 --- a/asciidtype/asciidtype/src/asciidtype_main.c +++ b/asciidtype/asciidtype/src/asciidtype_main.c @@ -1,10 +1,12 @@ #include #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 #include "numpy/ndarraytypes.h" #include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" #include "numpy/dtype_api.h" #include "dtype.h" @@ -21,6 +23,7 @@ PyMODINIT_FUNC PyInit__asciidtype_main(void) { import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -47,14 +50,10 @@ PyInit__asciidtype_main(void) goto error; } - PyObject *numpy = init_ufuncs(); - - if (numpy == NULL) { + if (init_ufuncs() == -1) { goto error; } - Py_DECREF(numpy); - return m; error: diff --git a/asciidtype/asciidtype/src/casts.c b/asciidtype/asciidtype/src/casts.c index 4dde783b..3b8a4949 100644 --- a/asciidtype/asciidtype/src/casts.c +++ b/asciidtype/asciidtype/src/casts.c @@ -1,9 +1,11 @@ #include #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/arrayobject.h" #include "numpy/dtype_api.h" #include "numpy/ndarraytypes.h" diff --git a/asciidtype/asciidtype/src/dtype.c b/asciidtype/asciidtype/src/dtype.c index dd4f3fde..8a94516f 100644 --- a/asciidtype/asciidtype/src/dtype.c +++ b/asciidtype/asciidtype/src/dtype.c @@ -4,9 +4,11 @@ // 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" diff --git a/asciidtype/asciidtype/src/umath.c b/asciidtype/asciidtype/src/umath.c index b2dd7483..bc98a063 100644 --- a/asciidtype/asciidtype/src/umath.c +++ b/asciidtype/asciidtype/src/umath.c @@ -1,9 +1,11 @@ #include #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" @@ -219,14 +221,12 @@ init_equal_ufunc(PyObject *numpy) return 0; } -PyObject * +int init_ufuncs(void) { - import_umath(); - PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return NULL; + return -1; } if (init_add_ufunc(numpy) < 0) { @@ -237,9 +237,9 @@ init_ufuncs(void) goto error; } - return numpy; + return 0; error: Py_DECREF(numpy); - return NULL; + return -1; } diff --git a/asciidtype/asciidtype/src/umath.h b/asciidtype/asciidtype/src/umath.h index 616364be..0a7e5e92 100644 --- a/asciidtype/asciidtype/src/umath.h +++ b/asciidtype/asciidtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -PyObject * +int init_ufuncs(void); #endif /*_NPY_UFUNC_H */ diff --git a/metadatadtype/metadatadtype/src/casts.c b/metadatadtype/metadatadtype/src/casts.c index 917097c5..58b119f0 100644 --- a/metadatadtype/metadatadtype/src/casts.c +++ b/metadatadtype/metadatadtype/src/casts.c @@ -1,9 +1,11 @@ #include #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" diff --git a/metadatadtype/metadatadtype/src/dtype.c b/metadatadtype/metadatadtype/src/dtype.c index b20f751b..c7b2d617 100644 --- a/metadatadtype/metadatadtype/src/dtype.c +++ b/metadatadtype/metadatadtype/src/dtype.c @@ -1,7 +1,14 @@ +// 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" diff --git a/metadatadtype/metadatadtype/src/dtype.h b/metadatadtype/metadatadtype/src/dtype.h index da384ad1..c37765f4 100644 --- a/metadatadtype/metadatadtype/src/dtype.h +++ b/metadatadtype/metadatadtype/src/dtype.h @@ -1,11 +1,6 @@ #ifndef _NPY_DTYPE_H #define _NPY_DTYPE_H -// clang-format off -#include -#include "structmember.h" -// clang-format on - typedef struct { PyArray_Descr base; PyObject *metadata; diff --git a/metadatadtype/metadatadtype/src/metadatadtype_main.c b/metadatadtype/metadatadtype/src/metadatadtype_main.c index 2aad1c4f..8dffe46b 100644 --- a/metadatadtype/metadatadtype/src/metadatadtype_main.c +++ b/metadatadtype/metadatadtype/src/metadatadtype_main.c @@ -1,9 +1,11 @@ #include #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 #include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" #include "numpy/dtype_api.h" #include "umath.h" @@ -20,6 +22,7 @@ PyMODINIT_FUNC PyInit__metadatadtype_main(void) { import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -47,7 +50,7 @@ PyInit__metadatadtype_main(void) goto error; } - if (init_ufuncs(m) == NULL) { + if (init_ufuncs() == -1) { goto error; } diff --git a/metadatadtype/metadatadtype/src/umath.c b/metadatadtype/metadatadtype/src/umath.c index fc256074..60a1ee7e 100644 --- a/metadatadtype/metadatadtype/src/umath.c +++ b/metadatadtype/metadatadtype/src/umath.c @@ -1,9 +1,11 @@ #include #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/ndarraytypes.h" #include "numpy/arrayobject.h" #include "numpy/ufuncobject.h" @@ -103,11 +105,9 @@ add_wrapping_loop(const char *ufunc_name, PyArray_DTypeMeta **dtypes, return res; } -PyObject * -init_ufuncs(PyObject *module) +int +init_ufuncs(void) { - import_umath(); - PyArray_DTypeMeta *binary_orig_dtypes[3] = {&MetadataDType, &MetadataDType, &MetadataDType}; PyArray_DTypeMeta *binary_wrapped_dtypes[3] = { @@ -126,9 +126,8 @@ init_ufuncs(PyObject *module) goto error; } - return module; + return 0; error: - Py_DECREF(module); - return NULL; + return -1; } diff --git a/metadatadtype/metadatadtype/src/umath.h b/metadatadtype/metadatadtype/src/umath.h index aee62841..0a7e5e92 100644 --- a/metadatadtype/metadatadtype/src/umath.h +++ b/metadatadtype/metadatadtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -PyObject * -init_ufuncs(PyObject *module); +int +init_ufuncs(void); #endif /*_NPY_UFUNC_H */ diff --git a/mpfdtype/mpfdtype/src/casts.cpp b/mpfdtype/mpfdtype/src/casts.cpp index 6a5aef44..e29c6f9a 100644 --- a/mpfdtype/mpfdtype/src/casts.cpp +++ b/mpfdtype/mpfdtype/src/casts.cpp @@ -1,8 +1,10 @@ #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API +#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 diff --git a/mpfdtype/mpfdtype/src/dtype.c b/mpfdtype/mpfdtype/src/dtype.c index f4e7b1a5..9877d342 100644 --- a/mpfdtype/mpfdtype/src/dtype.c +++ b/mpfdtype/mpfdtype/src/dtype.c @@ -1,9 +1,11 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API +#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/dtype_api.h" diff --git a/mpfdtype/mpfdtype/src/mpfdtype_main.c b/mpfdtype/mpfdtype/src/mpfdtype_main.c index b3452ba4..9b921a21 100644 --- a/mpfdtype/mpfdtype/src/mpfdtype_main.c +++ b/mpfdtype/mpfdtype/src/mpfdtype_main.c @@ -1,8 +1,10 @@ #include #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API #define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION #include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" #include "numpy/dtype_api.h" #include "dtype.h" @@ -19,9 +21,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__mpfdtype_main(void) { - if (_import_array() < 0) { - return NULL; - } + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -44,14 +45,10 @@ PyInit__mpfdtype_main(void) goto error; } - PyObject *numpy = init_mpf_umath(); - - if (numpy == NULL) { + if (init_mpf_umath() == -1) { goto error; } - Py_DECREF(numpy); - if (init_terrible_hacks() < 0) { goto error; } diff --git a/mpfdtype/mpfdtype/src/umath.cpp b/mpfdtype/mpfdtype/src/umath.cpp index d6d98086..65ee93c1 100644 --- a/mpfdtype/mpfdtype/src/umath.cpp +++ b/mpfdtype/mpfdtype/src/umath.cpp @@ -1,9 +1,11 @@ #include "scalar.h" #define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API +#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 @@ -592,17 +594,15 @@ init_comps(PyObject *numpy) /* * Function that adds our multiply loop to NumPy's multiply ufunc. */ -PyObject * +int init_mpf_umath(void) { - import_umath(); - /* * Get the multiply ufunc: */ PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return NULL; + return -1; } if (init_unary_ops(numpy) < 0) { @@ -615,10 +615,11 @@ init_mpf_umath(void) goto err; } - return numpy; + Py_DECREF(numpy); + return 0; err: Py_DECREF(numpy); - return NULL; + return -1; } diff --git a/mpfdtype/mpfdtype/src/umath.h b/mpfdtype/mpfdtype/src/umath.h index 3726c537..7db1b69e 100644 --- a/mpfdtype/mpfdtype/src/umath.h +++ b/mpfdtype/mpfdtype/src/umath.h @@ -5,7 +5,7 @@ extern "C" { #endif -PyObject * +int init_mpf_umath(void); #ifdef __cplusplus diff --git a/quaddtype/quaddtype/src/quaddtype_main.c b/quaddtype/quaddtype/src/quaddtype_main.c index b9ccc035..abddf6d4 100644 --- a/quaddtype/quaddtype/src/quaddtype_main.c +++ b/quaddtype/quaddtype/src/quaddtype_main.c @@ -50,15 +50,11 @@ PyInit__quaddtype_main(void) goto error; } - PyObject *numpy = init_multiply_ufunc(); - - if (numpy == NULL) { + if (init_multiply_ufunc() == -1) { PyErr_SetString(PyExc_TypeError, "Failed to initialize the quadscalar multiply ufunc."); goto error; } - Py_DECREF(numpy); - return m; error: diff --git a/quaddtype/quaddtype/src/umath.c b/quaddtype/quaddtype/src/umath.c index 6d519770..302c3d2e 100644 --- a/quaddtype/quaddtype/src/umath.c +++ b/quaddtype/quaddtype/src/umath.c @@ -73,22 +73,20 @@ quad_multiply_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *dtypes[], } // Function that adds our multiply loop to NumPy's multiply ufunc. -PyObject* +int init_multiply_ufunc(void) { - import_umath(); - // Get the multiply ufunc: PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return NULL; + return -1; } PyObject *multiply = PyObject_GetAttrString(numpy, "multiply"); + Py_DECREF(numpy); if (multiply == NULL) { - Py_DECREF(numpy); - return NULL; + return -1; } // The initializing "wrap up" code from the slides (plus one error check) @@ -116,10 +114,9 @@ init_multiply_ufunc(void) /* Register */ if (PyUFunc_AddLoopFromSpec(multiply, &MultiplySpec) < 0) { Py_DECREF(multiply); - Py_DECREF(numpy); - return NULL; + return -1; } Py_DECREF(multiply); - return numpy; + return 0; } diff --git a/quaddtype/quaddtype/src/umath.h b/quaddtype/quaddtype/src/umath.h index aa458f61..b883f57a 100644 --- a/quaddtype/quaddtype/src/umath.h +++ b/quaddtype/quaddtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -PyObject * +int init_multiply_ufunc(void); #endif /*_NPY_UFUNC_H */ diff --git a/stringdtype/stringdtype/src/casts.c b/stringdtype/stringdtype/src/casts.c index e5e1da4a..58db76f3 100644 --- a/stringdtype/stringdtype/src/casts.c +++ b/stringdtype/stringdtype/src/casts.c @@ -1,8 +1,10 @@ #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" diff --git a/stringdtype/stringdtype/src/casts.h b/stringdtype/stringdtype/src/casts.h index c95cd3b3..07e25d1f 100644 --- a/stringdtype/stringdtype/src/casts.h +++ b/stringdtype/stringdtype/src/casts.h @@ -1,17 +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/ndarraytypes.h" -#include "numpy/dtype_api.h" - PyArrayMethod_Spec ** get_casts(); diff --git a/stringdtype/stringdtype/src/dtype.c b/stringdtype/stringdtype/src/dtype.c index c080b3b4..23d50961 100644 --- a/stringdtype/stringdtype/src/dtype.c +++ b/stringdtype/stringdtype/src/dtype.c @@ -1,8 +1,10 @@ #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" diff --git a/stringdtype/stringdtype/src/main.c b/stringdtype/stringdtype/src/main.c index 097f3542..07b6bdc4 100644 --- a/stringdtype/stringdtype/src/main.c +++ b/stringdtype/stringdtype/src/main.c @@ -1,6 +1,7 @@ #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" @@ -95,6 +96,7 @@ PyMODINIT_FUNC PyInit__main(void) { import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -125,14 +127,10 @@ PyInit__main(void) goto error; } - PyObject *numpy = init_ufuncs(); - - if (numpy == NULL) { + if (init_ufuncs() == -1) { goto error; } - Py_DECREF(numpy); - return m; error: diff --git a/stringdtype/stringdtype/src/umath.c b/stringdtype/stringdtype/src/umath.c index 4136bdae..8b2670ef 100644 --- a/stringdtype/stringdtype/src/umath.c +++ b/stringdtype/stringdtype/src/umath.c @@ -1,8 +1,10 @@ #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" @@ -1287,16 +1289,14 @@ add_promoter(PyObject *numpy, const char *ufunc_name, goto error; \ } -PyObject * +int init_ufuncs(void) { PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return NULL; + return -1; } - import_umath(); - static char *comparison_ufunc_names[6] = {"equal", "not_equal", "greater", "greater_equal", "less", "less_equal"}; @@ -1421,9 +1421,10 @@ init_ufuncs(void) } } - return numpy; + Py_DECREF(numpy); + return 0; error: Py_DECREF(numpy); - return NULL; + return -1; } diff --git a/stringdtype/stringdtype/src/umath.h b/stringdtype/stringdtype/src/umath.h index 616364be..0a7e5e92 100644 --- a/stringdtype/stringdtype/src/umath.h +++ b/stringdtype/stringdtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -PyObject * +int init_ufuncs(void); #endif /*_NPY_UFUNC_H */ diff --git a/unytdtype/unytdtype/src/casts.c b/unytdtype/unytdtype/src/casts.c index d6e98f46..be0060c7 100644 --- a/unytdtype/unytdtype/src/casts.c +++ b/unytdtype/unytdtype/src/casts.c @@ -3,9 +3,11 @@ #include #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" diff --git a/unytdtype/unytdtype/src/dtype.c b/unytdtype/unytdtype/src/dtype.c index 93a2cc8d..6049e053 100644 --- a/unytdtype/unytdtype/src/dtype.c +++ b/unytdtype/unytdtype/src/dtype.c @@ -4,9 +4,11 @@ // 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" diff --git a/unytdtype/unytdtype/src/umath.c b/unytdtype/unytdtype/src/umath.c index 7a4ed4d8..4f5c1ffb 100644 --- a/unytdtype/unytdtype/src/umath.c +++ b/unytdtype/unytdtype/src/umath.c @@ -1,9 +1,11 @@ #include #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" @@ -65,22 +67,20 @@ unit_multiply_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *dtypes[], /* * Function that adds our multiply loop to NumPy's multiply ufunc. */ -PyObject * +int init_multiply_ufunc(void) { - import_umath(); - /* * Get the multiply ufunc: */ PyObject *numpy = PyImport_ImportModule("numpy"); if (numpy == NULL) { - return NULL; + return -1; } PyObject *multiply = PyObject_GetAttrString(numpy, "multiply"); + Py_DECREF(numpy); if (multiply == NULL) { - Py_DECREF(numpy); - return NULL; + return -1; } /* @@ -106,9 +106,8 @@ init_multiply_ufunc(void) /* Register */ if (PyUFunc_AddLoopFromSpec(multiply, &MultiplySpec) < 0) { Py_DECREF(multiply); - Py_DECREF(numpy); - return NULL; + return -1; } Py_DECREF(multiply); - return numpy; + return 0; } diff --git a/unytdtype/unytdtype/src/umath.h b/unytdtype/unytdtype/src/umath.h index aa458f61..b883f57a 100644 --- a/unytdtype/unytdtype/src/umath.h +++ b/unytdtype/unytdtype/src/umath.h @@ -1,7 +1,7 @@ #ifndef _NPY_UFUNC_H #define _NPY_UFUNC_H -PyObject * +int init_multiply_ufunc(void); #endif /*_NPY_UFUNC_H */ diff --git a/unytdtype/unytdtype/src/unytdtype_main.c b/unytdtype/unytdtype/src/unytdtype_main.c index 71b66d70..75627f4a 100644 --- a/unytdtype/unytdtype/src/unytdtype_main.c +++ b/unytdtype/unytdtype/src/unytdtype_main.c @@ -1,8 +1,10 @@ #include #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 #include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" #include "numpy/dtype_api.h" #include "dtype.h" @@ -19,6 +21,7 @@ PyMODINIT_FUNC PyInit__unytdtype_main(void) { import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { @@ -45,14 +48,10 @@ PyInit__unytdtype_main(void) goto error; } - PyObject *numpy = init_multiply_ufunc(); - - if (numpy == NULL) { + if (init_multiply_ufunc() == -1) { goto error; } - Py_DECREF(numpy); - return m; error: From e2594698f64c700e6dc3d2dbc23aaa8018314b3d Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 12:53:44 -0600 Subject: [PATCH 10/11] fix CI build error --- asciidtype/asciidtype/src/casts.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/asciidtype/asciidtype/src/casts.c b/asciidtype/asciidtype/src/casts.c index 3b8a4949..54c01d25 100644 --- a/asciidtype/asciidtype/src/casts.c +++ b/asciidtype/asciidtype/src/casts.c @@ -49,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; @@ -114,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; @@ -165,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; From 14b5343f6bf0dee3d0029f30e7bea34d18857d9f Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 16 Jul 2024 12:57:10 -0600 Subject: [PATCH 11/11] Fix migration I missed in quaddtype --- quaddtype/quaddtype/src/casts.c | 2 ++ quaddtype/quaddtype/src/dtype.c | 2 ++ quaddtype/quaddtype/src/quaddtype_main.c | 6 ++++-- quaddtype/quaddtype/src/umath.c | 2 ++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/quaddtype/quaddtype/src/casts.c b/quaddtype/quaddtype/src/casts.c index fb3fe28e..999344f5 100644 --- a/quaddtype/quaddtype/src/casts.c +++ b/quaddtype/quaddtype/src/casts.c @@ -1,9 +1,11 @@ #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" diff --git a/quaddtype/quaddtype/src/dtype.c b/quaddtype/quaddtype/src/dtype.c index 9722c5e7..63a6840e 100644 --- a/quaddtype/quaddtype/src/dtype.c +++ b/quaddtype/quaddtype/src/dtype.c @@ -1,9 +1,11 @@ #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" diff --git a/quaddtype/quaddtype/src/quaddtype_main.c b/quaddtype/quaddtype/src/quaddtype_main.c index abddf6d4..d9adeff5 100644 --- a/quaddtype/quaddtype/src/quaddtype_main.c +++ b/quaddtype/quaddtype/src/quaddtype_main.c @@ -1,8 +1,10 @@ #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 #include "numpy/arrayobject.h" +#include "numpy/ufuncobject.h" #include "numpy/dtype_api.h" #include "dtype.h" @@ -19,8 +21,8 @@ static struct PyModuleDef moduledef = { PyMODINIT_FUNC PyInit__quaddtype_main(void) { - if (import_array() < 0) - return NULL; + import_array(); + import_umath(); PyObject *m = PyModule_Create(&moduledef); if (m == NULL) { diff --git a/quaddtype/quaddtype/src/umath.c b/quaddtype/quaddtype/src/umath.c index 302c3d2e..6c9fece6 100644 --- a/quaddtype/quaddtype/src/umath.c +++ b/quaddtype/quaddtype/src/umath.c @@ -1,9 +1,11 @@ #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/ufuncobject.h"