2424 */
2525
2626#define PY_SSIZE_T_CLEAN
27+ #include <Python.h>
28+ #include <numpy/numpyconfig.h>
29+
30+ // Simple conditional based on constant existence
31+ #if defined(NPY_2_0_API_VERSION ) && NPY_API_VERSION >= NPY_2_0_API_VERSION
2732#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
28- #define NPY_TARGET_VERSION NPY_2_0_API_VERSION
29- #define TSK_BUG_ASSERT_MESSAGE \
30- "Please open an issue on" \
31- " GitHub, ideally with a reproducible example." \
32- " (https://github.com/tskit-dev/tskit/issues)"
33+ #undef NPY_FEATURE_VERSION
34+ #define NPY_FEATURE_VERSION NPY_2_0_API_VERSION
35+ #define HAVE_NUMPY_2 1
36+ #else
37+ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
38+ #define HAVE_NUMPY_2 0
39+ #endif
40+ #include <numpy/arrayobject.h>
3341
34- #include <Python.h>
3542#include <structmember.h>
36- #include <numpy/arrayobject.h>
3743#include <float.h>
3844
3945#include "kastore.h"
@@ -10775,6 +10781,7 @@ TreeSequence_make_array(TreeSequence *self, tsk_size_t size, int dtype, void *da
1077510781 return make_owned_array ((PyObject * ) self , size , dtype , data );
1077610782}
1077710783
10784+ #if HAVE_NUMPY_2
1077810785PyObject *
1077910786TreeSequence_decode_ragged_string_column (
1078010787 TreeSequence * self , tsk_size_t num_rows , const char * data , const tsk_size_t * offset )
@@ -10825,6 +10832,7 @@ TreeSequence_decode_ragged_string_column(
1082510832 Py_XDECREF (array );
1082610833 return ret ;
1082710834}
10835+ #endif
1082810836
1082910837static PyObject *
1083010838TreeSequence_get_individuals_flags (TreeSequence * self , void * closure )
@@ -11073,6 +11081,7 @@ TreeSequence_get_sites_position(TreeSequence *self, void *closure)
1107311081 return ret ;
1107411082}
1107511083
11084+ #if HAVE_NUMPY_2
1107611085static PyObject *
1107711086TreeSequence_get_sites_ancestral_state (TreeSequence * self , void * closure )
1107811087{
@@ -11088,6 +11097,7 @@ TreeSequence_get_sites_ancestral_state(TreeSequence *self, void *closure)
1108811097out :
1108911098 return ret ;
1109011099}
11100+ #endif
1109111101
1109211102static PyObject *
1109311103TreeSequence_get_sites_metadata (TreeSequence * self , void * closure )
@@ -11755,9 +11765,11 @@ static PyGetSetDef TreeSequence_getsetters[] = {
1175511765 { .name = "sites_position" ,
1175611766 .get = (getter ) TreeSequence_get_sites_position ,
1175711767 .doc = "The site position array" },
11768+ #if HAVE_NUMPY_2
1175811769 { .name = "sites_ancestral_state" ,
1175911770 .get = (getter ) TreeSequence_get_sites_ancestral_state ,
1176011771 .doc = "The site ancestral state array" },
11772+ #endif
1176111773 { .name = "sites_metadata" ,
1176211774 .get = (getter ) TreeSequence_get_sites_metadata ,
1176311775 .doc = "The site metadata array" },
@@ -14647,15 +14659,23 @@ PyInit__tskit(void)
1464714659{
1464814660 PyObject * module ;
1464914661
14662+ #if HAVE_NUMPY_2
1465014663 if (PyArray_ImportNumPyAPI () < 0 ) {
1465114664 return NULL ;
1465214665 }
14666+ #else
14667+ import_array ();
14668+ #endif
1465314669
1465414670 module = PyModule_Create (& tskitmodule );
1465514671 if (module == NULL ) {
1465614672 return NULL ;
1465714673 }
1465814674
14675+ if (PyModule_AddIntConstant (module , "HAS_NUMPY_2" , HAVE_NUMPY_2 )) {
14676+ return NULL ;
14677+ }
14678+
1465914679 if (register_lwt_class (module ) != 0 ) {
1466014680 return NULL ;
1466114681 }
0 commit comments