aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/tests/test_virtual_functions.py
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/pybind11/tests/test_virtual_functions.py')
-rw-r--r--3rdparty/pybind11/tests/test_virtual_functions.py78
1 files changed, 64 insertions, 14 deletions
diff --git a/3rdparty/pybind11/tests/test_virtual_functions.py b/3rdparty/pybind11/tests/test_virtual_functions.py
index ae199301..4d00d369 100644
--- a/3rdparty/pybind11/tests/test_virtual_functions.py
+++ b/3rdparty/pybind11/tests/test_virtual_functions.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
import pytest
import env # noqa: F401
@@ -10,12 +9,12 @@ from pybind11_tests import ConstructorStats # noqa: E402
def test_override(capture, msg):
class ExtendedExampleVirt(m.ExampleVirt):
def __init__(self, state):
- super(ExtendedExampleVirt, self).__init__(state + 1)
+ super().__init__(state + 1)
self.data = "Hello world"
def run(self, value):
- print("ExtendedExampleVirt::run(%i), calling parent.." % value)
- return super(ExtendedExampleVirt, self).run(value + 1)
+ print(f"ExtendedExampleVirt::run({value}), calling parent..")
+ return super().run(value + 1)
def run_bool(self):
print("ExtendedExampleVirt::run_bool()")
@@ -25,11 +24,11 @@ def test_override(capture, msg):
return "override1"
def pure_virtual(self):
- print("ExtendedExampleVirt::pure_virtual(): %s" % self.data)
+ print(f"ExtendedExampleVirt::pure_virtual(): {self.data}")
class ExtendedExampleVirt2(ExtendedExampleVirt):
def __init__(self, state):
- super(ExtendedExampleVirt2, self).__init__(state + 1)
+ super().__init__(state + 1)
def get_string2(self):
return "override2"
@@ -41,7 +40,7 @@ def test_override(capture, msg):
capture
== """
Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2)
- """ # noqa: E501 line too long
+ """
)
with pytest.raises(RuntimeError) as excinfo:
@@ -59,7 +58,7 @@ def test_override(capture, msg):
== """
ExtendedExampleVirt::run(20), calling parent..
Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2)
- """ # noqa: E501 line too long
+ """
)
with capture:
assert m.runExampleVirtBool(ex12p) is False
@@ -76,7 +75,7 @@ def test_override(capture, msg):
== """
ExtendedExampleVirt::run(50), calling parent..
Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2)
- """ # noqa: E501 line too long
+ """
)
cstats = ConstructorStats.get(m.ExampleVirt)
@@ -97,7 +96,7 @@ def test_alias_delay_initialization1(capture):
class B(m.A):
def __init__(self):
- super(B, self).__init__()
+ super().__init__()
def f(self):
print("In python f()")
@@ -137,7 +136,7 @@ def test_alias_delay_initialization2(capture):
class B2(m.A2):
def __init__(self):
- super(B2, self).__init__()
+ super().__init__()
def f(self):
print("In python B2.f()")
@@ -245,19 +244,51 @@ def test_dispatch_issue(msg):
class PyClass2(m.DispatchIssue):
def dispatch(self):
with pytest.raises(RuntimeError) as excinfo:
- super(PyClass2, self).dispatch()
+ super().dispatch()
assert (
msg(excinfo.value)
== 'Tried to call pure virtual function "Base::dispatch"'
)
- p = PyClass1()
- return m.dispatch_issue_go(p)
+ return m.dispatch_issue_go(PyClass1())
b = PyClass2()
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().__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)))()
+
+ 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")
@@ -407,3 +438,22 @@ def test_issue_1454():
# Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7)
m.test_gil()
m.test_gil_from_thread()
+
+
+def test_python_override():
+ def func():
+ class Test(m.test_override_cache_helper):
+ def func(self):
+ return 42
+
+ return Test()
+
+ def func2():
+ class Test(m.test_override_cache_helper):
+ pass
+
+ return Test()
+
+ for _ in range(1500):
+ assert m.test_override_cache(func()) == 42
+ assert m.test_override_cache(func2()) == 0