From ace34375335b7d8a482df9a32221dee41da48471 Mon Sep 17 00:00:00 2001 From: Andrews Sobral <andrewssobral@gmail.com> Date: Sat, 26 Aug 2017 21:28:19 +0200 Subject: [PATCH] Some bug fixes for building python 3 wrapper. --- CMakeLists.txt | 13 ++++++++++++- wrapper_python/np_opencv_converter.cpp | 16 ++++++++++++---- wrapper_python/utils/conversion.cpp | 20 ++++++++++++++++++-- wrapper_python/utils/conversion.h | 7 ++++++- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64f8505..8b897d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,12 @@ elseif() endif() message(STATUS "BGSLIBRARY WITH PYTHON SUPPORT: ${BGS_PYTHON_SUPPORT}") +# cmake -D BGS_PYTHON_SUPPORT=ON -D BGS_PYTHON_VERSION=2 .. +if(NOT DEFINED BGS_PYTHON_VERSION) + set(BGS_PYTHON_VERSION 2) +endif() +message(STATUS "PYTHON VERSION: ${BGS_PYTHON_VERSION}") + if(UNIX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x") set(CMAKE_MACOSX_RPATH 1) @@ -82,7 +88,12 @@ if(BGS_PYTHON_SUPPORT) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) - find_package(Boost REQUIRED COMPONENTS python) + message(STATUS "SEARCHING FOR BOOST COMPONENT FOR PYTHON ${BGS_PYTHON_VERSION}") + if(BGS_PYTHON_VERSION EQUAL 2) + find_package(Boost REQUIRED COMPONENTS python) + else() + find_package(Boost REQUIRED COMPONENTS python3) + endif() find_package(PythonInterp REQUIRED) find_package(PythonLibs REQUIRED) diff --git a/wrapper_python/np_opencv_converter.cpp b/wrapper_python/np_opencv_converter.cpp index 6050f6b..6799b4c 100644 --- a/wrapper_python/np_opencv_converter.cpp +++ b/wrapper_python/np_opencv_converter.cpp @@ -19,10 +19,18 @@ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>. namespace fs { namespace python { // Static PyInit -static void py_init() { - Py_Initialize(); - import_array(); -} +#if PY_VERSION_HEX >= 0x03000000 + static int py_init() { + Py_Initialize(); + import_array(); + return NULL; + } +#else + static void py_init() { + Py_Initialize(); + import_array(); + } +#endif // Singleton init and export converters static bool export_type_conversions_once = false; diff --git a/wrapper_python/utils/conversion.cpp b/wrapper_python/utils/conversion.cpp index f979d35..00a8a1e 100644 --- a/wrapper_python/utils/conversion.cpp +++ b/wrapper_python/utils/conversion.cpp @@ -23,10 +23,18 @@ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>. * inside modules/python/src2 folder. */ +#if PY_VERSION_HEX >= 0x03000000 +static int init() +{ + import_array(); + return NULL; +} +#else static void init() { - import_array(); + import_array(); } +#endif static int failmsg(const char *fmt, ...) { @@ -204,10 +212,18 @@ NumpyAllocator g_numpyAllocator; NDArrayConverter::NDArrayConverter() { init(); } +#if PY_VERSION_HEX >= 0x03000000 +int NDArrayConverter::init() +{ + import_array(); + return NULL; +} +#else void NDArrayConverter::init() { - import_array(); + import_array(); } +#endif cv::Mat NDArrayConverter::toMat(const PyObject *o) { diff --git a/wrapper_python/utils/conversion.h b/wrapper_python/utils/conversion.h index 17288f9..4f7aa96 100644 --- a/wrapper_python/utils/conversion.h +++ b/wrapper_python/utils/conversion.h @@ -66,7 +66,12 @@ enum { ARG_NONE = 0, ARG_MAT = 1, ARG_SCALAR = 2 }; class NDArrayConverter { private: - void init(); +#if PY_VERSION_HEX >= 0x03000000 + int init(); +#else + void init(); +#endif + public: NDArrayConverter(); cv::Mat toMat(const PyObject* o); -- GitLab