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

Function TEST_SUBMODULE

tests/test_callbacks.cpp:45–302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43int dummy_function(int i) { return i + 1; }
44
45TEST_SUBMODULE(callbacks, m) {
46 // test_callbacks, test_function_signatures
47 m.def("test_callback1", [](const py::object &func) { return func(); });
48 m.def("test_callback2", [](const py::object &func) { return func("Hello", 'x', true, 5); });
49 m.def("test_callback3", [](const std::function<int(int)> &func) {
50 return "func(43) = " + std::to_string(func(43));
51 });
52 m.def("test_callback4",
53 []() -> std::function<int(int)> { return [](int i) { return i + 1; }; });
54 m.def("test_callback5",
55 []() { return py::cpp_function([](int i) { return i + 1; }, py::arg("number")); });
56
57 // test_keyword_args_and_generalized_unpacking
58 m.def("test_tuple_unpacking", [](const py::function &f) {
59 auto t1 = py::make_tuple(2, 3);
60 auto t2 = py::make_tuple(5, 6);
61 return f("positional", 1, *t1, 4, *t2);
62 });
63
64 m.def("test_dict_unpacking", [](const py::function &f) {
65 auto d1 = py::dict("key"_a = "value", "a"_a = 1);
66 auto d2 = py::dict();
67 auto d3 = py::dict("b"_a = 2);
68 return f("positional", 1, **d1, **d2, **d3);
69 });
70
71 m.def("test_keyword_args", [](const py::function &f) { return f("x"_a = 10, "y"_a = 20); });
72
73 m.def("test_unpacking_and_keywords1", [](const py::function &f) {
74 auto args = py::make_tuple(2);
75 auto kwargs = py::dict("d"_a = 4);
76 return f(1, *args, "c"_a = 3, **kwargs);
77 });
78
79 m.def("test_unpacking_and_keywords2", [](const py::function &f) {
80 auto kwargs1 = py::dict("a"_a = 1);
81 auto kwargs2 = py::dict("c"_a = 3, "d"_a = 4);
82 return f("positional",
83 *py::make_tuple(1),
84 2,
85 *py::make_tuple(3, 4),
86 5,
87 "key"_a = "value",
88 **kwargs1,
89 "b"_a = 2,
90 **kwargs2,
91 "e"_a = 5);
92 });
93
94 m.def("test_unpacking_error1", [](const py::function &f) {
95 auto kwargs = py::dict("x"_a = 3);
96 return f("x"_a = 1, "y"_a = 2, **kwargs); // duplicate ** after keyword
97 });
98
99 m.def("test_unpacking_error2", [](const py::function &f) {
100 auto kwargs = py::dict("x"_a = 3);
101 return f(**kwargs, "x"_a = 1); // duplicate keyword after **
102 });

Callers

nothing calls this directly

Calls 15

cpp_functionClass · 0.85
make_tupleFunction · 0.85
fFunction · 0.85
dictClass · 0.85
UnregisteredTypeClass · 0.85
silence_unused_warningsFunction · 0.85
moveFunction · 0.85
MovableObjectClass · 0.85
aliveMethod · 0.80
ptrMethod · 0.80
releaseMethod · 0.80
funcFunction · 0.70

Tested by

no test coverage detected