93c42c22b8b2f45040302732407995ad1d689792
[gem5.git] / ext / pybind11 / tests / test_callbacks.py
1 import pytest
2 from pybind11_tests import callbacks as m
3
4
5 def test_callbacks():
6 from functools import partial
7
8 def func1():
9 return "func1"
10
11 def func2(a, b, c, d):
12 return "func2", a, b, c, d
13
14 def func3(a):
15 return "func3({})".format(a)
16
17 assert m.test_callback1(func1) == "func1"
18 assert m.test_callback2(func2) == ("func2", "Hello", "x", True, 5)
19 assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
20 assert m.test_callback1(partial(func3, "partial")) == "func3(partial)"
21 assert m.test_callback3(lambda i: i + 1) == "func(43) = 44"
22
23 f = m.test_callback4()
24 assert f(43) == 44
25 f = m.test_callback5()
26 assert f(number=43) == 44
27
28
29 def test_bound_method_callback():
30 # Bound Python method:
31 class MyClass:
32 def double(self, val):
33 return 2 * val
34
35 z = MyClass()
36 assert m.test_callback3(z.double) == "func(43) = 86"
37
38 z = m.CppBoundMethodTest()
39 assert m.test_callback3(z.triple) == "func(43) = 129"
40
41
42 def test_keyword_args_and_generalized_unpacking():
43
44 def f(*args, **kwargs):
45 return args, kwargs
46
47 assert m.test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
48 assert m.test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
49 assert m.test_keyword_args(f) == ((), {"x": 10, "y": 20})
50 assert m.test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
51 assert m.test_unpacking_and_keywords2(f) == (
52 ("positional", 1, 2, 3, 4, 5),
53 {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
54 )
55
56 with pytest.raises(TypeError) as excinfo:
57 m.test_unpacking_error1(f)
58 assert "Got multiple values for keyword argument" in str(excinfo.value)
59
60 with pytest.raises(TypeError) as excinfo:
61 m.test_unpacking_error2(f)
62 assert "Got multiple values for keyword argument" in str(excinfo.value)
63
64 with pytest.raises(RuntimeError) as excinfo:
65 m.test_arg_conversion_error1(f)
66 assert "Unable to convert call argument" in str(excinfo.value)
67
68 with pytest.raises(RuntimeError) as excinfo:
69 m.test_arg_conversion_error2(f)
70 assert "Unable to convert call argument" in str(excinfo.value)
71
72
73 def test_lambda_closure_cleanup():
74 m.test_cleanup()
75 cstats = m.payload_cstats()
76 assert cstats.alive() == 0
77 assert cstats.copy_constructions == 1
78 assert cstats.move_constructions >= 1
79
80
81 def test_cpp_function_roundtrip():
82 """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
83
84 assert m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2"
85 assert (m.test_dummy_function(m.roundtrip(m.dummy_function)) ==
86 "matches dummy_function: eval(1) = 2")
87 assert m.roundtrip(None, expect_none=True) is None
88 assert (m.test_dummy_function(lambda x: x + 2) ==
89 "can't convert to function pointer: eval(1) = 3")
90
91 with pytest.raises(TypeError) as excinfo:
92 m.test_dummy_function(m.dummy_function2)
93 assert "incompatible function arguments" in str(excinfo.value)
94
95 with pytest.raises(TypeError) as excinfo:
96 m.test_dummy_function(lambda x, y: x + y)
97 assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
98 "takes exactly 2 arguments"))
99
100
101 def test_function_signatures(doc):
102 assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
103 assert doc(m.test_callback4) == "test_callback4() -> Callable[[int], int]"
104
105
106 def test_movable_object():
107 assert m.callback_with_movable(lambda _: None) is True