#3357: Recursive dispatch fails to find python function override
()
| 265 | |
| 266 | |
| 267 | def 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 | |
| 300 | def test_override_ref(): |
nothing calls this directly
no test coverage detected