| 17 | int dummy_function(int i) { return i + 1; } |
| 18 | |
| 19 | TEST_SUBMODULE(callbacks, m) { |
| 20 | // test_callbacks, test_function_signatures |
| 21 | m.def("test_callback1", [](const py::object &func) { return func(); }); |
| 22 | m.def("test_callback2", [](const py::object &func) { return func("Hello", 'x', true, 5); }); |
| 23 | m.def("test_callback3", [](const std::function<int(int)> &func) { |
| 24 | return "func(43) = " + std::to_string(func(43)); |
| 25 | }); |
| 26 | m.def("test_callback4", |
| 27 | []() -> std::function<int(int)> { return [](int i) { return i + 1; }; }); |
| 28 | m.def("test_callback5", |
| 29 | []() { return py::cpp_function([](int i) { return i + 1; }, py::arg("number")); }); |
| 30 | |
| 31 | // test_keyword_args_and_generalized_unpacking |
| 32 | m.def("test_tuple_unpacking", [](const py::function &f) { |
| 33 | auto t1 = py::make_tuple(2, 3); |
| 34 | auto t2 = py::make_tuple(5, 6); |
| 35 | return f("positional", 1, *t1, 4, *t2); |
| 36 | }); |
| 37 | |
| 38 | m.def("test_dict_unpacking", [](const py::function &f) { |
| 39 | auto d1 = py::dict("key"_a = "value", "a"_a = 1); |
| 40 | auto d2 = py::dict(); |
| 41 | auto d3 = py::dict("b"_a = 2); |
| 42 | return f("positional", 1, **d1, **d2, **d3); |
| 43 | }); |
| 44 | |
| 45 | m.def("test_keyword_args", [](const py::function &f) { return f("x"_a = 10, "y"_a = 20); }); |
| 46 | |
| 47 | m.def("test_unpacking_and_keywords1", [](const py::function &f) { |
| 48 | auto args = py::make_tuple(2); |
| 49 | auto kwargs = py::dict("d"_a = 4); |
| 50 | return f(1, *args, "c"_a = 3, **kwargs); |
| 51 | }); |
| 52 | |
| 53 | m.def("test_unpacking_and_keywords2", [](const py::function &f) { |
| 54 | auto kwargs1 = py::dict("a"_a = 1); |
| 55 | auto kwargs2 = py::dict("c"_a = 3, "d"_a = 4); |
| 56 | return f("positional", |
| 57 | *py::make_tuple(1), |
| 58 | 2, |
| 59 | *py::make_tuple(3, 4), |
| 60 | 5, |
| 61 | "key"_a = "value", |
| 62 | **kwargs1, |
| 63 | "b"_a = 2, |
| 64 | **kwargs2, |
| 65 | "e"_a = 5); |
| 66 | }); |
| 67 | |
| 68 | m.def("test_unpacking_error1", [](const py::function &f) { |
| 69 | auto kwargs = py::dict("x"_a = 3); |
| 70 | return f("x"_a = 1, "y"_a = 2, **kwargs); // duplicate ** after keyword |
| 71 | }); |
| 72 | |
| 73 | m.def("test_unpacking_error2", [](const py::function &f) { |
| 74 | auto kwargs = py::dict("x"_a = 3); |
| 75 | return f(**kwargs, "x"_a = 1); // duplicate keyword after ** |
| 76 | }); |
nothing calls this directly
no test coverage detected