blob: a5109d03c869edd5950187d5fcc5fb725063f758 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2
3
Dean Moldovan665e8802016-08-12 22:28:31 +02004def test_callbacks():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02005 from functools import partial
6 from pybind11_tests import (test_callback1, test_callback2, test_callback3,
7 test_callback4, test_callback5)
8
9 def func1():
Dean Moldovan665e8802016-08-12 22:28:31 +020010 return "func1"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020011
12 def func2(a, b, c, d):
Dean Moldovan665e8802016-08-12 22:28:31 +020013 return "func2", a, b, c, d
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020014
15 def func3(a):
Dean Moldovan665e8802016-08-12 22:28:31 +020016 return "func3({})".format(a)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020017
Dean Moldovan665e8802016-08-12 22:28:31 +020018 assert test_callback1(func1) == "func1"
19 assert test_callback2(func2) == ("func2", "Hello", "x", True, 5)
20 assert test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
21 assert test_callback1(partial(func3, "partial")) == "func3(partial)"
22 assert test_callback3(lambda i: i + 1) == "func(43) = 44"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020023
24 f = test_callback4()
25 assert f(43) == 44
26 f = test_callback5()
27 assert f(number=43) == 44
28
29
Jason Rhinelandera01b6b82017-04-24 12:29:42 -040030def test_bound_method_callback():
31 from pybind11_tests import test_callback3, CppBoundMethodTest
32
33 # Bound Python method:
34 class MyClass:
35 def double(self, val):
36 return 2 * val
37
38 z = MyClass()
39 assert test_callback3(z.double) == "func(43) = 86"
40
41 z = CppBoundMethodTest()
42 assert test_callback3(z.triple) == "func(43) = 129"
43
44
Dean Moldovanc743e1b2016-08-29 03:05:42 +020045def test_keyword_args_and_generalized_unpacking():
46 from pybind11_tests import (test_tuple_unpacking, test_dict_unpacking, test_keyword_args,
47 test_unpacking_and_keywords1, test_unpacking_and_keywords2,
48 test_unpacking_error1, test_unpacking_error2,
49 test_arg_conversion_error1, test_arg_conversion_error2)
50
51 def f(*args, **kwargs):
52 return args, kwargs
53
54 assert test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
55 assert test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
56 assert test_keyword_args(f) == ((), {"x": 10, "y": 20})
57 assert test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
58 assert test_unpacking_and_keywords2(f) == (
59 ("positional", 1, 2, 3, 4, 5),
60 {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
61 )
62
63 with pytest.raises(TypeError) as excinfo:
64 test_unpacking_error1(f)
65 assert "Got multiple values for keyword argument" in str(excinfo.value)
66
67 with pytest.raises(TypeError) as excinfo:
68 test_unpacking_error2(f)
69 assert "Got multiple values for keyword argument" in str(excinfo.value)
70
71 with pytest.raises(RuntimeError) as excinfo:
72 test_arg_conversion_error1(f)
73 assert "Unable to convert call argument" in str(excinfo.value)
74
75 with pytest.raises(RuntimeError) as excinfo:
76 test_arg_conversion_error2(f)
77 assert "Unable to convert call argument" in str(excinfo.value)
78
79
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020080def test_lambda_closure_cleanup():
81 from pybind11_tests import test_cleanup, payload_cstats
82
83 test_cleanup()
84 cstats = payload_cstats()
85 assert cstats.alive() == 0
86 assert cstats.copy_constructions == 1
87 assert cstats.move_constructions >= 1
88
89
Dean Moldovan665e8802016-08-12 22:28:31 +020090def test_cpp_function_roundtrip():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020091 """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
92 from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip
93
Dean Moldovan665e8802016-08-12 22:28:31 +020094 assert test_dummy_function(dummy_function) == "matches dummy_function: eval(1) = 2"
95 assert test_dummy_function(roundtrip(dummy_function)) == "matches dummy_function: eval(1) = 2"
96 assert roundtrip(None, expect_none=True) is None
97 assert test_dummy_function(lambda x: x + 2) == "can't convert to function pointer: eval(1) = 3"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020098
Dean Moldovan665e8802016-08-12 22:28:31 +020099 with pytest.raises(TypeError) as excinfo:
100 test_dummy_function(dummy_function2)
Wenzel Jakobe99ebae2016-09-12 11:44:37 +0900101 assert "incompatible function arguments" in str(excinfo.value)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200102
Dean Moldovan665e8802016-08-12 22:28:31 +0200103 with pytest.raises(TypeError) as excinfo:
104 test_dummy_function(lambda x, y: x + y)
105 assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
106 "takes exactly 2 arguments"))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200107
108
109def test_function_signatures(doc):
110 from pybind11_tests import test_callback3, test_callback4
111
Dean Moldovan665e8802016-08-12 22:28:31 +0200112 assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200113 assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"
Lunderbergc7fcde72017-02-22 14:00:59 -0500114
115
116def test_movable_object():
117 from pybind11_tests import callback_with_movable
118
119 assert callback_with_movable(lambda _: None) is True