MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_recursive_dispatch_issue

Function test_recursive_dispatch_issue

tests/test_virtual_functions.py:267–297  ·  view source on GitHub ↗

#3357: Recursive dispatch fails to find python function override

()

Source from the content-addressed store, hash-verified

265
266
267def test_recursive_dispatch_issue():
268 """#3357: Recursive dispatch fails to find python function override"""
269
270 class Data(m.Data):
271 def __init__(self, value):
272 super().__init__()
273 self.value = value
274
275 class Adder(m.Adder):
276 def __call__(self, first, second, visitor):
277 # lambda is a workaround, which adds extra frame to the
278 # current CPython thread. Removing lambda reveals the bug
279 # [https://github.com/pybind/pybind11/issues/3357]
280 (lambda: visitor(Data(first.value + second.value)))() # noqa: PLC3002
281
282 class StoreResultVisitor:
283 def __init__(self):
284 self.result = None
285
286 def __call__(self, data):
287 self.result = data.value
288
289 store = StoreResultVisitor()
290
291 m.add2(Data(1), Data(2), Adder(), store)
292 assert store.result == 3
293
294 # without lambda in Adder class, this function fails with
295 # RuntimeError: Tried to call pure virtual function "AdderBase::__call__"
296 m.add3(Data(1), Data(2), Data(3), Adder(), store)
297 assert store.result == 6
298
299
300def test_override_ref():

Callers

nothing calls this directly

Calls 5

StoreResultVisitorClass · 0.85
add2Method · 0.80
add3Method · 0.80
DataClass · 0.70
AdderClass · 0.70

Tested by

no test coverage detected