From b065acc8f7d5e2e9b22324cd5ac337093189db56 Mon Sep 17 00:00:00 2001 From: jcarpent Date: Sun, 16 Oct 2016 13:59:01 +0200 Subject: [PATCH 1/5] [C++] Remove warning in the offset computation --- src/memory.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/memory.hpp b/src/memory.hpp index 99d5d4f3e..8c7390b66 100644 --- a/src/memory.hpp +++ b/src/memory.hpp @@ -77,8 +77,8 @@ namespace boost { namespace python { namespace objects { \ Holder* holder = Derived::construct(&instance->storage, (PyObject*)instance, x); \ holder->install(raw_result); \ \ - size_t holder_offset = reinterpret_cast(holder) \ - - reinterpret_cast(&instance->storage) \ + Py_ssize_t holder_offset = reinterpret_cast(holder) \ + - reinterpret_cast(&instance->storage) \ + offsetof(instance_t, storage); \ Py_SIZE(instance) = holder_offset; \ \ From d70a5dc76044471c3e2f1ab45901420626ab3933 Mon Sep 17 00:00:00 2001 From: jcarpent Date: Sun, 16 Oct 2016 13:59:49 +0200 Subject: [PATCH 2/5] [C++] Remove useless warning in the cast of memory ptr --- src/details.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/details.hpp b/src/details.hpp index 9ab072b0c..23b17e4e3 100644 --- a/src/details.hpp +++ b/src/details.hpp @@ -190,7 +190,7 @@ namespace eigenpy typename MapNumpy::EigenMap numpyMap = MapNumpy::map(pyArray); void* storage = ((bp::converter::rvalue_from_python_storage*) - (memory))->storage.bytes; + ((void*)memory))->storage.bytes; assert( (numpyMap.rows() Date: Wed, 19 Oct 2016 11:58:21 +0200 Subject: [PATCH 3/5] [C++] Correct import_array return type --- src/details.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/details.hpp b/src/details.hpp index 23b17e4e3..8e98d53af 100644 --- a/src/details.hpp +++ b/src/details.hpp @@ -201,11 +201,11 @@ namespace eigenpy eigenMatrix = numpyMap; } }; - +#define numpy_import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); } } + template void enableEigenPySpecific() { - import_array(); #ifdef EIGEN_DONT_VECTORIZE @@ -217,6 +217,7 @@ namespace eigenpy boost::python::to_python_converter >(); eigenpy::EigenFromPy(); + numpy_import_array(); typedef typename eigenpy::UnalignedEquivalent::type MatTypeDontAlign; #ifndef EIGENPY_ALIGNED From 6a502cde5c9a788bb1861e45a5222dddd2ead7db Mon Sep 17 00:00:00 2001 From: jcarpent Date: Wed, 19 Oct 2016 11:59:27 +0200 Subject: [PATCH 4/5] [C++] Register only FROM et TO MatType conversions One should register the non aligned version if needed explicitely --- src/details.hpp | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/details.hpp b/src/details.hpp index 8e98d53af..8ca96c9eb 100644 --- a/src/details.hpp +++ b/src/details.hpp @@ -206,28 +206,10 @@ namespace eigenpy template void enableEigenPySpecific() { - -#ifdef EIGEN_DONT_VECTORIZE - - boost::python::to_python_converter >(); - eigenpy::EigenFromPy(); -#else - - boost::python::to_python_converter >(); - eigenpy::EigenFromPy(); numpy_import_array(); - typedef typename eigenpy::UnalignedEquivalent::type MatTypeDontAlign; -#ifndef EIGENPY_ALIGNED - boost::python::to_python_converter >(); - eigenpy::EigenFromPy(); -#endif -#endif - - + boost::python::to_python_converter >(); + EigenFromPy(); } } // namespace eigenpy From 90ff656d697b784d07467855476a50b258112d71 Mon Sep 17 00:00:00 2001 From: jcarpent Date: Wed, 19 Oct 2016 14:31:56 +0200 Subject: [PATCH 5/5] [C++] Add check of registration at runtime --- CMakeLists.txt | 1 + src/details.hpp | 2 ++ src/exception.cpp | 7 +++++-- src/registration.hpp | 45 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/registration.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4962eb9b1..8acb0ef35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,7 @@ SET(HEADERS src/map.hpp src/geometry.hpp src/memory.hpp + src/registration.hpp src/angle-axis.hpp src/quaternion.hpp ) diff --git a/src/details.hpp b/src/details.hpp index 8ca96c9eb..406b13389 100644 --- a/src/details.hpp +++ b/src/details.hpp @@ -24,6 +24,7 @@ #include #include "eigenpy/eigenpy.hpp" +#include "eigenpy/registration.hpp" #include "eigenpy/exception.hpp" #include "eigenpy/map.hpp" @@ -206,6 +207,7 @@ namespace eigenpy template void enableEigenPySpecific() { + if(check_registration()) return; numpy_import_array(); boost::python::to_python_converter >(); diff --git a/src/exception.cpp b/src/exception.cpp index b8ac24cad..e67b29876 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 LAAS-CNRS + * Copyright (c) 2015-2016 LAAS-CNRS * * This file is part of eigenpy. * eigenpy is free software: you can redistribute it and/or @@ -14,7 +14,8 @@ * with eigenpy. If not, see . */ -#include +#include "eigenpy/exception.hpp" +#include "eigenpy/registration.hpp" namespace eigenpy @@ -30,6 +31,8 @@ namespace eigenpy void Exception::registerException() { + if(check_registration()) return; + pyType = boost::python::class_ ("Exception",boost::python::init()) .add_property("message", &eigenpy::Exception::copyMessage) diff --git a/src/registration.hpp b/src/registration.hpp new file mode 100644 index 000000000..482335f38 --- /dev/null +++ b/src/registration.hpp @@ -0,0 +1,45 @@ +/* + * Copyright 2016, Justin Carpentier, LAAS-CNRS + * + * This file is part of eigenpy. + * eigenpy is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * eigenpy is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. You should + * have received a copy of the GNU Lesser General Public License along + * with eigenpy. If not, see . + */ + +#ifndef __eigenpy_registration_hpp__ +#define __eigenpy_registration_hpp__ + +#include + +namespace eigenpy +{ + /// + /// \brief Check at runtime the registration of the type T inside the boost python registry. + /// + /// \tparam T The type to check the registration. + /// + /// \returns true if the type T is already registered. + /// + template + inline bool check_registration() + { + namespace bp = boost::python; + + const bp::type_info info = bp::type_id(); + const bp::converter::registration* reg = bp::converter::registry::query(info); + if (reg == NULL) return false; + else if ((*reg).m_to_python == NULL) return false; + + return true; + } +} + +#endif // ifndef __eigenpy_registration_hpp__