20 m.def(
"test_callback1", [](py::object
func) {
return func(); });
21 m.def(
"test_callback2", [](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"));
30 m.def(
"test_tuple_unpacking", [](py::function
f) {
33 return f(
"positional", 1, *t1, 4, *t2);
36 m.def(
"test_dict_unpacking", [](py::function f) {
37 auto d1 = py::dict(
"key"_a=
"value",
"a"_a=1);
39 auto d3 = py::dict(
"b"_a=2);
40 return f(
"positional", 1, **d1, **
d2, **d3);
43 m.def(
"test_keyword_args", [](py::function f) {
44 return f(
"x"_a=10,
"y"_a=20);
47 m.def(
"test_unpacking_and_keywords1", [](py::function f) {
49 auto kwargs = py::dict(
"d"_a=4);
53 m.def(
"test_unpacking_and_keywords2", [](py::function f) {
54 auto kwargs1 = py::dict(
"a"_a=1);
55 auto kwargs2 = py::dict(
"c"_a=3,
"d"_a=4);
57 "key"_a=
"value", **kwargs1,
"b"_a=2, **kwargs2,
"e"_a=5);
60 m.def(
"test_unpacking_error1", [](py::function f) {
61 auto kwargs = py::dict(
"x"_a=3);
62 return f(
"x"_a=1,
"y"_a=2, **
kwargs);
65 m.def(
"test_unpacking_error2", [](py::function f) {
66 auto kwargs = py::dict(
"x"_a=3);
70 m.def(
"test_arg_conversion_error1", [](py::function f) {
74 m.def(
"test_arg_conversion_error2", [](py::function f) {
86 m.def(
"payload_cstats", &ConstructorStats::get<Payload>);
88 m.def(
"test_cleanup", []() -> std::function<
void(
void)> {
100 m.def(
"dummy_function2", [](
int i,
int j) {
return i +
j; });
101 m.def(
"roundtrip", [](std::function<
int(
int)> f,
bool expect_none =
false) {
102 if (expect_none && f)
103 throw std::runtime_error(
"Expected None to be converted to empty std::function");
106 m.def(
"test_dummy_function", [](
const std::function<
int(
int)> &f) -> std::string {
107 using fn_type =
int (*)(
int);
108 auto result = f.target<fn_type>();
111 return "can't convert to function pointer: eval(1) = " + std::to_string(r);
113 auto r = (*result)(1);
114 return "matches dummy_function: eval(1) = " + std::to_string(r);
116 return "argument does NOT match dummy_function. This should never happen!";
120 class AbstractBase {
public:
virtual unsigned int func() = 0; };
121 m.def(
"func_accepting_func_accepting_base", [](std::function<
double(AbstractBase&)>) { });
123 struct MovableObject {
126 MovableObject() =
default;
127 MovableObject(
const MovableObject &) =
default;
128 MovableObject &operator=(
const MovableObject &) =
default;
129 MovableObject(MovableObject &&o) : valid(o.valid) { o.valid =
false; }
130 MovableObject &operator=(MovableObject &&o) {
136 py::class_<MovableObject>(
m,
"MovableObject");
139 m.def(
"callback_with_movable", [](std::function<
void(MovableObject &)> f) {
140 auto x = MovableObject();
146 struct CppBoundMethodTest {};
147 py::class_<CppBoundMethodTest>(
m,
"CppBoundMethodTest")
149 .def(
"triple", [](CppBoundMethodTest &,
int val) {
return 3 * val; });
152 using callback_f = std::function<void(int)>;
153 m.def(
"test_async_callback", [](callback_f f, py::list work) {
155 auto start_f = [
f](
int j) {
156 auto invoke_f = [
f,
j] {
157 std::this_thread::sleep_for(std::chrono::milliseconds(50));
160 auto t = std::thread(std::move(invoke_f));
166 start_f(py::cast<int>(i));
void print_destroyed(T *inst, Values &&...values)
void print_copy_created(T *inst, Values &&...values)
Dummy type which is not exported anywhere – something to trigger a conversion error.
void print_default_created(T *inst, Values &&...values)
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
int dummy_function(int i)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
TEST_SUBMODULE(callbacks, m)
void print_move_created(T *inst, Values &&...values)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x