| 43 | int dummy_function(int i) { return i + 1; } |
| 44 | |
| 45 | TEST_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 | }); |
nothing calls this directly
no test coverage detected