test_callbacks.cpp revision 11986:c12e4625ab56
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 77test_initializer callbacks([](py::module &m) { 78 m.def("test_callback1", &test_callback1); 79 m.def("test_callback2", &test_callback2); 80 m.def("test_callback3", &test_callback3); 81 m.def("test_callback4", &test_callback4); 82 m.def("test_callback5", &test_callback5); 83 84 // Test keyword args and generalized unpacking 85 m.def("test_tuple_unpacking", [](py::function f) { 86 auto t1 = py::make_tuple(2, 3); 87 auto t2 = py::make_tuple(5, 6); 88 return f("positional", 1, *t1, 4, *t2); 89 }); 90 91 m.def("test_dict_unpacking", [](py::function f) { 92 auto d1 = py::dict("key"_a="value", "a"_a=1); 93 auto d2 = py::dict(); 94 auto d3 = py::dict("b"_a=2); 95 return f("positional", 1, **d1, **d2, **d3); 96 }); 97 98 m.def("test_keyword_args", [](py::function f) { 99 return f("x"_a=10, "y"_a=20); 100 }); 101 102 m.def("test_unpacking_and_keywords1", [](py::function f) { 103 auto args = py::make_tuple(2); 104 auto kwargs = py::dict("d"_a=4); 105 return f(1, *args, "c"_a=3, **kwargs); 106 }); 107 108 m.def("test_unpacking_and_keywords2", [](py::function f) { 109 auto kwargs1 = py::dict("a"_a=1); 110 auto kwargs2 = py::dict("c"_a=3, "d"_a=4); 111 return f("positional", *py::make_tuple(1), 2, *py::make_tuple(3, 4), 5, 112 "key"_a="value", **kwargs1, "b"_a=2, **kwargs2, "e"_a=5); 113 }); 114 115 m.def("test_unpacking_error1", [](py::function f) { 116 auto kwargs = py::dict("x"_a=3); 117 return f("x"_a=1, "y"_a=2, **kwargs); // duplicate ** after keyword 118 }); 119 120 m.def("test_unpacking_error2", [](py::function f) { 121 auto kwargs = py::dict("x"_a=3); 122 return f(**kwargs, "x"_a=1); // duplicate keyword after ** 123 }); 124 125 m.def("test_arg_conversion_error1", [](py::function f) { 126 f(234, Unregistered(), "kw"_a=567); 127 }); 128 129 m.def("test_arg_conversion_error2", [](py::function f) { 130 f(234, "expected_name"_a=Unregistered(), "kw"_a=567); 131 }); 132 133 /* Test cleanup of lambda closure */ 134 m.def("test_cleanup", []() -> std::function<void(void)> { 135 Payload p; 136 137 return [p]() { 138 /* p should be cleaned up when the returned function is garbage collected */ 139 }; 140 }); 141 142 /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */ 143 m.def("dummy_function", &dummy_function); 144 m.def("dummy_function2", &dummy_function2); 145 m.def("roundtrip", &roundtrip, py::arg("f"), py::arg("expect_none")=false); 146 m.def("test_dummy_function", &test_dummy_function); 147 // Export the payload constructor statistics for testing purposes: 148 m.def("payload_cstats", &ConstructorStats::get<Payload>); 149}); 150