Skip to content

Fix casts to void* #4273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_same<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_same<void, T>>::value, T>
cast_safe(object &&o) {
return pybind11::cast<T>(std::move(o));
}
Expand Down
11 changes: 10 additions & 1 deletion tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ TEST_SUBMODULE(class_, m) {

protected:
virtual int foo() const { return value; }
virtual void *void_foo() { return (void *) &value; }

private:
int value = 42;
Expand All @@ -392,6 +393,7 @@ 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, ); }
};

class PublicistB : public ProtectedB {
Expand All @@ -401,11 +403,18 @@ 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::void_foo;
};

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

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

// test_brace_initialization
struct BraceInitialization {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ctypes

import pytest

import env # noqa: F401
Expand Down Expand Up @@ -313,6 +315,7 @@ def test_bind_protected_functions():

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

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