Skip to content

Commit

Permalink
Fix casts to void* (pybind#4275)
Browse files Browse the repository at this point in the history
* Fix casts to void*

* Improve tests

* style: pre-commit fixes

* remove c style cast

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
  • Loading branch information
3 people authored and henryiii committed Oct 23, 2022
1 parent 305c471 commit ce63bcb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 2 additions & 4 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1179,11 +1179,9 @@ enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&)
pybind11_fail("Internal error: cast_safe fallback invoked");
}
template <typename T>
enable_if_t<std::is_same<void, intrinsic_t<T>>::value, void> cast_safe(object &&) {}
enable_if_t<std::is_void<T>::value, void> cast_safe(object &&) {}
template <typename T>
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>,
std::is_same<void, intrinsic_t<T>>>::value,
T>
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>, std::is_void<T>>::value, T>
cast_safe(object &&o) {
return pybind11::cast<T>(std::move(o));
}
Expand Down
18 changes: 17 additions & 1 deletion tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ TEST_SUBMODULE(class_, m) {

protected:
virtual int foo() const { return value; }
virtual void *void_foo() { return static_cast<void *>(&value); }
virtual void *get_self() { return static_cast<void *>(this); }

private:
int value = 42;
Expand All @@ -392,6 +394,8 @@ TEST_SUBMODULE(class_, m) {
class TrampolineB : public ProtectedB {
public:
int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); }
void *void_foo() override { PYBIND11_OVERRIDE(void *, ProtectedB, void_foo, ); }
void *get_self() override { PYBIND11_OVERRIDE(void *, ProtectedB, get_self, ); }
};

class PublicistB : public ProtectedB {
Expand All @@ -401,11 +405,23 @@ TEST_SUBMODULE(class_, m) {
// (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827)
~PublicistB() override{}; // NOLINT(modernize-use-equals-default)
using ProtectedB::foo;
using ProtectedB::get_self;
using ProtectedB::void_foo;
};

m.def("read_foo", [](const void *original) {
const int *ptr = reinterpret_cast<const int *>(original);
return *ptr;
});

m.def("pointers_equal",
[](const void *original, const void *comparison) { return original == comparison; });

py::class_<ProtectedB, TrampolineB>(m, "ProtectedB")
.def(py::init<>())
.def("foo", &PublicistB::foo);
.def("foo", &PublicistB::foo)
.def("void_foo", &PublicistB::void_foo)
.def("get_self", &PublicistB::get_self);

// test_brace_initialization
struct BraceInitialization {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ def test_bind_protected_functions():

b = m.ProtectedB()
assert b.foo() == 42
assert m.read_foo(b.void_foo()) == 42
assert m.pointers_equal(b.get_self(), b)

class C(m.ProtectedB):
def __init__(self):
Expand Down

0 comments on commit ce63bcb

Please sign in to comment.