Skip to content

Commit

Permalink
Disable implicit conversion from 0 to pybind11::handle.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jun 15, 2022
1 parent 0964a90 commit ca8d9fd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 28 additions & 1 deletion include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ class object_api : public pyobject_tag {
bool rich_compare(object_api const &other, int value) const;
};

template <typename T, typename SFINAE = void>
struct has_member_ob_refcnt : std::false_type {};

template <typename T>
struct has_member_ob_refcnt<T, decltype((void) T::ob_refcnt, void())> : std::true_type {};

template <typename T, typename SFINAE = void>
struct is_c_api_py_object_pointer : std::false_type {};

template <typename T>
struct is_c_api_py_object_pointer<
T,
detail::enable_if_t<
std::is_pointer<T>::value
&& detail::has_member_ob_refcnt<typename std::remove_pointer<T>::type>::value>>
: std::true_type {};

PYBIND11_NAMESPACE_END(detail)

#if !defined(PYBIND11_HANDLE_REF_DEBUG) && !defined(NDEBUG)
Expand All @@ -212,8 +229,18 @@ class handle : public detail::object_api<handle> {
/// The default constructor creates a handle with a ``nullptr``-valued pointer
handle() = default;
/// Creates a ``handle`` from the given raw Python object pointer
/// Not using ``handle(PyObject *ptr)`` to avoid implicit conversion from ``0``.
template <typename T,
detail::enable_if_t<detail::is_c_api_py_object_pointer<T>::value
|| std::is_same<T, std::nullptr_t>::value,
int> = 0>
// NOLINTNEXTLINE(google-explicit-constructor)
handle(PyObject *ptr) : m_ptr(ptr) {} // Allow implicit conversion from PyObject*
handle(/* PyObject* */ T ptr) : m_ptr(ptr) {} // Allow implicit conversion from PyObject*

handle(const handle &) = default;
handle(handle &&) = default;
handle &operator=(const handle &) = default;
handle &operator=(handle &&) = default;

/// Return the underlying ``PyObject *`` pointer
PyObject *ptr() const { return m_ptr; }
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class float_ : public py::object {
};
} // namespace external

namespace implicit_conversion_from_0_to_handle {
// Uncomment to trigger compiler error. Note: Before PR #4008 this used to compile successfully.
// py::handle expected_to_trigger_compiler_error() { return 0; }
} // namespace implicit_conversion_from_0_to_handle

TEST_SUBMODULE(pytypes, m) {
// test_bool
m.def("get_bool", [] { return py::bool_(false); });
Expand Down

0 comments on commit ca8d9fd

Please sign in to comment.