Skip to content

Test recursive dispatch using visitor pattern. #3365

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

Merged
merged 1 commit into from
Oct 22, 2021
Merged
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
40 changes: 40 additions & 0 deletions tests/test_virtual_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ struct DispatchIssue : Base {
}
};

// An abstract adder class that uses visitor pattern to add two data
// objects and send the result to the visitor functor
struct AdderBase {
struct Data {};
using DataVisitor = std::function<void (const Data&)>;

virtual void operator()(const Data& first, const Data& second, const DataVisitor& visitor) const = 0;
virtual ~AdderBase() = default;
AdderBase() = default;
AdderBase(const AdderBase&) = delete;
};

struct Adder : AdderBase {
void operator()(const Data& first, const Data& second, const DataVisitor& visitor) const override {
PYBIND11_OVERRIDE_PURE_NAME(void, AdderBase, "__call__", operator(), first, second, visitor);
}
};


static void test_gil() {
{
py::gil_scoped_acquire lock;
Expand Down Expand Up @@ -295,6 +314,27 @@ TEST_SUBMODULE(virtual_functions, m) {

m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });

// test_recursive_dispatch_issue
// #3357: Recursive dispatch fails to find python function override
pybind11::class_<AdderBase, Adder>(m, "Adder")
.def(pybind11::init<>())
.def("__call__", &AdderBase::operator());

pybind11::class_<AdderBase::Data>(m, "Data")
.def(pybind11::init<>());

m.def("add2", [](const AdderBase::Data& first, const AdderBase::Data& second,
const AdderBase& adder, const AdderBase::DataVisitor& visitor) {
adder(first, second, visitor);
});

m.def("add3", [](const AdderBase::Data& first, const AdderBase::Data& second, const AdderBase::Data& third,
const AdderBase& adder, const AdderBase::DataVisitor& visitor) {
adder(first, second, [&] (const AdderBase::Data& first_plus_second) {
adder(first_plus_second, third, visitor);
});
});

// test_override_ref
// #392/397: overriding reference-returning functions
class OverrideTest {
Expand Down
33 changes: 33 additions & 0 deletions tests/test_virtual_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,39 @@ def dispatch(self):
assert m.dispatch_issue_go(b) == "Yay.."


def test_recursive_dispatch_issue(msg):
"""#3357: Recursive dispatch fails to find python function override"""

class Data(m.Data):
def __init__(self, value):
super(Data, self).__init__()
self.value = value

class Adder(m.Adder):
def __call__(self, first, second, visitor):
# lambda is a workaround, which adds extra frame to the
# current CPython thread. Removing lambda reveals the bug
# [https://github.com/pybind/pybind11/issues/3357]
(lambda: visitor(Data(first.value + second.value)))()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't the lambda have not been here, and the test marked with xfail? It's not a segfault, right?

Copy link
Collaborator

@rwgk rwgk Oct 24, 2021

Choose a reason for hiding this comment

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

Maybe an additional test.
I think it's important to keep this test (with the extra lambda) indefinitely. We want to be sure it doesn't break, even after the bug is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also thought about two tests: one expected to fail, another with a workaround. I can create another PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

May be nice ... but at the moment it doesn't buy as anything tangible (the existing comments are very clear) and it'll be an organic minor part of the work fixing the bug. I'd just leave it for later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. Though, it would be fun to try and learn more about xfail.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Up to you! I'll vote to merge if you send the PR.


class StoreResultVisitor:
def __init__(self):
self.result = None

def __call__(self, data):
self.result = data.value

store = StoreResultVisitor()

m.add2(Data(1), Data(2), Adder(), store)
assert store.result == 3

# without lambda in Adder class, this function fails with
# RuntimeError: Tried to call pure virtual function "AdderBase::__call__"
m.add3(Data(1), Data(2), Data(3), Adder(), store)
assert store.result == 6


def test_override_ref():
"""#392/397: overriding reference-returning functions"""
o = m.OverrideTest("asdf")
Expand Down