1 /*
2 tests/test_callbacks.cpp -- callbacks
3
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8 */
9
10 #include "pybind11_tests.h"
11 #include "constructor_stats.h"
12 #include <pybind11/functional.h>
13 #include <thread>
14
15
dummy_function(int i)16 int dummy_function(int i) { return i + 1; }
17
TEST_SUBMODULE(callbacks,m)18 TEST_SUBMODULE(callbacks, m) {
19 // test_callbacks, test_function_signatures
20 m.def("test_callback1", [](const py::object &func) { return func(); });
21 m.def("test_callback2", [](const py::object &func) { return func("Hello", 'x', true, 5); });
22 m.def("test_callback3", [](const std::function<int(int)> &func) {
23 return "func(43) = " + std::to_string(func(43)); });
24 m.def("test_callback4", []() -> std::function<int(int)> { return [](int i) { return i+1; }; });
25 m.def("test_callback5", []() {
26 return py::cpp_function([](int i) { return i+1; }, py::arg("number"));
27 });
28
29 // test_keyword_args_and_generalized_unpacking
30 m.def("test_tuple_unpacking", [](const py::function &f) {
31 auto t1 = py::make_tuple(2, 3);
32 auto t2 = py::make_tuple(5, 6);
33 return f("positional", 1, *t1, 4, *t2);
34 });
35
36 m.def("test_dict_unpacking", [](const py::function &f) {
37 auto d1 = py::dict("key"_a="value", "a"_a=1);
38 auto d2 = py::dict();
39 auto d3 = py::dict("b"_a=2);
40 return f("positional", 1, **d1, **d2, **d3);
41 });
42
43 m.def("test_keyword_args", [](const py::function &f) { return f("x"_a = 10, "y"_a = 20); });
44
45 m.def("test_unpacking_and_keywords1", [](const py::function &f) {
46 auto args = py::make_tuple(2);
47 auto kwargs = py::dict("d"_a=4);
48 return f(1, *args, "c"_a=3, **kwargs);
49 });
50
51 m.def("test_unpacking_and_keywords2", [](const py::function &f) {
52 auto kwargs1 = py::dict("a"_a=1);
53 auto kwargs2 = py::dict("c"_a=3, "d"_a=4);
54 return f("positional", *py::make_tuple(1), 2, *py::make_tuple(3, 4), 5,
55 "key"_a="value", **kwargs1, "b"_a=2, **kwargs2, "e"_a=5);
56 });
57
58 m.def("test_unpacking_error1", [](const py::function &f) {
59 auto kwargs = py::dict("x"_a=3);
60 return f("x"_a=1, "y"_a=2, **kwargs); // duplicate ** after keyword
61 });
62
63 m.def("test_unpacking_error2", [](const py::function &f) {
64 auto kwargs = py::dict("x"_a=3);
65 return f(**kwargs, "x"_a=1); // duplicate keyword after **
66 });
67
68 m.def("test_arg_conversion_error1",
69 [](const py::function &f) { f(234, UnregisteredType(), "kw"_a = 567); });
70
71 m.def("test_arg_conversion_error2", [](const py::function &f) {
72 f(234, "expected_name"_a=UnregisteredType(), "kw"_a=567);
73 });
74
75 // test_lambda_closure_cleanup
76 struct Payload {
77 Payload() { print_default_created(this); }
78 ~Payload() { print_destroyed(this); }
79 Payload(const Payload &) { print_copy_created(this); }
80 Payload(Payload &&) noexcept { print_move_created(this); }
81 };
82 // Export the payload constructor statistics for testing purposes:
83 m.def("payload_cstats", &ConstructorStats::get<Payload>);
84 m.def("test_lambda_closure_cleanup", []() -> std::function<void()> {
85 Payload p;
86
87 // In this situation, `Func` in the implementation of
88 // `cpp_function::initialize` is NOT trivially destructible.
89 return [p]() {
90 /* p should be cleaned up when the returned function is garbage collected */
91 (void) p;
92 };
93 });
94
95 class CppCallable {
96 public:
97 CppCallable() { track_default_created(this); }
98 ~CppCallable() { track_destroyed(this); }
99 CppCallable(const CppCallable &) { track_copy_created(this); }
100 CppCallable(CppCallable &&) noexcept { track_move_created(this); }
101 void operator()() {}
102 };
103
104 m.def("test_cpp_callable_cleanup", []() {
105 // Related issue: https://github.com/pybind/pybind11/issues/3228
106 // Related PR: https://github.com/pybind/pybind11/pull/3229
107 py::list alive_counts;
108 ConstructorStats &stat = ConstructorStats::get<CppCallable>();
109 alive_counts.append(stat.alive());
110 {
111 CppCallable cpp_callable;
112 alive_counts.append(stat.alive());
113 {
114 // In this situation, `Func` in the implementation of
115 // `cpp_function::initialize` IS trivially destructible,
116 // only `capture` is not.
117 py::cpp_function py_func(cpp_callable);
118 py::detail::silence_unused_warnings(py_func);
119 alive_counts.append(stat.alive());
120 }
121 alive_counts.append(stat.alive());
122 {
123 py::cpp_function py_func(std::move(cpp_callable));
124 py::detail::silence_unused_warnings(py_func);
125 alive_counts.append(stat.alive());
126 }
127 alive_counts.append(stat.alive());
128 }
129 alive_counts.append(stat.alive());
130 return alive_counts;
131 });
132
133 // test_cpp_function_roundtrip
134 /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
135 m.def("dummy_function", &dummy_function);
136 m.def("dummy_function_overloaded", [](int i, int j) { return i + j; });
137 m.def("dummy_function_overloaded", &dummy_function);
138 m.def("dummy_function2", [](int i, int j) { return i + j; });
139 m.def("roundtrip", [](std::function<int(int)> f, bool expect_none = false) {
140 if (expect_none && f)
141 throw std::runtime_error("Expected None to be converted to empty std::function");
142 return f;
143 }, py::arg("f"), py::arg("expect_none")=false);
144 m.def("test_dummy_function", [](const std::function<int(int)> &f) -> std::string {
145 using fn_type = int (*)(int);
146 auto result = f.target<fn_type>();
147 if (!result) {
148 auto r = f(1);
149 return "can't convert to function pointer: eval(1) = " + std::to_string(r);
150 }
151 if (*result == dummy_function) {
152 auto r = (*result)(1);
153 return "matches dummy_function: eval(1) = " + std::to_string(r);
154 }
155 return "argument does NOT match dummy_function. This should never happen!";
156
157 });
158
159 class AbstractBase {
160 public:
161 // [workaround(intel)] = default does not work here
162 // Defaulting this destructor results in linking errors with the Intel compiler
163 // (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827)
164 virtual ~AbstractBase() {} // NOLINT(modernize-use-equals-default)
165 virtual unsigned int func() = 0;
166 };
167 m.def("func_accepting_func_accepting_base",
168 [](const std::function<double(AbstractBase &)> &) {});
169
170 struct MovableObject {
171 bool valid = true;
172
173 MovableObject() = default;
174 MovableObject(const MovableObject &) = default;
175 MovableObject &operator=(const MovableObject &) = default;
176 MovableObject(MovableObject &&o) noexcept : valid(o.valid) { o.valid = false; }
177 MovableObject &operator=(MovableObject &&o) noexcept {
178 valid = o.valid;
179 o.valid = false;
180 return *this;
181 }
182 };
183 py::class_<MovableObject>(m, "MovableObject");
184
185 // test_movable_object
186 m.def("callback_with_movable", [](const std::function<void(MovableObject &)> &f) {
187 auto x = MovableObject();
188 f(x); // lvalue reference shouldn't move out object
189 return x.valid; // must still return `true`
190 });
191
192 // test_bound_method_callback
193 struct CppBoundMethodTest {};
194 py::class_<CppBoundMethodTest>(m, "CppBoundMethodTest")
195 .def(py::init<>())
196 .def("triple", [](CppBoundMethodTest &, int val) { return 3 * val; });
197
198 // This checks that builtin functions can be passed as callbacks
199 // rather than throwing RuntimeError due to trying to extract as capsule
200 m.def("test_sum_builtin", [](const std::function<double(py::iterable)> &sum_builtin, const py::iterable &i) {
201 return sum_builtin(i);
202 });
203
204 // test async Python callbacks
205 using callback_f = std::function<void(int)>;
206 m.def("test_async_callback", [](const callback_f &f, const py::list &work) {
207 // make detached thread that calls `f` with piece of work after a little delay
208 auto start_f = [f](int j) {
209 auto invoke_f = [f, j] {
210 std::this_thread::sleep_for(std::chrono::milliseconds(50));
211 f(j);
212 };
213 auto t = std::thread(std::move(invoke_f));
214 t.detach();
215 };
216
217 // spawn worker threads
218 for (auto i : work)
219 start_f(py::cast<int>(i));
220 });
221
222 m.def("callback_num_times", [](const py::function &f, std::size_t num) {
223 for (std::size_t i = 0; i < num; i++) {
224 f();
225 }
226 });
227 }
228