From ea3f4225b7daa589893068927fd91866c8ee9072 Mon Sep 17 00:00:00 2001 From: Vincent Palancher Date: Tue, 11 Jan 2022 17:01:04 +0100 Subject: [PATCH] build: Upgrades version of pybind11 from v2.5.0 to v2.9.0. The public constructor of `pybind11::module` is now deprecated. This patch replaces its only use by a call to `PYBIND11_EMBEDDED_MODULE` followed by a `pybind11::import(...)`. The constructor of a `pybind11::object` subclass from a `pybind11::object` from the wrong type now throws an exception. See https://github.com/pybind/pybind11/pull/2349. We had one case of this happening where we tried constructing a `bytes` from a `None`. We now first construct an `object`, then test if it's `None` before trying to convert it to `bytes`. Change-Id: I4aa2a1b4a49d3024f203bdd5c6f9c4ee86bfc9c4 Reviewed-on: http://gerrit2.aldebaran.lan/1760 Reviewed-by: philippe.martin Reviewed-by: jmonnon Tested-by: vincent.palancher --- cmake/set_dependencies.cmake | 2 +- src/pyobject.cpp | 9 +++++---- tests/test_qipython.cpp | 9 ++++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cmake/set_dependencies.cmake b/cmake/set_dependencies.cmake index b61a427..ce237f0 100644 --- a/cmake/set_dependencies.cmake +++ b/cmake/set_dependencies.cmake @@ -48,7 +48,7 @@ include_guard(GLOBAL) overridable_variable(BOOST_VERSION 1.77) # Version of pybind11 to use. -overridable_variable(PYBIND11_VERSION 2.5.0) +overridable_variable(PYBIND11_VERSION 2.9.0) # URL of the git repository from which to download pybind11. For more details, see CMake # `ExternalProject` module documentation of the `GIT_REPOSITORY` argument. diff --git a/src/pyobject.cpp b/src/pyobject.cpp index 11598e6..d90a1e0 100644 --- a/src/pyobject.cpp +++ b/src/pyobject.cpp @@ -270,10 +270,11 @@ namespace detail boost::optional readObjectUid(const ::py::object& obj) { GILAcquire lock; - const ::py::bytes qiObjectUid = ::py::getattr(obj, qiObjectUidAttributeName, ::py::none()); - if (!qiObjectUid.is_none()) - return deserializeObjectUid(static_cast(qiObjectUid)); - return {}; + const auto qiObjectUidObj = ::py::getattr(obj, qiObjectUidAttributeName, ::py::none()); + if (qiObjectUidObj.is_none()) + return {}; + const auto qiObjectUid = qiObjectUidObj.cast(); + return deserializeObjectUid(qiObjectUid); } void writeObjectUid(const pybind11::object& obj, const ObjectUid& uid) diff --git a/tests/test_qipython.cpp b/tests/test_qipython.cpp index c6ce81f..1ac1495 100644 --- a/tests/test_qipython.cpp +++ b/tests/test_qipython.cpp @@ -15,6 +15,10 @@ qiLogCategory("TestQiPython"); namespace py = pybind11; +PYBIND11_EMBEDDED_MODULE(qi, m) { + qi::py::exportAll(m); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); @@ -24,9 +28,8 @@ int main(int argc, char **argv) boost::optional app; app.emplace(argc, argv); - py::module m("qi"); - qi::py::exportAll(m); - py::globals()["qi"] = m; + py::globals()["qi"] = py::module::import("qi"); + int ret = EXIT_FAILURE; {