test_callbacks.cpp revision 12037:d28054ac6ec9
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 14 15py::object test_callback1(py::object func) { 16 return func(); 17} 18 19py::tuple test_callback2(py::object func) { 20 return func("Hello", 'x', true, 5); 21} 22 23std::string test_callback3(const std::function<int(int)> &func) { 24 return "func(43) = " + std::to_string(func(43)); 25} 26 27std::function<int(int)> test_callback4() { 28 return [](int i) { return i+1; }; 29} 30 31py::cpp_function test_callback5() { 32 return py::cpp_function([](int i) { return i+1; }, 33 py::arg("number")); 34} 35 36int dummy_function(int i) { return i + 1; } 37int dummy_function2(int i, int j) { return i + j; } 38std::function<int(int)> roundtrip(std::function<int(int)> f, bool expect_none = false) { 39 if (expect_none && f) { 40 throw std::runtime_error("Expected None to be converted to empty std::function"); 41 } 42 return f; 43} 44 45std::string test_dummy_function(const std::function<int(int)> &f) { 46 using fn_type = int (*)(int); 47 auto result = f.target<fn_type>(); 48 if (!result) { 49 auto r = f(1); 50 return "can't convert to function pointer: eval(1) = " + std::to_string(r); 51 } else if (*result == dummy_function) { 52 auto r = (*result)(1); 53 return "matches dummy_function: eval(1) = " + std::to_string(r); 54 } else { 55 return "argument does NOT match dummy_function. This should never happen!"; 56 } 57} 58 59struct Payload { 60 Payload() { 61 print_default_created(this); 62 } 63 ~Payload() { 64 print_destroyed(this); 65 } 66 Payload(const Payload &) { 67 print_copy_created(this); 68 } 69 Payload(Payload &&) { 70 print_move_created(this); 71 } 72}; 73 74/// Something to trigger a conversion error 75struct Unregistered {}; 76 77class AbstractBase { 78public: 79 virtual unsigned int func() = 0; 80}; 81 82void func_accepting_func_accepting_base(std::function<double(AbstractBase&)>) { } 83 84struct MovableObject { 85 bool valid = true; 86 87 MovableObject() = default; 88 MovableObject(const MovableObject &) = default; 89 MovableObject &operator=(const MovableObject &) = default; 90 MovableObject(MovableObject &&o) : valid(o.valid) { o.valid = false; } 91 MovableObject &operator=(MovableObject &&o) { 92 valid = o.valid; 93 o.valid = false; 94 return *this; 95 } 96}; 97 98test_initializer callbacks([](py::module &m) { 99 m.def("test_callback1", &test_callback1); 100 m.def("test_callback2", &test_callback2); 101 m.def("test_callback3", &test_callback3); 102 m.def("test_callback4", &test_callback4); 103 m.def("test_callback5", &test_callback5); 104 105 // Test keyword args and generalized unpacking 106 m.def("test_tuple_unpacking", [](py::function f) { 107 auto t1 = py::make_tuple(2, 3); 108 auto t2 = py::make_tuple(5, 6); 109 return f("positional", 1, *t1, 4, *t2); 110 }); 111 112 m.def("test_dict_unpacking", [](py::function f) { 113 auto d1 = py::dict("key"_a="value", "a"_a=1); 114 auto d2 = py::dict(); 115 auto d3 = py::dict("b"_a=2); 116 return f("positional", 1, **d1, **d2, **d3); 117 }); 118 119 m.def("test_keyword_args", [](py::function f) { 120 return f("x"_a=10, "y"_a=20); 121 }); 122 123 m.def("test_unpacking_and_keywords1", [](py::function f) { 124 auto args = py::make_tuple(2); 125 auto kwargs = py::dict("d"_a=4); 126 return f(1, *args, "c"_a=3, **kwargs); 127 }); 128 129 m.def("test_unpacking_and_keywords2", [](py::function f) { 130 auto kwargs1 = py::dict("a"_a=1); 131 auto kwargs2 = py::dict("c"_a=3, "d"_a=4); 132 return f("positional", *py::make_tuple(1), 2, *py::make_tuple(3, 4), 5, 133 "key"_a="value", **kwargs1, "b"_a=2, **kwargs2, "e"_a=5); 134 }); 135 136 m.def("test_unpacking_error1", [](py::function f) { 137 auto kwargs = py::dict("x"_a=3); 138 return f("x"_a=1, "y"_a=2, **kwargs); // duplicate ** after keyword 139 }); 140 141 m.def("test_unpacking_error2", [](py::function f) { 142 auto kwargs = py::dict("x"_a=3); 143 return f(**kwargs, "x"_a=1); // duplicate keyword after ** 144 }); 145 146 m.def("test_arg_conversion_error1", [](py::function f) { 147 f(234, Unregistered(), "kw"_a=567); 148 }); 149 150 m.def("test_arg_conversion_error2", [](py::function f) { 151 f(234, "expected_name"_a=Unregistered(), "kw"_a=567); 152 }); 153 154 /* Test cleanup of lambda closure */ 155 m.def("test_cleanup", []() -> std::function<void(void)> { 156 Payload p; 157 158 return [p]() { 159 /* p should be cleaned up when the returned function is garbage collected */ 160 (void) p; 161 }; 162 }); 163 164 /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */ 165 m.def("dummy_function", &dummy_function); 166 m.def("dummy_function2", &dummy_function2); 167 m.def("roundtrip", &roundtrip, py::arg("f"), py::arg("expect_none")=false); 168 m.def("test_dummy_function", &test_dummy_function); 169 // Export the payload constructor statistics for testing purposes: 170 m.def("payload_cstats", &ConstructorStats::get<Payload>); 171 172 m.def("func_accepting_func_accepting_base", 173 func_accepting_func_accepting_base); 174 175 py::class_<MovableObject>(m, "MovableObject"); 176 177 m.def("callback_with_movable", [](std::function<void(MovableObject &)> f) { 178 auto x = MovableObject(); 179 f(x); // lvalue reference shouldn't move out object 180 return x.valid; // must still return `true` 181 }); 182}); 183