Skip to content

Commit

Permalink
Merge pull request #97 from ngoldbaum/update-for-numpy-2.0
Browse files Browse the repository at this point in the history
Update for numpy 2.0
  • Loading branch information
ngoldbaum authored Jul 16, 2024
2 parents 1f2c42e + 14b5343 commit 70626c3
Show file tree
Hide file tree
Showing 47 changed files with 626 additions and 630 deletions.
19 changes: 9 additions & 10 deletions asciidtype/asciidtype/src/asciidtype_main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/experimental_dtype_api.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"
#include "umath.h"
Expand All @@ -18,13 +22,8 @@ static struct PyModuleDef moduledef = {
PyMODINIT_FUNC
PyInit__asciidtype_main(void)
{
if (_import_array() < 0) {
return NULL;
}

if (import_experimental_dtype_api(15) < 0) {
return NULL;
}
import_array();
import_umath();

PyObject *m = PyModule_Create(&moduledef);
if (m == NULL) {
Expand All @@ -51,7 +50,7 @@ PyInit__asciidtype_main(void)
goto error;
}

if (init_ufuncs() < 0) {
if (init_ufuncs() == -1) {
goto error;
}

Expand Down
13 changes: 8 additions & 5 deletions asciidtype/asciidtype/src/casts.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/experimental_dtype_api.h"
#include "numpy/dtype_api.h"
#include "numpy/ndarraytypes.h"

#include "casts.h"
Expand Down Expand Up @@ -46,7 +49,7 @@ ascii_to_ascii(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
PyArray_Descr *const *descrs = context->descriptors;
long in_size = ((ASCIIDTypeObject *)descrs[0])->size;
long out_size = ((ASCIIDTypeObject *)descrs[1])->size;
long copy_size;
Expand Down Expand Up @@ -111,7 +114,7 @@ unicode_to_ascii(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
PyArray_Descr *const *descrs = context->descriptors;
long in_size = (descrs[0]->elsize) / 4;
long out_size = ((ASCIIDTypeObject *)descrs[1])->size;
long copy_size;
Expand Down Expand Up @@ -162,7 +165,7 @@ ascii_to_unicode(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
PyArray_Descr *const *descrs = context->descriptors;
long in_size = ((ASCIIDTypeObject *)descrs[0])->size;
long out_size = (descrs[1]->elsize) / 4;
long copy_size;
Expand Down
9 changes: 0 additions & 9 deletions asciidtype/asciidtype/src/casts.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#ifndef _NPY_CASTS_H
#define _NPY_CASTS_H

#include <Python.h>

#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);

Expand Down
16 changes: 16 additions & 0 deletions asciidtype/asciidtype/src/dtype.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// clang-format off
#include <Python.h>
#include "structmember.h"
// clang-format on

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"

#include "casts.h"
Expand Down
12 changes: 0 additions & 12 deletions asciidtype/asciidtype/src/dtype.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
#ifndef _NPY_DTYPE_H
#define _NPY_DTYPE_H

// clang-format off
#include <Python.h>
#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;
Expand Down
14 changes: 8 additions & 6 deletions asciidtype/asciidtype/src/umath.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#include "numpy/arrayobject.h"
#include "numpy/experimental_dtype_api.h"
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"
#include "string.h"
Expand All @@ -17,7 +20,7 @@ ascii_add_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
PyArray_Descr *const *descrs = context->descriptors;
long in1_size = ((ASCIIDTypeObject *)descrs[0])->size;
long in2_size = ((ASCIIDTypeObject *)descrs[1])->size;
long out_size = ((ASCIIDTypeObject *)descrs[2])->size;
Expand Down Expand Up @@ -112,7 +115,7 @@ ascii_equal_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
PyArray_Descr *const *descrs = context->descriptors;
long in1_size = ((ASCIIDTypeObject *)descrs[0])->size;
long in2_size = ((ASCIIDTypeObject *)descrs[1])->size;

Expand Down Expand Up @@ -234,7 +237,6 @@ init_ufuncs(void)
goto error;
}

Py_DECREF(numpy);
return 0;

error:
Expand Down
14 changes: 14 additions & 0 deletions asciidtype/reinstall.sh
Original file line number Diff line number Diff line change
@@ -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'
11 changes: 7 additions & 4 deletions metadatadtype/metadatadtype/src/casts.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/experimental_dtype_api.h"
#include "numpy/dtype_api.h"
#include "numpy/ndarraytypes.h"

#include "casts.h"
Expand Down Expand Up @@ -186,7 +189,7 @@ static PyArray_DTypeMeta *m2m_dtypes[2] = {NULL, NULL};
static PyType_Slot m2m_slots[] = {
{NPY_METH_resolve_descriptors,
&metadata_to_metadata_resolve_descriptors},
{_NPY_METH_get_loop, &metadata_to_metadata_get_loop},
{NPY_METH_get_loop, &metadata_to_metadata_get_loop},
{0, NULL}};

PyArrayMethod_Spec MetadataToMetadataCastSpec = {
Expand All @@ -200,7 +203,7 @@ PyArrayMethod_Spec MetadataToMetadataCastSpec = {
};

static PyType_Slot m2f_slots[] = {
{_NPY_METH_get_loop, &metadata_to_float64_get_loop}, {0, NULL}};
{NPY_METH_get_loop, &metadata_to_float64_get_loop}, {0, NULL}};

static char *m2f_name = "cast_MetadataDType_to_Float64";

Expand Down
9 changes: 0 additions & 9 deletions metadatadtype/metadatadtype/src/casts.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#ifndef _NPY_CASTS_H
#define _NPY_CASTS_H

#include <Python.h>

#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);

Expand Down
20 changes: 18 additions & 2 deletions metadatadtype/metadatadtype/src/dtype.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// clang-format off
#include <Python.h>
#include "structmember.h"
// clang-format on

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/arrayobject.h"
#include "numpy/dtype_api.h"
#include "numpy/ndarraytypes.h"

#include "dtype.h"

#include "casts.h"
Expand Down Expand Up @@ -74,8 +89,9 @@ new_metadatadtype_instance(PyObject *metadata)
}
Py_INCREF(metadata);
new->metadata = metadata;
new->base.elsize = sizeof(double);
new->base.alignment = _Alignof(double); /* is there a better spelling? */
PyArray_Descr *base = (PyArray_Descr *)new;
base->elsize = sizeof(double);
base->alignment = _Alignof(double); /* is there a better spelling? */
/* do not support byte-order for now */

return new;
Expand Down
12 changes: 0 additions & 12 deletions metadatadtype/metadatadtype/src/dtype.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
#ifndef _NPY_DTYPE_H
#define _NPY_DTYPE_H

// clang-format off
#include <Python.h>
#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;
Expand Down
19 changes: 9 additions & 10 deletions metadatadtype/metadatadtype/src/metadatadtype_main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#include "numpy/arrayobject.h"
#include "numpy/experimental_dtype_api.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"
#include "umath.h"
#include "dtype.h"

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
Expand All @@ -18,12 +21,8 @@ static struct PyModuleDef moduledef = {
PyMODINIT_FUNC
PyInit__metadatadtype_main(void)
{
if (_import_array() < 0) {
return NULL;
}
if (import_experimental_dtype_api(15) < 0) {
return NULL;
}
import_array();
import_umath();

PyObject *m = PyModule_Create(&moduledef);
if (m == NULL) {
Expand Down Expand Up @@ -51,7 +50,7 @@ PyInit__metadatadtype_main(void)
goto error;
}

if (init_ufuncs() < 0) {
if (init_ufuncs() == -1) {
goto error;
}

Expand Down
18 changes: 11 additions & 7 deletions metadatadtype/metadatadtype/src/umath.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include <Python.h>

#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#include "numpy/arrayobject.h"
#include "numpy/experimental_dtype_api.h"
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"

#include "dtype.h"
#include "umath.h"

static int
translate_given_descrs(int nin, int nout,
PyArray_DTypeMeta *NPY_UNUSED(wrapped_dtypes[]),
PyArray_Descr *given_descrs[],
PyArray_DTypeMeta *const NPY_UNUSED(wrapped_dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *new_descrs[])
{
for (int i = 0; i < nin + nout; i++) {
Expand All @@ -35,8 +38,8 @@ translate_given_descrs(int nin, int nout,

static int
translate_loop_descrs(int nin, int NPY_UNUSED(nout),
PyArray_DTypeMeta *NPY_UNUSED(new_dtypes[]),
PyArray_Descr *given_descrs[],
PyArray_DTypeMeta *const NPY_UNUSED(new_dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *original_descrs[],
PyArray_Descr *loop_descrs[])
{
Expand Down Expand Up @@ -125,5 +128,6 @@ init_ufuncs(void)

return 0;
error:

return -1;
}
Loading

0 comments on commit 70626c3

Please sign in to comment.