Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 4 | def test_callbacks(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 5 | 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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 10 | return "func1" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 11 | |
| 12 | def func2(a, b, c, d): |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 13 | return "func2", a, b, c, d |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 14 | |
| 15 | def func3(a): |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 16 | return "func3({})".format(a) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 17 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 18 | 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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 23 | |
| 24 | f = test_callback4() |
| 25 | assert f(43) == 44 |
| 26 | f = test_callback5() |
| 27 | assert f(number=43) == 44 |
| 28 | |
| 29 | |
Jason Rhinelander | a01b6b8 | 2017-04-24 12:29:42 -0400 | [diff] [blame] | 30 | def 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 Moldovan | c743e1b | 2016-08-29 03:05:42 +0200 | [diff] [blame] | 45 | def 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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 80 | def 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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 90 | def test_cpp_function_roundtrip(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 91 | """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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 94 | 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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 98 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 99 | with pytest.raises(TypeError) as excinfo: |
| 100 | test_dummy_function(dummy_function2) |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 101 | assert "incompatible function arguments" in str(excinfo.value) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 102 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 103 | 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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def test_function_signatures(doc): |
| 110 | from pybind11_tests import test_callback3, test_callback4 |
| 111 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 112 | assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 113 | assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]" |
Lunderberg | c7fcde7 | 2017-02-22 14:00:59 -0500 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def test_movable_object(): |
| 117 | from pybind11_tests import callback_with_movable |
| 118 | |
| 119 | assert callback_with_movable(lambda _: None) is True |