Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access, |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 3 | __str__, argument and return value conventions |
| 4 | |
| 5 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 6 | |
| 7 | All rights reserved. Use of this source code is governed by a |
| 8 | BSD-style license that can be found in the LICENSE file. |
| 9 | */ |
| 10 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 11 | #include "pybind11_tests.h" |
| 12 | #include "constructor_stats.h" |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 13 | |
| 14 | class ExampleMandA { |
| 15 | public: |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 16 | ExampleMandA() { print_default_created(this); } |
| 17 | ExampleMandA(int value) : value(value) { print_created(this, value); } |
| 18 | ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); } |
| 19 | ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); } |
| 20 | ~ExampleMandA() { print_destroyed(this); } |
| 21 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 22 | std::string toString() { |
| 23 | return "ExampleMandA[value=" + std::to_string(value) + "]"; |
| 24 | } |
| 25 | |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 26 | void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; } |
| 27 | void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; } |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 28 | |
| 29 | void add1(ExampleMandA other) { value += other.value; } // passing by value |
| 30 | void add2(ExampleMandA &other) { value += other.value; } // passing by reference |
| 31 | void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference |
| 32 | void add4(ExampleMandA *other) { value += other->value; } // passing by pointer |
| 33 | void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer |
| 34 | |
| 35 | void add6(int other) { value += other; } // passing by value |
| 36 | void add7(int &other) { value += other; } // passing by reference |
| 37 | void add8(const int &other) { value += other; } // passing by const reference |
| 38 | void add9(int *other) { value += *other; } // passing by pointer |
| 39 | void add10(const int *other) { value += *other; } // passing by const pointer |
| 40 | |
| 41 | ExampleMandA self1() { return *this; } // return by value |
| 42 | ExampleMandA &self2() { return *this; } // return by reference |
| 43 | const ExampleMandA &self3() { return *this; } // return by const reference |
| 44 | ExampleMandA *self4() { return this; } // return by pointer |
| 45 | const ExampleMandA *self5() { return this; } // return by const pointer |
| 46 | |
| 47 | int internal1() { return value; } // return by value |
| 48 | int &internal2() { return value; } // return by reference |
| 49 | const int &internal3() { return value; } // return by const reference |
| 50 | int *internal4() { return &value; } // return by pointer |
| 51 | const int *internal5() { return &value; } // return by const pointer |
| 52 | |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 53 | py::str overloaded(int, float) { return "(int, float)"; } |
| 54 | py::str overloaded(float, int) { return "(float, int)"; } |
| 55 | py::str overloaded(int, int) { return "(int, int)"; } |
| 56 | py::str overloaded(float, float) { return "(float, float)"; } |
| 57 | py::str overloaded(int, float) const { return "(int, float) const"; } |
| 58 | py::str overloaded(float, int) const { return "(float, int) const"; } |
| 59 | py::str overloaded(int, int) const { return "(int, int) const"; } |
| 60 | py::str overloaded(float, float) const { return "(float, float) const"; } |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 61 | |
Jason Rhinelander | d355f2f | 2017-04-16 22:31:13 -0400 | [diff] [blame] | 62 | static py::str overloaded() { return "static"; } |
| 63 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 64 | int value = 0; |
| 65 | }; |
| 66 | |
Dean Moldovan | 5b7e190 | 2016-10-21 18:51:14 +0200 | [diff] [blame] | 67 | struct TestProperties { |
| 68 | int value = 1; |
| 69 | static int static_value; |
| 70 | |
| 71 | int get() const { return value; } |
| 72 | void set(int v) { value = v; } |
| 73 | |
| 74 | static int static_get() { return static_value; } |
| 75 | static void static_set(int v) { static_value = v; } |
| 76 | }; |
| 77 | |
| 78 | int TestProperties::static_value = 1; |
| 79 | |
Dean Moldovan | e0e2ea3 | 2017-04-06 23:45:12 +0200 | [diff] [blame] | 80 | struct TestPropertiesOverride : TestProperties { |
| 81 | int value = 99; |
| 82 | static int static_value; |
| 83 | }; |
| 84 | |
| 85 | int TestPropertiesOverride::static_value = 99; |
| 86 | |
Dean Moldovan | 03f627e | 2016-11-01 11:44:57 +0100 | [diff] [blame] | 87 | struct SimpleValue { int value = 1; }; |
| 88 | |
| 89 | struct TestPropRVP { |
| 90 | SimpleValue v1; |
| 91 | SimpleValue v2; |
| 92 | static SimpleValue sv1; |
| 93 | static SimpleValue sv2; |
| 94 | |
| 95 | const SimpleValue &get1() const { return v1; } |
| 96 | const SimpleValue &get2() const { return v2; } |
| 97 | SimpleValue get_rvalue() const { return v2; } |
| 98 | void set1(int v) { v1.value = v; } |
| 99 | void set2(int v) { v2.value = v; } |
| 100 | }; |
| 101 | |
| 102 | SimpleValue TestPropRVP::sv1{}; |
| 103 | SimpleValue TestPropRVP::sv2{}; |
| 104 | |
Dean Moldovan | 6fccf69 | 2016-10-11 01:12:48 +0200 | [diff] [blame] | 105 | class DynamicClass { |
| 106 | public: |
| 107 | DynamicClass() { print_default_created(this); } |
| 108 | ~DynamicClass() { print_destroyed(this); } |
| 109 | }; |
| 110 | |
Dean Moldovan | b8cb5ca | 2016-10-14 18:01:17 +0200 | [diff] [blame] | 111 | class CppDerivedDynamicClass : public DynamicClass { }; |
| 112 | |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 113 | // py::arg/py::arg_v testing: these arguments just record their argument when invoked |
| 114 | class ArgInspector1 { public: std::string arg = "(default arg inspector 1)"; }; |
| 115 | class ArgInspector2 { public: std::string arg = "(default arg inspector 2)"; }; |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 116 | class ArgAlwaysConverts { }; |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 117 | namespace pybind11 { namespace detail { |
| 118 | template <> struct type_caster<ArgInspector1> { |
| 119 | public: |
| 120 | PYBIND11_TYPE_CASTER(ArgInspector1, _("ArgInspector1")); |
| 121 | |
| 122 | bool load(handle src, bool convert) { |
| 123 | value.arg = "loading ArgInspector1 argument " + |
| 124 | std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed. " |
| 125 | "Argument value = " + (std::string) str(src); |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | static handle cast(const ArgInspector1 &src, return_value_policy, handle) { |
| 130 | return str(src.arg).release(); |
| 131 | } |
| 132 | }; |
| 133 | template <> struct type_caster<ArgInspector2> { |
| 134 | public: |
| 135 | PYBIND11_TYPE_CASTER(ArgInspector2, _("ArgInspector2")); |
| 136 | |
| 137 | bool load(handle src, bool convert) { |
| 138 | value.arg = "loading ArgInspector2 argument " + |
| 139 | std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed. " |
| 140 | "Argument value = " + (std::string) str(src); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | static handle cast(const ArgInspector2 &src, return_value_policy, handle) { |
| 145 | return str(src.arg).release(); |
| 146 | } |
| 147 | }; |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 148 | template <> struct type_caster<ArgAlwaysConverts> { |
| 149 | public: |
| 150 | PYBIND11_TYPE_CASTER(ArgAlwaysConverts, _("ArgAlwaysConverts")); |
| 151 | |
| 152 | bool load(handle, bool convert) { |
| 153 | return convert; |
| 154 | } |
| 155 | |
| 156 | static handle cast(const ArgAlwaysConverts &, return_value_policy, handle) { |
Jason Rhinelander | 60526d4 | 2017-07-07 17:26:14 -0400 | [diff] [blame] | 157 | return py::none().release(); |
| 158 | } |
| 159 | }; |
| 160 | }} |
| 161 | |
| 162 | // test_custom_caster_destruction |
| 163 | class DestructionTester { |
| 164 | public: |
| 165 | DestructionTester() { print_default_created(this); } |
| 166 | ~DestructionTester() { print_destroyed(this); } |
| 167 | DestructionTester(const DestructionTester &) { print_copy_created(this); } |
| 168 | DestructionTester(DestructionTester &&) { print_move_created(this); } |
| 169 | DestructionTester &operator=(const DestructionTester &) { print_copy_assigned(this); return *this; } |
| 170 | DestructionTester &operator=(DestructionTester &&) { print_move_assigned(this); return *this; } |
| 171 | }; |
| 172 | namespace pybind11 { namespace detail { |
| 173 | template <> struct type_caster<DestructionTester> { |
| 174 | PYBIND11_TYPE_CASTER(DestructionTester, _("DestructionTester")); |
| 175 | bool load(handle, bool) { return true; } |
| 176 | |
| 177 | static handle cast(const DestructionTester &, return_value_policy, handle) { |
| 178 | return py::bool_(true).release(); |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 179 | } |
| 180 | }; |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 181 | }} |
| 182 | |
Jason Rhinelander | 23bf894 | 2017-05-16 11:07:28 -0400 | [diff] [blame] | 183 | // Issue/PR #648: bad arg default debugging output |
Jason Rhinelander | 1eaacd1 | 2017-02-08 02:45:51 -0500 | [diff] [blame] | 184 | class NotRegistered {}; |
| 185 | |
Jason Rhinelander | 4e1e4a5 | 2017-05-17 11:55:43 -0400 | [diff] [blame] | 186 | // Test None-allowed py::arg argument policy |
| 187 | class NoneTester { public: int answer = 42; }; |
| 188 | int none1(const NoneTester &obj) { return obj.answer; } |
| 189 | int none2(NoneTester *obj) { return obj ? obj->answer : -1; } |
| 190 | int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; } |
| 191 | int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; } |
| 192 | int none5(std::shared_ptr<NoneTester> obj) { return obj ? obj->answer : -1; } |
| 193 | |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 194 | struct StrIssue { |
| 195 | int val = -1; |
| 196 | |
| 197 | StrIssue() = default; |
| 198 | StrIssue(int i) : val{i} {} |
| 199 | }; |
| 200 | |
Jason Rhinelander | 23bf894 | 2017-05-16 11:07:28 -0400 | [diff] [blame] | 201 | // Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class |
| 202 | class UnregisteredBase { |
| 203 | public: |
| 204 | void do_nothing() const {} |
| 205 | void increase_value() { rw_value++; ro_value += 0.25; } |
| 206 | void set_int(int v) { rw_value = v; } |
| 207 | int get_int() const { return rw_value; } |
| 208 | double get_double() const { return ro_value; } |
| 209 | int rw_value = 42; |
| 210 | double ro_value = 1.25; |
| 211 | }; |
| 212 | class RegisteredDerived : public UnregisteredBase { |
| 213 | public: |
| 214 | using UnregisteredBase::UnregisteredBase; |
| 215 | double sum() const { return rw_value + ro_value; } |
| 216 | }; |
| 217 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 218 | test_initializer methods_and_attributes([](py::module &m) { |
Jason Rhinelander | 0a90b2d | 2017-04-16 20:30:52 -0400 | [diff] [blame] | 219 | py::class_<ExampleMandA> emna(m, "ExampleMandA"); |
| 220 | emna.def(py::init<>()) |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 221 | .def(py::init<int>()) |
| 222 | .def(py::init<const ExampleMandA&>()) |
| 223 | .def("add1", &ExampleMandA::add1) |
| 224 | .def("add2", &ExampleMandA::add2) |
| 225 | .def("add3", &ExampleMandA::add3) |
| 226 | .def("add4", &ExampleMandA::add4) |
| 227 | .def("add5", &ExampleMandA::add5) |
| 228 | .def("add6", &ExampleMandA::add6) |
| 229 | .def("add7", &ExampleMandA::add7) |
| 230 | .def("add8", &ExampleMandA::add8) |
| 231 | .def("add9", &ExampleMandA::add9) |
| 232 | .def("add10", &ExampleMandA::add10) |
| 233 | .def("self1", &ExampleMandA::self1) |
| 234 | .def("self2", &ExampleMandA::self2) |
| 235 | .def("self3", &ExampleMandA::self3) |
| 236 | .def("self4", &ExampleMandA::self4) |
| 237 | .def("self5", &ExampleMandA::self5) |
| 238 | .def("internal1", &ExampleMandA::internal1) |
| 239 | .def("internal2", &ExampleMandA::internal2) |
| 240 | .def("internal3", &ExampleMandA::internal3) |
| 241 | .def("internal4", &ExampleMandA::internal4) |
| 242 | .def("internal5", &ExampleMandA::internal5) |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 243 | #if defined(PYBIND11_OVERLOAD_CAST) |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 244 | .def("overloaded", py::overload_cast<int, float>(&ExampleMandA::overloaded)) |
| 245 | .def("overloaded", py::overload_cast<float, int>(&ExampleMandA::overloaded)) |
| 246 | .def("overloaded", py::overload_cast<int, int>(&ExampleMandA::overloaded)) |
| 247 | .def("overloaded", py::overload_cast<float, float>(&ExampleMandA::overloaded)) |
| 248 | .def("overloaded_float", py::overload_cast<float, float>(&ExampleMandA::overloaded)) |
| 249 | .def("overloaded_const", py::overload_cast<int, float>(&ExampleMandA::overloaded, py::const_)) |
| 250 | .def("overloaded_const", py::overload_cast<float, int>(&ExampleMandA::overloaded, py::const_)) |
| 251 | .def("overloaded_const", py::overload_cast<int, int>(&ExampleMandA::overloaded, py::const_)) |
| 252 | .def("overloaded_const", py::overload_cast<float, float>(&ExampleMandA::overloaded, py::const_)) |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 253 | #else |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 254 | .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, float)>(&ExampleMandA::overloaded)) |
| 255 | .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, int)>(&ExampleMandA::overloaded)) |
| 256 | .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded)) |
| 257 | .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded)) |
| 258 | .def("overloaded_float", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded)) |
| 259 | .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, float) const>(&ExampleMandA::overloaded)) |
| 260 | .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, int) const>(&ExampleMandA::overloaded)) |
| 261 | .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, int) const>(&ExampleMandA::overloaded)) |
| 262 | .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, float) const>(&ExampleMandA::overloaded)) |
Dean Moldovan | 4e959c9 | 2016-12-08 11:07:52 +0100 | [diff] [blame] | 263 | #endif |
Jason Rhinelander | d355f2f | 2017-04-16 22:31:13 -0400 | [diff] [blame] | 264 | // Raise error if trying to mix static/non-static overloads on the same name: |
| 265 | .def_static("add_mixed_overloads1", []() { |
| 266 | auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests").attr("ExampleMandA")); |
| 267 | emna.def ("overload_mixed1", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded)) |
| 268 | .def_static("overload_mixed1", static_cast<py::str ( *)( )>(&ExampleMandA::overloaded)); |
| 269 | }) |
| 270 | .def_static("add_mixed_overloads2", []() { |
| 271 | auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests").attr("ExampleMandA")); |
| 272 | emna.def_static("overload_mixed2", static_cast<py::str ( *)( )>(&ExampleMandA::overloaded)) |
| 273 | .def ("overload_mixed2", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded)); |
| 274 | }) |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 275 | .def("__str__", &ExampleMandA::toString) |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 276 | .def_readwrite("value", &ExampleMandA::value); |
Dean Moldovan | 6fccf69 | 2016-10-11 01:12:48 +0200 | [diff] [blame] | 277 | |
Jason Rhinelander | 0a90b2d | 2017-04-16 20:30:52 -0400 | [diff] [blame] | 278 | // Issue #443: can't call copied methods in Python 3 |
| 279 | emna.attr("add2b") = emna.attr("add2"); |
| 280 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 281 | py::class_<TestProperties>(m, "TestProperties") |
Dean Moldovan | 5b7e190 | 2016-10-21 18:51:14 +0200 | [diff] [blame] | 282 | .def(py::init<>()) |
| 283 | .def_readonly("def_readonly", &TestProperties::value) |
| 284 | .def_readwrite("def_readwrite", &TestProperties::value) |
| 285 | .def_property_readonly("def_property_readonly", &TestProperties::get) |
| 286 | .def_property("def_property", &TestProperties::get, &TestProperties::set) |
| 287 | .def_readonly_static("def_readonly_static", &TestProperties::static_value) |
| 288 | .def_readwrite_static("def_readwrite_static", &TestProperties::static_value) |
| 289 | .def_property_readonly_static("def_property_readonly_static", |
| 290 | [](py::object) { return TestProperties::static_get(); }) |
| 291 | .def_property_static("def_property_static", |
| 292 | [](py::object) { return TestProperties::static_get(); }, |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 293 | [](py::object, int v) { TestProperties::static_set(v); }) |
| 294 | .def_property_static("static_cls", |
| 295 | [](py::object cls) { return cls; }, |
| 296 | [](py::object cls, py::function f) { f(cls); }); |
Dean Moldovan | 5b7e190 | 2016-10-21 18:51:14 +0200 | [diff] [blame] | 297 | |
Dean Moldovan | e0e2ea3 | 2017-04-06 23:45:12 +0200 | [diff] [blame] | 298 | py::class_<TestPropertiesOverride, TestProperties>(m, "TestPropertiesOverride") |
| 299 | .def(py::init<>()) |
| 300 | .def_readonly("def_readonly", &TestPropertiesOverride::value) |
| 301 | .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value); |
| 302 | |
Dean Moldovan | 03f627e | 2016-11-01 11:44:57 +0100 | [diff] [blame] | 303 | py::class_<SimpleValue>(m, "SimpleValue") |
| 304 | .def_readwrite("value", &SimpleValue::value); |
| 305 | |
| 306 | auto static_get1 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv1; }; |
| 307 | auto static_get2 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv2; }; |
| 308 | auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.value = v; }; |
| 309 | auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.value = v; }; |
| 310 | auto rvp_copy = py::return_value_policy::copy; |
| 311 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 312 | py::class_<TestPropRVP>(m, "TestPropRVP") |
Dean Moldovan | 03f627e | 2016-11-01 11:44:57 +0100 | [diff] [blame] | 313 | .def(py::init<>()) |
| 314 | .def_property_readonly("ro_ref", &TestPropRVP::get1) |
| 315 | .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy) |
| 316 | .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy)) |
| 317 | .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1) |
| 318 | .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy) |
| 319 | .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2) |
| 320 | .def_property_readonly_static("static_ro_ref", static_get1) |
| 321 | .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy) |
| 322 | .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy)) |
| 323 | .def_property_static("static_rw_ref", static_get1, static_set1) |
| 324 | .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy) |
| 325 | .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2) |
| 326 | .def_property_readonly("rvalue", &TestPropRVP::get_rvalue) |
| 327 | .def_property_readonly_static("static_rvalue", [](py::object) { return SimpleValue(); }); |
| 328 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 329 | struct MetaclassOverride { }; |
| 330 | py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type)) |
| 331 | .def_property_readonly_static("readonly", [](py::object) { return 1; }); |
| 332 | |
Wenzel Jakob | 64cb699 | 2016-12-26 13:12:10 +0100 | [diff] [blame] | 333 | #if !defined(PYPY_VERSION) |
Dean Moldovan | 6fccf69 | 2016-10-11 01:12:48 +0200 | [diff] [blame] | 334 | py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr()) |
| 335 | .def(py::init()); |
Dean Moldovan | b8cb5ca | 2016-10-14 18:01:17 +0200 | [diff] [blame] | 336 | |
| 337 | py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass") |
| 338 | .def(py::init()); |
Wenzel Jakob | 64cb699 | 2016-12-26 13:12:10 +0100 | [diff] [blame] | 339 | #endif |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 340 | |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 341 | // Test converting. The ArgAlwaysConverts is just there to make the first no-conversion pass |
| 342 | // fail so that our call always ends up happening via the second dispatch (the one that allows |
| 343 | // some conversion). |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 344 | class ArgInspector { |
| 345 | public: |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 346 | ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; } |
| 347 | std::string g(ArgInspector1 a, const ArgInspector1 &b, int c, ArgInspector2 *d, ArgAlwaysConverts) { |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 348 | return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg; |
| 349 | } |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 350 | static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; } |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 351 | }; |
| 352 | py::class_<ArgInspector>(m, "ArgInspector") |
| 353 | .def(py::init<>()) |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 354 | .def("f", &ArgInspector::f, py::arg(), py::arg() = ArgAlwaysConverts()) |
| 355 | .def("g", &ArgInspector::g, "a"_a.noconvert(), "b"_a, "c"_a.noconvert()=13, "d"_a=ArgInspector2(), py::arg() = ArgAlwaysConverts()) |
| 356 | .def_static("h", &ArgInspector::h, py::arg().noconvert(), py::arg() = ArgAlwaysConverts()) |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 357 | ; |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 358 | m.def("arg_inspect_func", [](ArgInspector2 a, ArgInspector1 b, ArgAlwaysConverts) { return a.arg + "\n" + b.arg; }, |
| 359 | py::arg().noconvert(false), py::arg_v(nullptr, ArgInspector1()).noconvert(true), py::arg() = ArgAlwaysConverts()); |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 360 | |
| 361 | m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f")); |
| 362 | m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert()); |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 363 | m.def("ints_preferred", [](int i) { return i / 2; }, py::arg("i")); |
| 364 | m.def("ints_only", [](int i) { return i / 2; }, py::arg("i").noconvert()); |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 365 | |
Jason Rhinelander | 23bf894 | 2017-05-16 11:07:28 -0400 | [diff] [blame] | 366 | // Issue/PR #648: bad arg default debugging output |
Jason Rhinelander | 1eaacd1 | 2017-02-08 02:45:51 -0500 | [diff] [blame] | 367 | #if !defined(NDEBUG) |
| 368 | m.attr("debug_enabled") = true; |
| 369 | #else |
| 370 | m.attr("debug_enabled") = false; |
| 371 | #endif |
| 372 | m.def("bad_arg_def_named", []{ |
Jason Rhinelander | 2d14c1c | 2017-04-15 11:12:41 -0400 | [diff] [blame] | 373 | auto m = py::module::import("pybind11_tests"); |
Jason Rhinelander | 1eaacd1 | 2017-02-08 02:45:51 -0500 | [diff] [blame] | 374 | m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg("a") = NotRegistered()); |
| 375 | }); |
| 376 | m.def("bad_arg_def_unnamed", []{ |
Jason Rhinelander | 2d14c1c | 2017-04-15 11:12:41 -0400 | [diff] [blame] | 377 | auto m = py::module::import("pybind11_tests"); |
Jason Rhinelander | 1eaacd1 | 2017-02-08 02:45:51 -0500 | [diff] [blame] | 378 | m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg() = NotRegistered()); |
| 379 | }); |
Jason Rhinelander | 4e1e4a5 | 2017-05-17 11:55:43 -0400 | [diff] [blame] | 380 | |
| 381 | py::class_<NoneTester, std::shared_ptr<NoneTester>>(m, "NoneTester") |
| 382 | .def(py::init<>()); |
| 383 | m.def("no_none1", &none1, py::arg().none(false)); |
| 384 | m.def("no_none2", &none2, py::arg().none(false)); |
| 385 | m.def("no_none3", &none3, py::arg().none(false)); |
| 386 | m.def("no_none4", &none4, py::arg().none(false)); |
| 387 | m.def("no_none5", &none5, py::arg().none(false)); |
| 388 | m.def("ok_none1", &none1); |
| 389 | m.def("ok_none2", &none2, py::arg().none(true)); |
| 390 | m.def("ok_none3", &none3); |
| 391 | m.def("ok_none4", &none4, py::arg().none(true)); |
| 392 | m.def("ok_none5", &none5); |
| 393 | |
Dean Moldovan | bdfb50f | 2017-06-07 16:52:50 +0200 | [diff] [blame] | 394 | // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid |
| 395 | py::class_<StrIssue>(m, "StrIssue") |
| 396 | .def(py::init<int>()) |
| 397 | .def(py::init<>()) |
| 398 | .def("__str__", [](const StrIssue &si) { |
| 399 | return "StrIssue[" + std::to_string(si.val) + "]"; } |
| 400 | ); |
Jason Rhinelander | 23bf894 | 2017-05-16 11:07:28 -0400 | [diff] [blame] | 401 | |
| 402 | // Issues #854/910: incompatible function args when member function/pointer is in unregistered |
| 403 | // base class The methods and member pointers below actually resolve to members/pointers in |
| 404 | // UnregisteredBase; before this test/fix they would be registered via lambda with a first |
| 405 | // argument of an unregistered type, and thus uncallable. |
| 406 | py::class_<RegisteredDerived>(m, "RegisteredDerived") |
| 407 | .def(py::init<>()) |
| 408 | .def("do_nothing", &RegisteredDerived::do_nothing) |
| 409 | .def("increase_value", &RegisteredDerived::increase_value) |
| 410 | .def_readwrite("rw_value", &RegisteredDerived::rw_value) |
| 411 | .def_readonly("ro_value", &RegisteredDerived::ro_value) |
| 412 | // These should trigger a static_assert if uncommented |
| 413 | //.def_readwrite("fails", &SimpleValue::value) // should trigger a static_assert if uncommented |
| 414 | //.def_readonly("fails", &SimpleValue::value) // should trigger a static_assert if uncommented |
| 415 | .def_property("rw_value_prop", &RegisteredDerived::get_int, &RegisteredDerived::set_int) |
| 416 | .def_property_readonly("ro_value_prop", &RegisteredDerived::get_double) |
| 417 | // This one is in the registered class: |
| 418 | .def("sum", &RegisteredDerived::sum) |
| 419 | ; |
| 420 | |
| 421 | using Adapted = decltype(py::method_adaptor<RegisteredDerived>(&RegisteredDerived::do_nothing)); |
| 422 | static_assert(std::is_same<Adapted, void (RegisteredDerived::*)() const>::value, ""); |
Jason Rhinelander | 60526d4 | 2017-07-07 17:26:14 -0400 | [diff] [blame] | 423 | |
| 424 | // test_custom_caster_destruction |
| 425 | // Test that `take_ownership` works on types with a custom type caster when given a pointer |
| 426 | |
| 427 | // default policy: don't take ownership: |
| 428 | m.def("custom_caster_no_destroy", []() { static auto *dt = new DestructionTester(); return dt; }); |
| 429 | |
| 430 | m.def("custom_caster_destroy", []() { return new DestructionTester(); }, |
| 431 | py::return_value_policy::take_ownership); // Takes ownership: destroy when finished |
| 432 | m.def("custom_caster_destroy_const", []() -> const DestructionTester * { return new DestructionTester(); }, |
| 433 | py::return_value_policy::take_ownership); // Likewise (const doesn't inhibit destruction) |
| 434 | m.def("destruction_tester_cstats", &ConstructorStats::get<DestructionTester>, py::return_value_policy::reference); |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 435 | }); |