Skip to content
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

Fix and test for issue #4117 #4274

Closed
wants to merge 3 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: 4 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1178,11 +1178,13 @@ template <typename T>
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, remove_cvref_t<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,
std::is_same<void, remove_cvref_t<T>>>::value,
T>
cast_safe(object &&o) {
return pybind11::cast<T>(std::move(o));
Expand Down
31 changes: 31 additions & 0 deletions tests/test_virtual_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,35 @@ inline int test_override_cache(std::shared_ptr<test_override_cache_helper> const
// are rather long).
void initialize_inherited_virtuals(py::module_ &m);

namespace issue_4117 { // Broken by PR #3861

class Animal {
public:
virtual ~Animal() {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
virtual ~Animal() {}
virtual ~Animal() = default;

virtual void *go() = 0;
};

class PyAnimal : public Animal {
public:
/* Inherit the constructors */
using Animal::Animal;
/* Trampoline (need one for each virtual function) */
void *go() override {
PYBIND11_OVERRIDE_PURE(void *, /* Return type */
Animal, /* Parent class */
go, /* Name of function in C++ (must match Python name) */
);
}
};

void bindings(py::module_ m) {
py::class_<Animal, PyAnimal /* <--- trampoline*/>(m, "Issue4117Animal")
.def(py::init<>())
.def("go", &Animal::go);
}

} // namespace issue_4117

TEST_SUBMODULE(virtual_functions, m) {
// test_override
py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
Expand Down Expand Up @@ -410,6 +439,8 @@ TEST_SUBMODULE(virtual_functions, m) {
.def("func", &test_override_cache_helper::func);

m.def("test_override_cache", test_override_cache);

issue_4117::bindings(m);
}

// Inheriting virtual methods. We do two versions here: the repeat-everything version and the
Expand Down
9 changes: 9 additions & 0 deletions tests/test_virtual_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,12 @@ class Test(m.test_override_cache_helper):
for _ in range(1500):
assert m.test_override_cache(func()) == 42
assert m.test_override_cache(func2()) == 0


def test_issue_4117():
class Lynx(m.Issue4117Animal):
def go(self):
return self

obj = Lynx()
assert obj.go() is obj