2323 * SOFTWARE.
2424 */
2525
26- #define PY_SSIZE_T_CLEAN
27- #define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
28- #define NPY_TARGET_VERSION NPY_2_0_API_VERSION
2926#define TSK_BUG_ASSERT_MESSAGE \
3027 "Please open an issue on" \
3128 " GitHub, ideally with a reproducible example." \
3229 " (https://github.com/tskit-dev/tskit/issues)"
3330
31+ #define PY_SSIZE_T_CLEAN
3432#include <Python.h>
35- #include <structmember.h>
33+ #include <numpy/numpyconfig.h>
34+
35+ #if defined(NPY_2_0_API_VERSION ) && NPY_API_VERSION >= NPY_2_0_API_VERSION
36+ #define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
37+ #undef NPY_FEATURE_VERSION
38+ #define NPY_FEATURE_VERSION NPY_2_0_API_VERSION
39+ #define HAVE_NUMPY_2 1
40+ #else
41+ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
42+ #define HAVE_NUMPY_2 0
43+ #endif
3644#include <numpy/arrayobject.h>
45+
46+ #include <structmember.h>
3747#include <float.h>
3848
3949#include "kastore.h"
@@ -10775,6 +10785,7 @@ TreeSequence_make_array(TreeSequence *self, tsk_size_t size, int dtype, void *da
1077510785 return make_owned_array ((PyObject * ) self , size , dtype , data );
1077610786}
1077710787
10788+ #if HAVE_NUMPY_2
1077810789PyObject *
1077910790TreeSequence_decode_ragged_string_column (
1078010791 TreeSequence * self , tsk_size_t num_rows , const char * data , const tsk_size_t * offset )
@@ -10825,6 +10836,7 @@ TreeSequence_decode_ragged_string_column(
1082510836 Py_XDECREF (array );
1082610837 return ret ;
1082710838}
10839+ #endif
1082810840
1082910841static PyObject *
1083010842TreeSequence_get_individuals_flags (TreeSequence * self , void * closure )
@@ -11073,6 +11085,7 @@ TreeSequence_get_sites_position(TreeSequence *self, void *closure)
1107311085 return ret ;
1107411086}
1107511087
11088+ #if HAVE_NUMPY_2
1107611089static PyObject *
1107711090TreeSequence_get_sites_ancestral_state (TreeSequence * self , void * closure )
1107811091{
@@ -11088,6 +11101,7 @@ TreeSequence_get_sites_ancestral_state(TreeSequence *self, void *closure)
1108811101out :
1108911102 return ret ;
1109011103}
11104+ #endif
1109111105
1109211106static PyObject *
1109311107TreeSequence_get_sites_metadata (TreeSequence * self , void * closure )
@@ -11755,9 +11769,11 @@ static PyGetSetDef TreeSequence_getsetters[] = {
1175511769 { .name = "sites_position" ,
1175611770 .get = (getter ) TreeSequence_get_sites_position ,
1175711771 .doc = "The site position array" },
11772+ #if HAVE_NUMPY_2
1175811773 { .name = "sites_ancestral_state" ,
1175911774 .get = (getter ) TreeSequence_get_sites_ancestral_state ,
1176011775 .doc = "The site ancestral state array" },
11776+ #endif
1176111777 { .name = "sites_metadata" ,
1176211778 .get = (getter ) TreeSequence_get_sites_metadata ,
1176311779 .doc = "The site metadata array" },
@@ -14647,15 +14663,23 @@ PyInit__tskit(void)
1464714663{
1464814664 PyObject * module ;
1464914665
14666+ #if HAVE_NUMPY_2
1465014667 if (PyArray_ImportNumPyAPI () < 0 ) {
1465114668 return NULL ;
1465214669 }
14670+ #else
14671+ import_array ();
14672+ #endif
1465314673
1465414674 module = PyModule_Create (& tskitmodule );
1465514675 if (module == NULL ) {
1465614676 return NULL ;
1465714677 }
1465814678
14679+ if (PyModule_AddIntConstant (module , "HAS_NUMPY_2" , HAVE_NUMPY_2 )) {
14680+ return NULL ;
14681+ }
14682+
1465914683 if (register_lwt_class (module ) != 0 ) {
1466014684 return NULL ;
1466114685 }
0 commit comments