From b893bd323e2192401c76cc57bb5efad0e310eee0 Mon Sep 17 00:00:00 2001 From: Mmanu Chaturvedi Date: Fri, 17 May 2019 11:53:52 -0400 Subject: [PATCH] cast: Ensure that pointers do not use rvalue / move overload --- include/pybind11/cast.h | 2 +- tests/test_builtin_casters.cpp | 9 +++++++++ tests/test_builtin_casters.py | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 89d2ebf142..003625feab 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1970,7 +1970,7 @@ detail::enable_if_t< template detail::enable_if_t< - std::is_rvalue_reference::value && !detail::is_pyobject>::value, object> + std::is_rvalue_reference::value && !std::is_pointer::value && !detail::is_pyobject>::value, object> cast(T&& value) { // Have to use `pybind11::move` because some compilers might try to bind `move` to `std::move`... return pybind11::move(std::move(value)); diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index e026127f89..db48fd4836 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -167,4 +167,13 @@ TEST_SUBMODULE(builtin_casters, m) { py::object o = py::cast(v); return py::cast(o) == v; }); + + // For Drake issue: https://github.com/RobotLocomotion/drake/issues/9398 + m.def("test_pointer_caster", []() -> bool { + UserType a; + UserType *a_ptr = &a; + py::object o = py::cast(&a); // Rvalue + py::object o1 = py::cast(a_ptr); // Non-rvalue + return (py::cast(o) == a_ptr && py::cast(o1) == a_ptr); + }); } diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 73cc465f5b..1905c90a8a 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -340,3 +340,7 @@ def test_int_long(): def test_void_caster_2(): assert m.test_void_caster() + + +def test_pointer_caster(): + assert m.test_pointer_caster()