2 from threading
import Thread
7 from pybind11_tests
import callbacks
as m
8 from pybind11_tests
import detailed_error_messages_enabled
12 from functools
import partial
17 def func2(a, b, c, d):
18 return "func2", a, b, c, d
23 assert m.test_callback1(func1) ==
"func1"
24 assert m.test_callback2(func2) == (
"func2",
"Hello",
"x",
True, 5)
25 assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == (
"func2", 1, 2, 3, 4)
26 assert m.test_callback1(partial(func3,
"partial")) ==
"func3(partial)"
27 assert m.test_callback3(
lambda i: i + 1) ==
"func(43) = 44"
29 f = m.test_callback4()
31 f = m.test_callback5()
32 assert f(number=43) == 44
38 def double(self, val):
42 assert m.test_callback3(z.double) ==
"func(43) = 86"
44 z = m.CppBoundMethodTest()
45 assert m.test_callback3(z.triple) ==
"func(43) = 129"
49 def f(*args, **kwargs):
52 assert m.test_tuple_unpacking(f) == ((
"positional", 1, 2, 3, 4, 5, 6), {})
53 assert m.test_dict_unpacking(f) == (
55 {
"key":
"value",
"a": 1,
"b": 2},
57 assert m.test_keyword_args(f) == ((), {
"x": 10,
"y": 20})
58 assert m.test_unpacking_and_keywords1(f) == ((1, 2), {
"c": 3,
"d": 4})
59 assert m.test_unpacking_and_keywords2(f) == (
60 (
"positional", 1, 2, 3, 4, 5),
61 {
"key":
"value",
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5},
64 with pytest.raises(TypeError)
as excinfo:
65 m.test_unpacking_error1(f)
66 assert "Got multiple values for keyword argument" in str(excinfo.value)
68 with pytest.raises(TypeError)
as excinfo:
69 m.test_unpacking_error2(f)
70 assert "Got multiple values for keyword argument" in str(excinfo.value)
72 with pytest.raises(RuntimeError)
as excinfo:
73 m.test_arg_conversion_error1(f)
74 assert str(excinfo.value) ==
"Unable to convert call argument " + (
75 "'1' of type 'UnregisteredType' to Python object"
76 if detailed_error_messages_enabled
77 else "'1' to Python object (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)"
80 with pytest.raises(RuntimeError)
as excinfo:
81 m.test_arg_conversion_error2(f)
82 assert str(excinfo.value) ==
"Unable to convert call argument " + (
83 "'expected_name' of type 'UnregisteredType' to Python object"
84 if detailed_error_messages_enabled
85 else "'expected_name' to Python object "
86 "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)"
91 m.test_lambda_closure_cleanup()
92 cstats = m.payload_cstats()
93 assert cstats.alive() == 0
94 assert cstats.copy_constructions == 1
95 assert cstats.move_constructions >= 1
99 alive_counts = m.test_cpp_callable_cleanup()
100 assert alive_counts == [0, 1, 2, 1, 2, 1, 0]
104 """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
107 m.test_dummy_function(m.dummy_function) ==
"matches dummy_function: eval(1) = 2"
110 m.test_dummy_function(m.roundtrip(m.dummy_function))
111 ==
"matches dummy_function: eval(1) = 2"
114 m.test_dummy_function(m.dummy_function_overloaded)
115 ==
"matches dummy_function: eval(1) = 2"
117 assert m.roundtrip(
None, expect_none=
True)
is None
119 m.test_dummy_function(
lambda x: x + 2)
120 ==
"can't convert to function pointer: eval(1) = 3"
123 with pytest.raises(TypeError)
as excinfo:
124 m.test_dummy_function(m.dummy_function2)
125 assert "incompatible function arguments" in str(excinfo.value)
127 with pytest.raises(TypeError)
as excinfo:
128 m.test_dummy_function(
lambda x, y: x + y)
130 s
in str(excinfo.value)
131 for s
in (
"missing 1 required positional argument",
"takes exactly 2 arguments")
136 assert doc(m.test_callback3) ==
"test_callback3(arg0: Callable[[int], int]) -> str"
137 assert doc(m.test_callback4) ==
"test_callback4() -> Callable[[int], int]"
141 assert m.callback_with_movable(
lambda _:
None)
is True
146 reason=
"PyPy segfaults on here. See discussion on #1413.",
149 """Test if python builtins like sum() can be used as callbacks"""
150 assert m.test_sum_builtin(sum, [1, 2, 3]) == 6
151 assert m.test_sum_builtin(sum, []) == 0
165 return lambda j: res.append(s.value + j)
169 m.test_async_callback(gen_f(), work)
171 from time
import sleep
174 assert sum(res) == sum(x + 3
for x
in work)
178 t = Thread(target=test_async_callbacks)
188 one_million = 1000000
192 for rep
in range(repeats):
194 m.callback_num_times(
lambda:
None, num_millions * one_million)
195 td = time.time() - t0
196 rate = num_millions / td
if td
else 0
201 f
"callback_num_times: {num_millions:d} million / {td:.3f} seconds = {rate:.3f} million / second"
204 print(
"Min Mean Max")
205 print(f
"{min(rates):6.3f} {sum(rates) / len(rates):6.3f} {max(rates):6.3f}")
209 assert m.custom_function(4) == 36
210 assert m.roundtrip(m.custom_function)(4) == 36
214 m.custom_function2
is None, reason=
"Current PYBIND11_INTERNALS_VERSION too low"
217 assert m.custom_function2(3) == 27
218 assert m.roundtrip(m.custom_function2)(3) == 27