File indexing completed on 2025-01-18 10:17:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <pybind11/functional.h>
0011
0012 #include "constructor_stats.h"
0013 #include "pybind11_tests.h"
0014
0015 #include <thread>
0016
0017 int dummy_function(int i) { return i + 1; }
0018
0019 TEST_SUBMODULE(callbacks, m) {
0020
0021 m.def("test_callback1", [](const py::object &func) { return func(); });
0022 m.def("test_callback2", [](const py::object &func) { return func("Hello", 'x', true, 5); });
0023 m.def("test_callback3", [](const std::function<int(int)> &func) {
0024 return "func(43) = " + std::to_string(func(43));
0025 });
0026 m.def("test_callback4",
0027 []() -> std::function<int(int)> { return [](int i) { return i + 1; }; });
0028 m.def("test_callback5",
0029 []() { return py::cpp_function([](int i) { return i + 1; }, py::arg("number")); });
0030
0031
0032 m.def("test_tuple_unpacking", [](const py::function &f) {
0033 auto t1 = py::make_tuple(2, 3);
0034 auto t2 = py::make_tuple(5, 6);
0035 return f("positional", 1, *t1, 4, *t2);
0036 });
0037
0038 m.def("test_dict_unpacking", [](const py::function &f) {
0039 auto d1 = py::dict("key"_a = "value", "a"_a = 1);
0040 auto d2 = py::dict();
0041 auto d3 = py::dict("b"_a = 2);
0042 return f("positional", 1, **d1, **d2, **d3);
0043 });
0044
0045 m.def("test_keyword_args", [](const py::function &f) { return f("x"_a = 10, "y"_a = 20); });
0046
0047 m.def("test_unpacking_and_keywords1", [](const py::function &f) {
0048 auto args = py::make_tuple(2);
0049 auto kwargs = py::dict("d"_a = 4);
0050 return f(1, *args, "c"_a = 3, **kwargs);
0051 });
0052
0053 m.def("test_unpacking_and_keywords2", [](const py::function &f) {
0054 auto kwargs1 = py::dict("a"_a = 1);
0055 auto kwargs2 = py::dict("c"_a = 3, "d"_a = 4);
0056 return f("positional",
0057 *py::make_tuple(1),
0058 2,
0059 *py::make_tuple(3, 4),
0060 5,
0061 "key"_a = "value",
0062 **kwargs1,
0063 "b"_a = 2,
0064 **kwargs2,
0065 "e"_a = 5);
0066 });
0067
0068 m.def("test_unpacking_error1", [](const py::function &f) {
0069 auto kwargs = py::dict("x"_a = 3);
0070 return f("x"_a = 1, "y"_a = 2, **kwargs);
0071 });
0072
0073 m.def("test_unpacking_error2", [](const py::function &f) {
0074 auto kwargs = py::dict("x"_a = 3);
0075 return f(**kwargs, "x"_a = 1);
0076 });
0077
0078 m.def("test_arg_conversion_error1",
0079 [](const py::function &f) { f(234, UnregisteredType(), "kw"_a = 567); });
0080
0081 m.def("test_arg_conversion_error2", [](const py::function &f) {
0082 f(234, "expected_name"_a = UnregisteredType(), "kw"_a = 567);
0083 });
0084
0085
0086 struct Payload {
0087 Payload() { print_default_created(this); }
0088 ~Payload() { print_destroyed(this); }
0089 Payload(const Payload &) { print_copy_created(this); }
0090 Payload(Payload &&) noexcept { print_move_created(this); }
0091 };
0092
0093 m.def("payload_cstats", &ConstructorStats::get<Payload>);
0094 m.def("test_lambda_closure_cleanup", []() -> std::function<void()> {
0095 Payload p;
0096
0097
0098
0099 return [p]() {
0100
0101 (void) p;
0102 };
0103 });
0104
0105 class CppCallable {
0106 public:
0107 CppCallable() { track_default_created(this); }
0108 ~CppCallable() { track_destroyed(this); }
0109 CppCallable(const CppCallable &) { track_copy_created(this); }
0110 CppCallable(CppCallable &&) noexcept { track_move_created(this); }
0111 void operator()() {}
0112 };
0113
0114 m.def("test_cpp_callable_cleanup", []() {
0115
0116
0117 py::list alive_counts;
0118 ConstructorStats &stat = ConstructorStats::get<CppCallable>();
0119 alive_counts.append(stat.alive());
0120 {
0121 CppCallable cpp_callable;
0122 alive_counts.append(stat.alive());
0123 {
0124
0125
0126
0127 py::cpp_function py_func(cpp_callable);
0128 py::detail::silence_unused_warnings(py_func);
0129 alive_counts.append(stat.alive());
0130 }
0131 alive_counts.append(stat.alive());
0132 {
0133 py::cpp_function py_func(std::move(cpp_callable));
0134 py::detail::silence_unused_warnings(py_func);
0135 alive_counts.append(stat.alive());
0136 }
0137 alive_counts.append(stat.alive());
0138 }
0139 alive_counts.append(stat.alive());
0140 return alive_counts;
0141 });
0142
0143
0144
0145 m.def("dummy_function", &dummy_function);
0146 m.def("dummy_function_overloaded", [](int i, int j) { return i + j; });
0147 m.def("dummy_function_overloaded", &dummy_function);
0148 m.def("dummy_function2", [](int i, int j) { return i + j; });
0149 m.def(
0150 "roundtrip",
0151 [](std::function<int(int)> f, bool expect_none = false) {
0152 if (expect_none && f) {
0153 throw std::runtime_error("Expected None to be converted to empty std::function");
0154 }
0155 return f;
0156 },
0157 py::arg("f"),
0158 py::arg("expect_none") = false);
0159 m.def("test_dummy_function", [](const std::function<int(int)> &f) -> std::string {
0160 using fn_type = int (*)(int);
0161 const auto *result = f.target<fn_type>();
0162 if (!result) {
0163 auto r = f(1);
0164 return "can't convert to function pointer: eval(1) = " + std::to_string(r);
0165 }
0166 if (*result == dummy_function) {
0167 auto r = (*result)(1);
0168 return "matches dummy_function: eval(1) = " + std::to_string(r);
0169 }
0170 return "argument does NOT match dummy_function. This should never happen!";
0171 });
0172
0173 class AbstractBase {
0174 public:
0175
0176
0177
0178 virtual ~AbstractBase() {}
0179 virtual unsigned int func() = 0;
0180 };
0181 m.def("func_accepting_func_accepting_base",
0182 [](const std::function<double(AbstractBase &)> &) {});
0183
0184 struct MovableObject {
0185 bool valid = true;
0186
0187 MovableObject() = default;
0188 MovableObject(const MovableObject &) = default;
0189 MovableObject &operator=(const MovableObject &) = default;
0190 MovableObject(MovableObject &&o) noexcept : valid(o.valid) { o.valid = false; }
0191 MovableObject &operator=(MovableObject &&o) noexcept {
0192 valid = o.valid;
0193 o.valid = false;
0194 return *this;
0195 }
0196 };
0197 py::class_<MovableObject>(m, "MovableObject");
0198
0199
0200 m.def("callback_with_movable", [](const std::function<void(MovableObject &)> &f) {
0201 auto x = MovableObject();
0202 f(x);
0203 return x.valid;
0204 });
0205
0206
0207 struct CppBoundMethodTest {};
0208 py::class_<CppBoundMethodTest>(m, "CppBoundMethodTest")
0209 .def(py::init<>())
0210 .def("triple", [](CppBoundMethodTest &, int val) { return 3 * val; });
0211
0212
0213
0214 m.def("test_sum_builtin",
0215 [](const std::function<double(py::iterable)> &sum_builtin, const py::iterable &i) {
0216 return sum_builtin(i);
0217 });
0218
0219
0220 using callback_f = std::function<void(int)>;
0221 m.def("test_async_callback", [](const callback_f &f, const py::list &work) {
0222
0223 auto start_f = [f](int j) {
0224 auto invoke_f = [f, j] {
0225 std::this_thread::sleep_for(std::chrono::milliseconds(50));
0226 f(j);
0227 };
0228 auto t = std::thread(std::move(invoke_f));
0229 t.detach();
0230 };
0231
0232
0233 for (auto i : work) {
0234 start_f(py::cast<int>(i));
0235 }
0236 });
0237
0238 m.def("callback_num_times", [](const py::function &f, std::size_t num) {
0239 for (std::size_t i = 0; i < num; i++) {
0240 f();
0241 }
0242 });
0243
0244 auto *custom_def = []() {
0245 static PyMethodDef def;
0246 def.ml_name = "example_name";
0247 def.ml_doc = "Example doc";
0248 def.ml_meth = [](PyObject *, PyObject *args) -> PyObject * {
0249 if (PyTuple_Size(args) != 1) {
0250 throw std::runtime_error("Invalid number of arguments for example_name");
0251 }
0252 PyObject *first = PyTuple_GetItem(args, 0);
0253 if (!PyLong_Check(first)) {
0254 throw std::runtime_error("Invalid argument to example_name");
0255 }
0256 auto result = py::cast(PyLong_AsLong(first) * 9);
0257 return result.release().ptr();
0258 };
0259 def.ml_flags = METH_VARARGS;
0260 return &def;
0261 }();
0262
0263
0264
0265
0266 constexpr const char *rec_capsule_name
0267 = pybind11::detail::internals_function_record_capsule_name;
0268 py::capsule rec_capsule(std::malloc(1), [](void *data) { std::free(data); });
0269 rec_capsule.set_name(rec_capsule_name);
0270 m.add_object("custom_function", PyCFunction_New(custom_def, rec_capsule.ptr()));
0271
0272
0273 #if PYBIND11_INTERNALS_VERSION > 4
0274
0275 py::capsule rec_capsule2(std::malloc(1), [](void *data) { std::free(data); });
0276 m.add_object("custom_function2", PyCFunction_New(custom_def, rec_capsule2.ptr()));
0277 #else
0278 m.add_object("custom_function2", py::none());
0279 #endif
0280 }