From 836ea5858b3d80f8bc5a4cf8530e272b3be37753 Mon Sep 17 00:00:00 2001
From: Justin Carpentier <justin.carpentier@inria.fr>
Date: Fri, 21 Feb 2020 14:41:10 +0100
Subject: [PATCH] core: import numpy only once

---
 CMakeLists.txt                 |  2 ++
 include/eigenpy/details.hpp    |  1 -
 include/eigenpy/fwd.hpp        | 10 +++------
 include/eigenpy/map.hpp        |  1 -
 include/eigenpy/numpy-type.hpp | 39 ----------------------------------
 include/eigenpy/numpy.hpp      | 24 +++++++++++++++++++++
 src/eigenpy.cpp                |  1 +
 src/numpy.cpp                  | 18 ++++++++++++++++
 8 files changed, 48 insertions(+), 48 deletions(-)
 create mode 100644 include/eigenpy/numpy.hpp
 create mode 100644 src/numpy.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 43d10098..908bd085 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 33501dbd..06a29905 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 702b7d43..629d9276 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 db5ffc89..487506cd 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 2037e0f2..df972cf9 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 00000000..466cf263
--- /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 85d6f5b1..c2f49067 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 00000000..4c76caef
--- /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;
+  }
+}
-- 
GitLab