MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_cpp_function_roundtrip

Function test_cpp_function_roundtrip

tests/test_callbacks.py:108–137  ·  view source on GitHub ↗

Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer

()

Source from the content-addressed store, hash-verified

106
107
108def 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
140def test_function_signatures(doc):

Callers

nothing calls this directly

Calls 1

strClass · 0.85

Tested by

no test coverage detected