diff --git a/CMakeLists.txt b/CMakeLists.txt index 43d10098f3d446202f12221184bd1d5c70722c33..908bd0852e9e8cd9e36407e0183330bca01e2327 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ SET(${PROJECT_NAME}_HEADERS include/eigenpy/geometry-conversion.hpp include/eigenpy/memory.hpp include/eigenpy/numpy-type.hpp + include/eigenpy/numpy.hpp include/eigenpy/registration.hpp include/eigenpy/angle-axis.hpp include/eigenpy/quaternion.hpp @@ -141,6 +142,7 @@ SET(${PROJECT_NAME}_SOURCES ${${PROJECT_NAME}_DECOMPOSITIONS_SOURCES} src/exception.cpp src/eigenpy.cpp + src/numpy.cpp src/matrix-float.cpp src/matrix-complex-float.cpp src/matrix-complex-double.cpp diff --git a/include/eigenpy/details.hpp b/include/eigenpy/details.hpp index 33501dbd1cc4023885c93cae8c909c3126c7dbdc..06a29905eab4e4f312bf27f36b31c13591d00bb4 100644 --- a/include/eigenpy/details.hpp +++ b/include/eigenpy/details.hpp @@ -584,7 +584,6 @@ namespace eigenpy template<typename MatType> void enableEigenPySpecific() { - numpy_import_array(); if(check_registration<MatType>()) return; bp::to_python_converter<MatType,EigenToPy<MatType> >(); diff --git a/include/eigenpy/fwd.hpp b/include/eigenpy/fwd.hpp index 702b7d435254205003bf797445bf223b632d9f77..629d9276a96f456927155e4148a460b17227be66 100644 --- a/include/eigenpy/fwd.hpp +++ b/include/eigenpy/fwd.hpp @@ -11,12 +11,9 @@ #include <boost/python.hpp> #include <Eigen/Core> -#include <numpy/numpyconfig.h> -#ifdef NPY_1_8_API_VERSION -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#endif - -#include <numpy/noprefix.h> +#define NO_IMPORT_ARRAY + #include "eigenpy/numpy.hpp" +#undef NO_IMPORT_ARRAY #ifdef NPY_ALIGNED #if EIGEN_VERSION_AT_LEAST(3,2,90) @@ -31,4 +28,3 @@ #include "eigenpy/expose.hpp" #endif // ifndef __eigenpy_fwd_hpp__ - diff --git a/include/eigenpy/map.hpp b/include/eigenpy/map.hpp index db5ffc89ffb85376d8df9b9a72d68416b211131a..487506cd0116e01b23fa622a223e737c8281dac0 100644 --- a/include/eigenpy/map.hpp +++ b/include/eigenpy/map.hpp @@ -7,7 +7,6 @@ #define __eigenpy_map_hpp__ #include "eigenpy/fwd.hpp" -#include <numpy/arrayobject.h> #include "eigenpy/exception.hpp" #include "eigenpy/stride.hpp" diff --git a/include/eigenpy/numpy-type.hpp b/include/eigenpy/numpy-type.hpp index 2037e0f2bb800f0b3df2b1fbd2ce9a06d87f0eeb..df972cf9ee93bb30c643fc1d82e98f98b58604cf 100644 --- a/include/eigenpy/numpy-type.hpp +++ b/include/eigenpy/numpy-type.hpp @@ -145,45 +145,6 @@ namespace eigenpy NP_TYPE np_type; }; - - namespace details - { - struct import_numpy - { - - static bool status() - { - return instance().imported; - } - - protected: - - static import_numpy & instance() - { - static import_numpy m_instance; - return m_instance; - } - - import_numpy() - : imported(false) - { - if(_import_array() < 0) - { - PyErr_Print(); - PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); - } - else - imported = true; - } - - bool imported; - }; - } // namespace details - - inline bool numpy_import_array() - { - return details::import_numpy::status(); - } } #endif // ifndef __eigenpy_numpy_type_hpp__ diff --git a/include/eigenpy/numpy.hpp b/include/eigenpy/numpy.hpp new file mode 100644 index 0000000000000000000000000000000000000000..466cf263f3ded24ed959232f78d5c7356906329f --- /dev/null +++ b/include/eigenpy/numpy.hpp @@ -0,0 +1,24 @@ +/* + * Copyright 2020 INRIA + */ + +#ifndef __eigenpy_numpy_hpp__ +#define __eigenpy_numpy_hpp__ + +#ifndef PY_ARRAY_UNIQUE_SYMBOL + #define PY_ARRAY_UNIQUE_SYMBOL EIGENPY_ARRAY_API +#endif + +#include <numpy/numpyconfig.h> +#ifdef NPY_1_8_API_VERSION + #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#endif + +#include <numpy/noprefix.h> + +namespace eigenpy +{ + void import_numpy(); +} + +#endif // ifndef __eigenpy_numpy_hpp__ diff --git a/src/eigenpy.cpp b/src/eigenpy.cpp index 85d6f5b18687841307f5bf017761b504e7aebd68..c2f49067d105f24fb919f7a5cfa0c510e0b9e58f 100644 --- a/src/eigenpy.cpp +++ b/src/eigenpy.cpp @@ -28,6 +28,7 @@ namespace eigenpy void enableEigenPy() { using namespace Eigen; + import_numpy(); Exception::registerException(); diff --git a/src/numpy.cpp b/src/numpy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c76caef62ce2d8e7a14f5a2c7a06f14f081e4b0 --- /dev/null +++ b/src/numpy.cpp @@ -0,0 +1,18 @@ +/* + * Copyright 2020 INRIA + */ + +#include "eigenpy/numpy.hpp" + +namespace eigenpy +{ + void import_numpy() + { + if(_import_array() < 0) + { + PyErr_Print(); + PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); + } + // std::cout << "init _import_array " << std::endl; + } +}