3 from pybind11_tests
import callbacks
as m
4 from threading
import Thread
8 from functools
import partial
13 def func2(a, b, c, d):
14 return "func2", a, b, c, d
17 return "func3({})".format(a)
19 assert m.test_callback1(func1) ==
"func1" 20 assert m.test_callback2(func2) == (
"func2",
"Hello",
"x",
True, 5)
21 assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == (
"func2", 1, 2, 3, 4)
22 assert m.test_callback1(partial(func3,
"partial")) ==
"func3(partial)" 23 assert m.test_callback3(
lambda i: i + 1) ==
"func(43) = 44" 25 f = m.test_callback4()
27 f = m.test_callback5()
28 assert f(number=43) == 44
34 def double(self, val):
38 assert m.test_callback3(z.double) ==
"func(43) = 86" 40 z = m.CppBoundMethodTest()
41 assert m.test_callback3(z.triple) ==
"func(43) = 129" 46 def f(*args, **kwargs):
49 assert m.test_tuple_unpacking(f) == ((
"positional", 1, 2, 3, 4, 5, 6), {})
50 assert m.test_dict_unpacking(f) == ((
"positional", 1), {
"key":
"value",
"a": 1,
"b": 2})
51 assert m.test_keyword_args(f) == ((), {
"x": 10,
"y": 20})
52 assert m.test_unpacking_and_keywords1(f) == ((1, 2), {
"c": 3,
"d": 4})
53 assert m.test_unpacking_and_keywords2(f) == (
54 (
"positional", 1, 2, 3, 4, 5),
55 {
"key":
"value",
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5}
58 with pytest.raises(TypeError)
as excinfo:
59 m.test_unpacking_error1(f)
60 assert "Got multiple values for keyword argument" in str(excinfo.value)
62 with pytest.raises(TypeError)
as excinfo:
63 m.test_unpacking_error2(f)
64 assert "Got multiple values for keyword argument" in str(excinfo.value)
66 with pytest.raises(RuntimeError)
as excinfo:
67 m.test_arg_conversion_error1(f)
68 assert "Unable to convert call argument" in str(excinfo.value)
70 with pytest.raises(RuntimeError)
as excinfo:
71 m.test_arg_conversion_error2(f)
72 assert "Unable to convert call argument" in str(excinfo.value)
77 cstats = m.payload_cstats()
78 assert cstats.alive() == 0
79 assert cstats.copy_constructions == 1
80 assert cstats.move_constructions >= 1
84 """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer""" 86 assert m.test_dummy_function(m.dummy_function) ==
"matches dummy_function: eval(1) = 2" 87 assert (m.test_dummy_function(m.roundtrip(m.dummy_function)) ==
88 "matches dummy_function: eval(1) = 2")
89 assert m.roundtrip(
None, expect_none=
True)
is None 90 assert (m.test_dummy_function(
lambda x: x + 2) ==
91 "can't convert to function pointer: eval(1) = 3")
93 with pytest.raises(TypeError)
as excinfo:
94 m.test_dummy_function(m.dummy_function2)
95 assert "incompatible function arguments" in str(excinfo.value)
97 with pytest.raises(TypeError)
as excinfo:
98 m.test_dummy_function(
lambda x, y: x + y)
99 assert any(s
in str(excinfo.value)
for s
in (
"missing 1 required positional argument",
100 "takes exactly 2 arguments"))
104 assert doc(m.test_callback3) ==
"test_callback3(arg0: Callable[[int], int]) -> str" 105 assert doc(m.test_callback4) ==
"test_callback4() -> Callable[[int], int]" 109 assert m.callback_with_movable(
lambda _:
None)
is True 115 def __init__(self, value):
123 return lambda j: res.append(s.value + j)
127 m.test_async_callback(gen_f(), work)
129 from time
import sleep
131 assert sum(res) ==
sum([x + 3
for x
in work])
135 t = Thread(target=test_async_callbacks)
Annotation for documentation.
def test_bound_method_callback()
def test_async_async_callbacks()
def test_lambda_closure_cleanup()
def test_keyword_args_and_generalized_unpacking()
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
def test_function_signatures(doc)
def test_movable_object()
def test_cpp_function_roundtrip()
def test_async_callbacks()