From: Anthony Geay Date: Thu, 17 Oct 2024 15:26:43 +0000 (+0200) Subject: [EDF31196] : Porting for numpy2 compatibility X-Git-Tag: V9_14_0a1~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fagy%2F31196_numpy_2;p=tools%2Fmedcoupling.git [EDF31196] : Porting for numpy2 compatibility --- diff --git a/CMakeLists.txt b/CMakeLists.txt index f817bca18..f2edb8383 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -348,6 +348,10 @@ ENDIF(WIN32) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) +IF(MEDCOUPLING_ENABLE_PYTHON AND NUMPY_FOUND) + ADD_DEFINITIONS( "-DNPY_NO_DEPRECATED_API" ) +ENDIF(MEDCOUPLING_ENABLE_PYTHON AND NUMPY_FOUND) + ADD_SUBDIRECTORY(src) IF(MEDCOUPLING_BUILD_DOC) diff --git a/src/MEDCoupling_Swig/CMakeLists.txt b/src/MEDCoupling_Swig/CMakeLists.txt index 7474395ca..1e26d4dcd 100644 --- a/src/MEDCoupling_Swig/CMakeLists.txt +++ b/src/MEDCoupling_Swig/CMakeLists.txt @@ -72,8 +72,8 @@ SET (MC_Swig_interf ) INCLUDE_DIRECTORIES( - ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} + ${PYTHON_INCLUDE_DIRS} ${PTHREAD_INCLUDE_DIR} # pthread dependency due to python2.7 library ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} @@ -133,15 +133,15 @@ SET_SOURCE_FILES_PROPERTIES(MEDCouplingRemapper.i PROPERTIES CPLUSPLUS ON) IF(WIN32) SET_PROPERTY(SOURCE MEDCouplingRemapper.i PROPERTY COMPILE_DEFINITIONS WIN32) ENDIF() -IF ("${PYTHON_VERSION_MAJOR}" STREQUAL "3") - SET_SOURCE_FILES_PROPERTIES(MEDCouplingRemapper.i PROPERTIES SWIG_FLAGS "-py3") -ELSE() - SET_SOURCE_FILES_PROPERTIES(MEDCouplingRemapper.i PROPERTIES SWIG_DEFINITIONS "-shadow") -ENDIF() -SET(SWIG_MODULE_MEDCouplingRemapper_EXTRA_FLAGS "${NUMPY_DEFINITIONS};${SCIPY_DEFINITIONS}") -IF(MEDCOUPLING_USE_64BIT_IDS) - STRING(APPEND SWIG_MODULE_MEDCouplingRemapper_EXTRA_FLAGS ";-DMEDCOUPLING_USE_64BIT_IDS") -ENDIF(MEDCOUPLING_USE_64BIT_IDS) +#IF ("${PYTHON_VERSION_MAJOR}" STREQUAL "3") +# SET_SOURCE_FILES_PROPERTIES(MEDCouplingRemapper.i PROPERTIES SWIG_FLAGS "-py3") +#ELSE() +# SET_SOURCE_FILES_PROPERTIES(MEDCouplingRemapper.i PROPERTIES SWIG_DEFINITIONS "-shadow") +#ENDIF() +#SET(SWIG_MODULE_MEDCouplingRemapper_EXTRA_FLAGS "${NUMPY_DEFINITIONS};${SCIPY_DEFINITIONS}") +#IF(MEDCOUPLING_USE_64BIT_IDS) +# STRING(APPEND SWIG_MODULE_MEDCouplingRemapper_EXTRA_FLAGS ";-DMEDCOUPLING_USE_64BIT_IDS") +#ENDIF(MEDCOUPLING_USE_64BIT_IDS) # _ABR_ Ensure dependency mechanism on all SWIG files and headers SET (SWIG_MODULE_MEDCouplingRemapper_EXTRA_DEPS ${MEDCoupling_SWIG_DPYS_FILES} @@ -149,26 +149,27 @@ SET (SWIG_MODULE_MEDCouplingRemapper_EXTRA_DEPS ${MEDCoupling_SWIG_DPYS_FILES} ${interpkernel_HEADERS_HXX} ${interpkernel_HEADERS_TXX}) -IF(${CMAKE_VERSION} VERSION_LESS "3.8.0") - SWIG_ADD_MODULE(MEDCouplingRemapper python MEDCouplingRemapper.i) -ELSE() - SWIG_ADD_LIBRARY(MEDCouplingRemapper LANGUAGE python SOURCES MEDCouplingRemapper.i) -ENDIF() +#IF(${CMAKE_VERSION} VERSION_LESS "3.8.0") +# SWIG_ADD_MODULE(MEDCouplingRemapper python MEDCouplingRemapper.i) +#ELSE() +# SWIG_ADD_LIBRARY(MEDCouplingRemapper LANGUAGE python SOURCES MEDCouplingRemapper.i) +#ENDIF() -IF(WIN32 OR CYGWIN) - SWIG_LINK_LIBRARIES(MEDCouplingRemapper ${PYTHON_LIBRARIES} medcouplingremapper) -ELSE(WIN32 OR CYGWIN) - # ${PYTHON_LIBRARIES} not needed see https://www.python.org/dev/peps/pep-0513/#libpythonx-y-so-1 - SWIG_LINK_LIBRARIES(MEDCouplingRemapper medcouplingremapper) -ENDIF(WIN32 OR CYGWIN) +#IF(WIN32 OR CYGWIN) +# SWIG_LINK_LIBRARIES(MEDCouplingRemapper ${PYTHON_LIBRARIES} medcouplingremapper) +#ELSE(WIN32 OR CYGWIN) +# # ${PYTHON_LIBRARIES} not needed see https://www.python.org/dev/peps/pep-0513/#libpythonx-y-so-1 +# SWIG_LINK_LIBRARIES(MEDCouplingRemapper medcouplingremapper) +#ENDIF(WIN32 OR CYGWIN) IF(WIN32) SET_TARGET_PROPERTIES(_MEDCouplingRemapper PROPERTIES DEBUG_OUTPUT_NAME _MEDCouplingRemapper_d) SET_TARGET_PROPERTIES(_${MEDCouling_target_name} PROPERTIES DEBUG_OUTPUT_NAME _${MEDCouling_target_name}_d) ENDIF(WIN32) -INSTALL(TARGETS ${SWIG_MODULE_${MEDCouling_target_name}_REAL_NAME} ${SWIG_MODULE_MEDCouplingRemapper_REAL_NAME} DESTINATION ${MEDCOUPLING_INSTALL_PYTHON}) +INSTALL(TARGETS ${SWIG_MODULE_${MEDCouling_target_name}_REAL_NAME} DESTINATION ${MEDCOUPLING_INSTALL_PYTHON}) +#INSTALL(TARGETS ${SWIG_MODULE_${MEDCouling_target_name}_REAL_NAME} ${SWIG_MODULE_MEDCouplingRemapper_REAL_NAME} DESTINATION ${MEDCOUPLING_INSTALL_PYTHON}) -SET(PYFILES_TO_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/MEDCouplingRemapper.py) +#SET(PYFILES_TO_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/MEDCouplingRemapper.py) IF(WIN32) SET(PYFILES_TO_INSTALL ${PYFILES_TO_INSTALL} ${CMAKE_CURRENT_BINARY_DIR}/MEDCouplingCompat.py) ELSE() diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index 9208a72f4..7bb28fb3c 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -159,8 +159,9 @@ void numarrdeal2(void *pt, void *obj) } template -MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype, const char *msg) +MCData *BuildNewInstance(PyObject *elt0Arr, int npyObjectType, PyTypeObject *pytype, const char *msg) { + PyArrayObject *elt0 = reinterpret_cast( elt0Arr );//PyObject_HEAD on top of struct tagPyArrayObject int ndim=PyArray_NDIM(elt0); if(ndim!=1 && ndim!=2) throw INTERP_KERNEL::Exception("Input numpy array should have dimension equal to 1 or 2 !"); @@ -175,7 +176,7 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype npy_intp sz0=PyArray_DIM(elt0,0); npy_intp sz1=ndim==2?PyArray_DIM(elt0,1):1; // - int itemSize=PyArray_ITEMSIZE(elt0); + auto itemSize=PyArray_ITEMSIZE(elt0); if(itemSize!=sizeof(T)) { std::ostringstream oss; oss << "Input numpy array has not itemSize set to " << sizeof(T) << " !"; @@ -194,7 +195,7 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & MED_NUMPY_OWNDATA)?elt0C:NULL; int mask=MED_NUMPY_OWNDATA; mask=~mask; elt0C->flags&=mask; - PyObject *deepestObj=elt0; + PyObject *deepestObj=elt0Arr; PyObject *base=elt0C->base; if(base) deepestObj=base; bool isSpetialCase(false); @@ -228,7 +229,7 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype PyCallBackDataArraySt *cb=PyObject_GC_New(PyCallBackDataArraySt,pytype); cb->_pt_mc=ret; ret->useArray(reinterpret_cast(data),true,MEDCoupling::DeallocType::C_DEALLOC,sz0,sz1); - PyObject *ref=PyWeakref_NewRef(deepestObj,(PyObject *)cb); + PyObject *ref=PyWeakref_NewRef(reinterpret_cast(deepestObj),(PyObject *)cb); void **objs=new void *[2]; objs[0]=cb; objs[1]=ref; mma.setParameterForDeallocator(objs); mma.setSpecificDeallocator(numarrdeal2); diff --git a/src/MEDLoader/Swig/CMakeLists.txt b/src/MEDLoader/Swig/CMakeLists.txt index 479afcf42..bc13f294c 100644 --- a/src/MEDLoader/Swig/CMakeLists.txt +++ b/src/MEDLoader/Swig/CMakeLists.txt @@ -43,8 +43,8 @@ SET (MEDLoader_SWIG_DPYS_FILES MEDLoaderTypemaps.i) INCLUDE_DIRECTORIES( - ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} + ${PYTHON_INCLUDE_DIRS} ${MEDFILE_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/src/MEDPartitioner_Swig/CMakeLists.txt b/src/MEDPartitioner_Swig/CMakeLists.txt index 696df4459..2227bace2 100644 --- a/src/MEDPartitioner_Swig/CMakeLists.txt +++ b/src/MEDPartitioner_Swig/CMakeLists.txt @@ -37,9 +37,9 @@ SET (MEDPartitioner_SWIG_DPYS_FILES MEDPartitionerCommon.i) INCLUDE_DIRECTORIES( + ${NUMPY_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} ${PTHREAD_INCLUDE_DIR} # pthread dependency due to python2.7 library - ${NUMPY_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${HDF5_INCLUDE_DIRS} diff --git a/src/ParaMEDMEM_Swig/CMakeLists.txt b/src/ParaMEDMEM_Swig/CMakeLists.txt index 4e7f502b4..e1fdb6f07 100644 --- a/src/ParaMEDMEM_Swig/CMakeLists.txt +++ b/src/ParaMEDMEM_Swig/CMakeLists.txt @@ -38,8 +38,8 @@ IF(MEDCOUPLING_USE_64BIT_IDS) ENDIF(MEDCOUPLING_USE_64BIT_IDS) INCLUDE_DIRECTORIES( - ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} + ${PYTHON_INCLUDE_DIRS} ${MPI_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM diff --git a/src/PyWrapping/CMakeLists.txt b/src/PyWrapping/CMakeLists.txt index e1b4e2f5a..9c8039d9a 100644 --- a/src/PyWrapping/CMakeLists.txt +++ b/src/PyWrapping/CMakeLists.txt @@ -40,8 +40,8 @@ ENDIF(MEDCOUPLING_USE_64BIT_IDS) SET(medcoupling_SWIG_DPYS_FILES medcoupling.i) INCLUDE_DIRECTORIES( - ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} + ${PYTHON_INCLUDE_DIRS} ${MEDFILE_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/src/RENUMBER_Swig/CMakeLists.txt b/src/RENUMBER_Swig/CMakeLists.txt index 462bcb348..a927ed365 100644 --- a/src/RENUMBER_Swig/CMakeLists.txt +++ b/src/RENUMBER_Swig/CMakeLists.txt @@ -41,10 +41,10 @@ SET (MEDRenumber_SWIG_DPYS_FILES MEDRenumberImpl.i) INCLUDE_DIRECTORIES( + ${NUMPY_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} ${PTHREAD_INCLUDE_DIR} # pthread dependency due to python2.7 library ${CMAKE_CURRENT_SOURCE_DIR} - ${NUMPY_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../RENUMBER ${CMAKE_CURRENT_SOURCE_DIR}/../MEDCoupling_Swig