blob: 8e0fc2d8e8ee961542769a5c3c028ecbb743baa2 [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access,
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04003 __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 Moldovana0c1ccf2016-08-12 13:50:00 +020011#include "pybind11_tests.h"
12#include "constructor_stats.h"
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040013
14class ExampleMandA {
15public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040016 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 Rhinelanderb3f3d792016-07-18 16:43:18 -040022 std::string toString() {
23 return "ExampleMandA[value=" + std::to_string(value) + "]";
24 }
25
Jason Rhinelander3f589372016-08-07 13:05:26 -040026 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 Rhinelanderb3f3d792016-07-18 16:43:18 -040028
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 Rhinelandere5505892017-02-03 18:25:34 -050053 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 Moldovan4e959c92016-12-08 11:07:52 +010061
Jason Rhinelanderd355f2f2017-04-16 22:31:13 -040062 static py::str overloaded() { return "static"; }
63
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040064 int value = 0;
65};
66
Dean Moldovan5b7e1902016-10-21 18:51:14 +020067struct 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
78int TestProperties::static_value = 1;
79
Dean Moldovane0e2ea32017-04-06 23:45:12 +020080struct TestPropertiesOverride : TestProperties {
81 int value = 99;
82 static int static_value;
83};
84
85int TestPropertiesOverride::static_value = 99;
86
Dean Moldovan03f627e2016-11-01 11:44:57 +010087struct SimpleValue { int value = 1; };
88
89struct 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
102SimpleValue TestPropRVP::sv1{};
103SimpleValue TestPropRVP::sv2{};
104
Dean Moldovan6fccf692016-10-11 01:12:48 +0200105class DynamicClass {
106public:
107 DynamicClass() { print_default_created(this); }
108 ~DynamicClass() { print_destroyed(this); }
109};
110
Dean Moldovanb8cb5ca2016-10-14 18:01:17 +0200111class CppDerivedDynamicClass : public DynamicClass { };
112
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500113// py::arg/py::arg_v testing: these arguments just record their argument when invoked
114class ArgInspector1 { public: std::string arg = "(default arg inspector 1)"; };
115class ArgInspector2 { public: std::string arg = "(default arg inspector 2)"; };
Jason Rhinelandere5505892017-02-03 18:25:34 -0500116class ArgAlwaysConverts { };
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500117namespace pybind11 { namespace detail {
118template <> struct type_caster<ArgInspector1> {
119public:
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};
133template <> struct type_caster<ArgInspector2> {
134public:
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 Rhinelandere5505892017-02-03 18:25:34 -0500148template <> struct type_caster<ArgAlwaysConverts> {
149public:
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 Rhinelander60526d42017-07-07 17:26:14 -0400157 return py::none().release();
158 }
159};
160}}
161
162// test_custom_caster_destruction
163class DestructionTester {
164public:
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};
172namespace pybind11 { namespace detail {
173template <> 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 Rhinelandere5505892017-02-03 18:25:34 -0500179 }
180};
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500181}}
182
Jason Rhinelander23bf8942017-05-16 11:07:28 -0400183// Issue/PR #648: bad arg default debugging output
Jason Rhinelander1eaacd12017-02-08 02:45:51 -0500184class NotRegistered {};
185
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400186// Test None-allowed py::arg argument policy
187class NoneTester { public: int answer = 42; };
188int none1(const NoneTester &obj) { return obj.answer; }
189int none2(NoneTester *obj) { return obj ? obj->answer : -1; }
190int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
191int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
192int none5(std::shared_ptr<NoneTester> obj) { return obj ? obj->answer : -1; }
193
Dean Moldovanbdfb50f2017-06-07 16:52:50 +0200194struct StrIssue {
195 int val = -1;
196
197 StrIssue() = default;
198 StrIssue(int i) : val{i} {}
199};
200
Jason Rhinelander23bf8942017-05-16 11:07:28 -0400201// Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class
202class UnregisteredBase {
203public:
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};
212class RegisteredDerived : public UnregisteredBase {
213public:
214 using UnregisteredBase::UnregisteredBase;
215 double sum() const { return rw_value + ro_value; }
216};
217
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400218test_initializer methods_and_attributes([](py::module &m) {
Jason Rhinelander0a90b2d2017-04-16 20:30:52 -0400219 py::class_<ExampleMandA> emna(m, "ExampleMandA");
220 emna.def(py::init<>())
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400221 .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 Moldovan4e959c92016-12-08 11:07:52 +0100243#if defined(PYBIND11_OVERLOAD_CAST)
Jason Rhinelandere5505892017-02-03 18:25:34 -0500244 .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 Moldovan4e959c92016-12-08 11:07:52 +0100253#else
Jason Rhinelandere5505892017-02-03 18:25:34 -0500254 .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 Moldovan4e959c92016-12-08 11:07:52 +0100263#endif
Jason Rhinelanderd355f2f2017-04-16 22:31:13 -0400264 // 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 Rhinelanderb3f3d792016-07-18 16:43:18 -0400275 .def("__str__", &ExampleMandA::toString)
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100276 .def_readwrite("value", &ExampleMandA::value);
Dean Moldovan6fccf692016-10-11 01:12:48 +0200277
Jason Rhinelander0a90b2d2017-04-16 20:30:52 -0400278 // Issue #443: can't call copied methods in Python 3
279 emna.attr("add2b") = emna.attr("add2");
280
Dean Moldovandd016652017-02-16 23:02:56 +0100281 py::class_<TestProperties>(m, "TestProperties")
Dean Moldovan5b7e1902016-10-21 18:51:14 +0200282 .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 Moldovanc91f8bd2017-02-13 18:11:24 +0100293 [](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 Moldovan5b7e1902016-10-21 18:51:14 +0200297
Dean Moldovane0e2ea32017-04-06 23:45:12 +0200298 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 Moldovan03f627e2016-11-01 11:44:57 +0100303 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 Moldovandd016652017-02-16 23:02:56 +0100312 py::class_<TestPropRVP>(m, "TestPropRVP")
Dean Moldovan03f627e2016-11-01 11:44:57 +0100313 .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 Moldovandd016652017-02-16 23:02:56 +0100329 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 Jakob64cb6992016-12-26 13:12:10 +0100333#if !defined(PYPY_VERSION)
Dean Moldovan6fccf692016-10-11 01:12:48 +0200334 py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
335 .def(py::init());
Dean Moldovanb8cb5ca2016-10-14 18:01:17 +0200336
337 py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
338 .def(py::init());
Wenzel Jakob64cb6992016-12-26 13:12:10 +0100339#endif
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500340
Jason Rhinelandere5505892017-02-03 18:25:34 -0500341 // 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 Rhinelanderabc29ca2017-01-23 03:50:00 -0500344 class ArgInspector {
345 public:
Jason Rhinelandere5505892017-02-03 18:25:34 -0500346 ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; }
347 std::string g(ArgInspector1 a, const ArgInspector1 &b, int c, ArgInspector2 *d, ArgAlwaysConverts) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500348 return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg;
349 }
Jason Rhinelandere5505892017-02-03 18:25:34 -0500350 static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; }
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500351 };
352 py::class_<ArgInspector>(m, "ArgInspector")
353 .def(py::init<>())
Jason Rhinelandere5505892017-02-03 18:25:34 -0500354 .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 Rhinelanderabc29ca2017-01-23 03:50:00 -0500357 ;
Jason Rhinelandere5505892017-02-03 18:25:34 -0500358 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 Rhinelanderabc29ca2017-01-23 03:50:00 -0500360
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 Moldovanbdfb50f2017-06-07 16:52:50 +0200363 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 Rhinelanderabc29ca2017-01-23 03:50:00 -0500365
Jason Rhinelander23bf8942017-05-16 11:07:28 -0400366 // Issue/PR #648: bad arg default debugging output
Jason Rhinelander1eaacd12017-02-08 02:45:51 -0500367#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 Rhinelander2d14c1c2017-04-15 11:12:41 -0400373 auto m = py::module::import("pybind11_tests");
Jason Rhinelander1eaacd12017-02-08 02:45:51 -0500374 m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg("a") = NotRegistered());
375 });
376 m.def("bad_arg_def_unnamed", []{
Jason Rhinelander2d14c1c2017-04-15 11:12:41 -0400377 auto m = py::module::import("pybind11_tests");
Jason Rhinelander1eaacd12017-02-08 02:45:51 -0500378 m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg() = NotRegistered());
379 });
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400380
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 Moldovanbdfb50f2017-06-07 16:52:50 +0200394 // 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 Rhinelander23bf8942017-05-16 11:07:28 -0400401
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 Rhinelander60526d42017-07-07 17:26:14 -0400423
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 Rhinelander52f4be82016-09-03 14:54:22 -0400435});