Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer
()
| 106 | |
| 107 | |
| 108 | def test_cpp_function_roundtrip(): |
| 109 | """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer""" |
| 110 | |
| 111 | assert ( |
| 112 | m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2" |
| 113 | ) |
| 114 | assert ( |
| 115 | m.test_dummy_function(m.roundtrip(m.dummy_function)) |
| 116 | == "matches dummy_function: eval(1) = 2" |
| 117 | ) |
| 118 | assert ( |
| 119 | m.test_dummy_function(m.dummy_function_overloaded) |
| 120 | == "matches dummy_function: eval(1) = 2" |
| 121 | ) |
| 122 | assert m.roundtrip(None, expect_none=True) is None |
| 123 | assert ( |
| 124 | m.test_dummy_function(lambda x: x + 2) |
| 125 | == "can't convert to function pointer: eval(1) = 3" |
| 126 | ) |
| 127 | |
| 128 | with pytest.raises(TypeError) as excinfo: |
| 129 | m.test_dummy_function(m.dummy_function2) |
| 130 | assert "incompatible function arguments" in str(excinfo.value) |
| 131 | |
| 132 | with pytest.raises(TypeError) as excinfo: |
| 133 | m.test_dummy_function(lambda x, y: x + y) |
| 134 | assert any( |
| 135 | s in str(excinfo.value) |
| 136 | for s in ("missing 1 required positional argument", "takes exactly 2 arguments") |
| 137 | ) |
| 138 | |
| 139 | |
| 140 | def test_function_signatures(doc): |