From 9de6d200b286767305f0654df2d1715813461277 Mon Sep 17 00:00:00 2001 From: Ellon Mendes Date: Thu, 9 Jun 2016 16:14:12 +0200 Subject: [PATCH 1/2] [python] Use macro to avoid shared_ptr registration warnings in boost python Warning message was: ../lib/python2.7/site-packages/gtsam/__init__.py:1: RuntimeWarning: to-Python converter for boost::shared_ptr already registered; second conversion method ignored. --- python/handwritten/linear/NoiseModel.cpp | 22 ++++++++++++++---- python/handwritten/navigation/ImuFactor.cpp | 25 ++++++++++++++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/python/handwritten/linear/NoiseModel.cpp b/python/handwritten/linear/NoiseModel.cpp index 3612f7e14..3b7896a27 100644 --- a/python/handwritten/linear/NoiseModel.cpp +++ b/python/handwritten/linear/NoiseModel.cpp @@ -28,6 +28,20 @@ #include "gtsam/linear/NoiseModel.h" +/* Fix to avoid registration warnings */ +// Solution taken from https://github.com/BVLC/caffe/pull/4069/commits/673e8cfc0b8f05f9fa3ebbad7cc6202822e5d9c5 +#define REGISTER_SHARED_PTR_TO_PYTHON(PTR) do { \ + const boost::python::type_info info = \ + boost::python::type_id >(); \ + const boost::python::converter::registration* reg = \ + boost::python::converter::registry::query(info); \ + if (reg == NULL) { \ + boost::python::register_ptr_to_python >(); \ + } else if ((*reg).m_to_python == NULL) { \ + boost::python::register_ptr_to_python >(); \ + } \ +} while (0) + using namespace boost::python; using namespace gtsam; using namespace gtsam::noiseModel; @@ -110,7 +124,7 @@ void exportNoiseModels(){ .def("Covariance",&Gaussian::Covariance, Gaussian_Covariance_overloads()) .staticmethod("Covariance") ; - register_ptr_to_python< boost::shared_ptr >(); + REGISTER_SHARED_PTR_TO_PYTHON(Gaussian); class_, bases >("Diagonal", no_init) .def("Sigmas",&Diagonal::Sigmas, Diagonal_Sigmas_overloads()) @@ -120,7 +134,7 @@ void exportNoiseModels(){ .def("Precisions",&Diagonal::Precisions, Diagonal_Precisions_overloads()) .staticmethod("Precisions") ; - register_ptr_to_python< boost::shared_ptr >(); + REGISTER_SHARED_PTR_TO_PYTHON(Diagonal); class_, bases >("Isotropic", no_init) .def("Sigma",&Isotropic::Sigma, Isotropic_Sigma_overloads()) @@ -130,12 +144,12 @@ void exportNoiseModels(){ .def("Precision",&Isotropic::Precision, Isotropic_Precision_overloads()) .staticmethod("Precision") ; - register_ptr_to_python< boost::shared_ptr >(); + REGISTER_SHARED_PTR_TO_PYTHON(Isotropic); class_, bases >("Unit", no_init) .def("Create",&Unit::Create) .staticmethod("Create") ; - register_ptr_to_python< boost::shared_ptr >(); + REGISTER_SHARED_PTR_TO_PYTHON(Unit); } diff --git a/python/handwritten/navigation/ImuFactor.cpp b/python/handwritten/navigation/ImuFactor.cpp index afc6f331d..d7c295894 100644 --- a/python/handwritten/navigation/ImuFactor.cpp +++ b/python/handwritten/navigation/ImuFactor.cpp @@ -22,6 +22,21 @@ #include "gtsam/navigation/ImuFactor.h" #include "gtsam/navigation/GPSFactor.h" +/* Fix to avoid registration warnings */ +// Solution taken from https://github.com/BVLC/caffe/pull/4069/commits/673e8cfc0b8f05f9fa3ebbad7cc6202822e5d9c5 +#define REGISTER_SHARED_PTR_TO_PYTHON(PTR) do { \ + const boost::python::type_info info = \ + boost::python::type_id >(); \ + const boost::python::converter::registration* reg = \ + boost::python::converter::registry::query(info); \ + if (reg == NULL) { \ + boost::python::register_ptr_to_python >(); \ + } else if ((*reg).m_to_python == NULL) { \ + boost::python::register_ptr_to_python >(); \ + } \ +} while (0) + + using namespace boost::python; using namespace gtsam; @@ -78,7 +93,7 @@ void exportImuFactor() { .staticmethod("MakeSharedU"); // NOTE(frank): https://mail.python.org/pipermail/cplusplus-sig/2016-January/017362.html - register_ptr_to_python< boost::shared_ptr >(); + REGISTER_SHARED_PTR_TO_PYTHON(PreintegrationParams); class_( #ifdef GTSAM_TANGENT_PREINTEGRATION @@ -105,21 +120,21 @@ void exportImuFactor() { .def("error", &ImuFactor::error) .def(init()) .def(repr(self)); - register_ptr_to_python>(); + REGISTER_SHARED_PTR_TO_PYTHON(ImuFactor); class_, boost::shared_ptr>("ImuFactor2") .def("error", &ImuFactor2::error) .def(init()) .def(repr(self)); - register_ptr_to_python>(); + REGISTER_SHARED_PTR_TO_PYTHON(ImuFactor2); class_, boost::shared_ptr>("GPSFactor") .def("error", &GPSFactor::error) .def(init()); - register_ptr_to_python>(); + REGISTER_SHARED_PTR_TO_PYTHON(GPSFactor); class_, boost::shared_ptr>("GPSFactor2") .def("error", &GPSFactor2::error) .def(init()); - register_ptr_to_python>(); + REGISTER_SHARED_PTR_TO_PYTHON(GPSFactor2); } From 62e0cb661311f0676aace1e0e670df9b7af670c3 Mon Sep 17 00:00:00 2001 From: Ellon Mendes Date: Mon, 13 Jun 2016 15:13:49 +0200 Subject: [PATCH 2/2] [python] Put the macro into a separated file (python/handwritten/common.h) --- python/handwritten/common.h | 31 +++++++++++++++++++++ python/handwritten/linear/NoiseModel.cpp | 14 +--------- python/handwritten/navigation/ImuFactor.cpp | 15 +--------- 3 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 python/handwritten/common.h diff --git a/python/handwritten/common.h b/python/handwritten/common.h new file mode 100644 index 000000000..72d2ae846 --- /dev/null +++ b/python/handwritten/common.h @@ -0,0 +1,31 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @brief common macros used by handwritten exports of the python module + * @author Ellon Paiva Mendes (LAAS-CNRS) + **/ + +#pragma once + + /* Fix to avoid registration warnings */ +// Solution taken from https://github.com/BVLC/caffe/pull/4069/commits/673e8cfc0b8f05f9fa3ebbad7cc6202822e5d9c5 +#define REGISTER_SHARED_PTR_TO_PYTHON(PTR) do { \ + const boost::python::type_info info = \ + boost::python::type_id >(); \ + const boost::python::converter::registration* reg = \ + boost::python::converter::registry::query(info); \ + if (reg == NULL) { \ + boost::python::register_ptr_to_python >(); \ + } else if ((*reg).m_to_python == NULL) { \ + boost::python::register_ptr_to_python >(); \ + } \ +} while (0) diff --git a/python/handwritten/linear/NoiseModel.cpp b/python/handwritten/linear/NoiseModel.cpp index 3b7896a27..00fa9d74f 100644 --- a/python/handwritten/linear/NoiseModel.cpp +++ b/python/handwritten/linear/NoiseModel.cpp @@ -28,19 +28,7 @@ #include "gtsam/linear/NoiseModel.h" -/* Fix to avoid registration warnings */ -// Solution taken from https://github.com/BVLC/caffe/pull/4069/commits/673e8cfc0b8f05f9fa3ebbad7cc6202822e5d9c5 -#define REGISTER_SHARED_PTR_TO_PYTHON(PTR) do { \ - const boost::python::type_info info = \ - boost::python::type_id >(); \ - const boost::python::converter::registration* reg = \ - boost::python::converter::registry::query(info); \ - if (reg == NULL) { \ - boost::python::register_ptr_to_python >(); \ - } else if ((*reg).m_to_python == NULL) { \ - boost::python::register_ptr_to_python >(); \ - } \ -} while (0) +#include "python/handwritten/common.h" using namespace boost::python; using namespace gtsam; diff --git a/python/handwritten/navigation/ImuFactor.cpp b/python/handwritten/navigation/ImuFactor.cpp index d7c295894..0cf3062b5 100644 --- a/python/handwritten/navigation/ImuFactor.cpp +++ b/python/handwritten/navigation/ImuFactor.cpp @@ -22,20 +22,7 @@ #include "gtsam/navigation/ImuFactor.h" #include "gtsam/navigation/GPSFactor.h" -/* Fix to avoid registration warnings */ -// Solution taken from https://github.com/BVLC/caffe/pull/4069/commits/673e8cfc0b8f05f9fa3ebbad7cc6202822e5d9c5 -#define REGISTER_SHARED_PTR_TO_PYTHON(PTR) do { \ - const boost::python::type_info info = \ - boost::python::type_id >(); \ - const boost::python::converter::registration* reg = \ - boost::python::converter::registry::query(info); \ - if (reg == NULL) { \ - boost::python::register_ptr_to_python >(); \ - } else if ((*reg).m_to_python == NULL) { \ - boost::python::register_ptr_to_python >(); \ - } \ -} while (0) - +#include "python/handwritten/common.h" using namespace boost::python; using namespace gtsam;