blob: 362ad448b5a19044bbb831b9d851ac501a67ae4e [file] [log] [blame]
Wenzel Jakob17cdb062016-03-10 13:24:10 +01001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_issues.cpp -- collection of testcases for miscellaneous issues
Wenzel Jakob17cdb062016-03-10 13:24:10 +01003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob17cdb062016-03-10 13:24:10 +01005
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Wenzel Jakobf54ded72016-04-20 17:00:57 +020012#include <pybind11/stl.h>
Jason Rhinelander1b05ce52016-08-09 17:57:59 -040013#include <pybind11/operators.h>
Wenzel Jakob17cdb062016-03-10 13:24:10 +010014
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020015
Jason Rhinelander3f589372016-08-07 13:05:26 -040016#define TRACKERS(CLASS) CLASS() { print_default_created(this); } ~CLASS() { print_destroyed(this); }
17struct NestABase { int value = -2; TRACKERS(NestABase) };
18struct NestA : NestABase { int value = 3; NestA& operator+=(int i) { value += i; return *this; } TRACKERS(NestA) };
19struct NestB { NestA a; int value = 4; NestB& operator-=(int i) { value -= i; return *this; } TRACKERS(NestB) };
20struct NestC { NestB b; int value = 5; NestC& operator*=(int i) { value *= i; return *this; } TRACKERS(NestC) };
21
Wenzel Jakob382484a2016-09-10 15:28:37 +090022/// #393
23class OpTest1 {};
24class OpTest2 {};
25
26OpTest1 operator+(const OpTest1 &, const OpTest1 &) {
27 py::print("Add OpTest1 with OpTest1");
28 return OpTest1();
29}
30OpTest2 operator+(const OpTest2 &, const OpTest2 &) {
31 py::print("Add OpTest2 with OpTest2");
32 return OpTest2();
33}
34OpTest2 operator+(const OpTest2 &, const OpTest1 &) {
35 py::print("Add OpTest2 with OpTest1");
36 return OpTest2();
37}
38
Jason Rhinelander6873c202016-10-24 21:58:22 -040039// #461
40class Dupe1 {
41public:
42 Dupe1(int v) : v_{v} {}
43 int get_value() const { return v_; }
44private:
45 int v_;
46};
47class Dupe2 {};
48class Dupe3 {};
49class DupeException : public std::runtime_error {};
50
Wenzel Jakob17cdb062016-03-10 13:24:10 +010051void init_issues(py::module &m) {
52 py::module m2 = m.def_submodule("issues");
53
Wenzel Jakob9059bd82016-05-01 10:39:45 +020054#if !defined(_MSC_VER)
55 // Visual Studio 2015 currently cannot compile this test
56 // (see the comment in type_caster_base::make_copy_constructor)
57 // #70 compilation issue if operator new is not public
58 class NonConstructible { private: void *operator new(size_t bytes) throw(); };
59 py::class_<NonConstructible>(m, "Foo");
Wenzel Jakobbd57eb42016-05-01 14:42:20 +020060 m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
Wenzel Jakob9059bd82016-05-01 10:39:45 +020061 py::return_value_policy::reference);
62#endif
63
Wenzel Jakob17cdb062016-03-10 13:24:10 +010064 // #137: const char* isn't handled properly
Dean Moldovan665e8802016-08-12 22:28:31 +020065 m2.def("print_cchar", [](const char *s) { return std::string(s); });
Wenzel Jakobd3349af2016-03-26 23:04:10 +010066
67 // #150: char bindings broken
Dean Moldovan665e8802016-08-12 22:28:31 +020068 m2.def("print_char", [](char c) { return std::string(1, c); });
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020069
70 // #159: virtual function dispatch has problems with similar-named functions
Dean Moldovan665e8802016-08-12 22:28:31 +020071 struct Base { virtual std::string dispatch() const {
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020072 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
Dean Moldovan665e8802016-08-12 22:28:31 +020073 return {};
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020074 }; };
Wenzel Jakobe7074972016-04-30 22:44:00 +020075
76 struct DispatchIssue : Base {
Dean Moldovan665e8802016-08-12 22:28:31 +020077 virtual std::string dispatch() const {
78 PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
Wenzel Jakobe7074972016-04-30 22:44:00 +020079 }
80 };
81
Jason Rhinelander5fffe202016-09-06 12:17:06 -040082 py::class_<Base, DispatchIssue>(m2, "DispatchIssue")
Wenzel Jakobf54ded72016-04-20 17:00:57 +020083 .def(py::init<>())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020084 .def("dispatch", &Base::dispatch);
85
Dean Moldovan665e8802016-08-12 22:28:31 +020086 m2.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
Wenzel Jakobe7074972016-04-30 22:44:00 +020087
88 struct Placeholder { int i; Placeholder(int i) : i(i) { } };
Wenzel Jakobf54ded72016-04-20 17:00:57 +020089
90 py::class_<Placeholder>(m2, "Placeholder")
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020091 .def(py::init<int>())
Wenzel Jakobf54ded72016-04-20 17:00:57 +020092 .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
93
94 // #171: Can't return reference wrappers (or STL datastructures containing them)
Wenzel Jakob85f07e12016-09-04 23:00:49 +090095 m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4) {
Wenzel Jakobf54ded72016-04-20 17:00:57 +020096 Placeholder *p1 = new Placeholder{1};
97 Placeholder *p2 = new Placeholder{2};
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020098 Placeholder *p3 = new Placeholder{3};
Wenzel Jakobf54ded72016-04-20 17:00:57 +020099 std::vector<std::reference_wrapper<Placeholder>> v;
100 v.push_back(std::ref(*p1));
101 v.push_back(std::ref(*p2));
102 v.push_back(std::ref(*p3));
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +0200103 v.push_back(p4);
Wenzel Jakobf54ded72016-04-20 17:00:57 +0200104 return v;
105 });
Wenzel Jakob6ca6e822016-04-27 14:33:52 +0200106
107 // #181: iterator passthrough did not compile
108 m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
109 return py::make_iterator(std::begin(s), std::end(s));
110 });
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200111
112 // #187: issue involving std::shared_ptr<> return value policy & garbage collection
113 struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
114 struct ElementA : ElementBase {
115 ElementA(int v) : v(v) { }
116 int value() { return v; }
117 int v;
118 };
119
120 struct ElementList {
121 void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
122 std::vector<std::shared_ptr<ElementBase>> l;
123 };
124
125 py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
126
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400127 py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m2, "ElementA")
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200128 .def(py::init<int>())
129 .def("value", &ElementA::value);
130
131 py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
132 .def(py::init<>())
133 .def("add", &ElementList::add)
Wenzel Jakob85f07e12016-09-04 23:00:49 +0900134 .def("get", [](ElementList &el) {
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200135 py::list list;
136 for (auto &e : el.l)
137 list.append(py::cast(e));
138 return list;
139 });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200140
141 // (no id): should not be able to pass 'None' to a reference argument
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200142 m2.def("get_element", [](ElementA &el) { return el.value(); });
Wenzel Jakob3f200fa2016-05-17 15:35:29 +0200143
144 // (no id): don't cast doubles to ints
145 m2.def("expect_float", [](float f) { return f; });
146 m2.def("expect_int", [](int i) { return i; });
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200147
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200148 try {
149 py::class_<Placeholder>(m2, "Placeholder");
150 throw std::logic_error("Expected an exception!");
Dean Moldovanf2b36c22016-06-01 23:03:10 +0200151 } catch (std::runtime_error &) {
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200152 /* All good */
153 }
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400154
155 // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
156 class StrIssue {
157 public:
158 StrIssue(int i) : val{i} {}
159 StrIssue() : StrIssue(-1) {}
160 int value() const { return val; }
161 private:
162 int val;
163 };
164 py::class_<StrIssue> si(m2, "StrIssue");
165 si .def(py::init<int>())
166 .def(py::init<>())
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200167 .def("__str__", [](const StrIssue &si) { return "StrIssue[" + std::to_string(si.value()) + "]"; })
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400168 ;
169
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400170 // Issue #328: first member in a class can't be used in operators
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -0400171 py::class_<NestABase>(m2, "NestABase").def(py::init<>()).def_readwrite("value", &NestABase::value);
172 py::class_<NestA>(m2, "NestA").def(py::init<>()).def(py::self += int())
173 .def("as_base", [](NestA &a) -> NestABase& { return (NestABase&) a; }, py::return_value_policy::reference_internal);
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400174 py::class_<NestB>(m2, "NestB").def(py::init<>()).def(py::self -= int()).def_readwrite("a", &NestB::a);
175 py::class_<NestC>(m2, "NestC").def(py::init<>()).def(py::self *= int()).def_readwrite("b", &NestC::b);
Dean Moldovan665e8802016-08-12 22:28:31 +0200176 m2.def("get_NestA", [](const NestA &a) { return a.value; });
177 m2.def("get_NestB", [](const NestB &b) { return b.value; });
178 m2.def("get_NestC", [](const NestC &c) { return c.value; });
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900179
180 // Issue 389: r_v_p::move should fall-through to copy on non-movable objects
181 class MoveIssue1 {
182 public:
183 MoveIssue1(int v) : v{v} {}
Dean Moldovan81511be2016-09-07 00:50:10 +0200184 MoveIssue1(const MoveIssue1 &c) { v = c.v; }
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900185 MoveIssue1(MoveIssue1 &&) = delete;
186 int v;
187 };
188 class MoveIssue2 {
189 public:
190 MoveIssue2(int v) : v{v} {}
191 MoveIssue2(MoveIssue2 &&) = default;
192 int v;
193 };
194 py::class_<MoveIssue1>(m2, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
195 py::class_<MoveIssue2>(m2, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
196 m2.def("get_moveissue1", [](int i) -> MoveIssue1 * { return new MoveIssue1(i); }, py::return_value_policy::move);
197 m2.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
Jason Rhinelander56f71772016-09-07 13:32:49 -0400198
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400199 // Issues 392/397: overridding reference-returning functions
Jason Rhinelander56f71772016-09-07 13:32:49 -0400200 class OverrideTest {
201 public:
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400202 struct A { std::string value = "hi"; };
203 std::string v;
Jason Rhinelander56f71772016-09-07 13:32:49 -0400204 A a;
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400205 explicit OverrideTest(const std::string &v) : v{v} {}
206 virtual std::string str_value() { return v; }
207 virtual std::string &str_ref() { return v; }
Jason Rhinelander56f71772016-09-07 13:32:49 -0400208 virtual A A_value() { return a; }
209 virtual A &A_ref() { return a; }
210 };
211 class PyOverrideTest : public OverrideTest {
212 public:
213 using OverrideTest::OverrideTest;
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400214 std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400215 // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
216 // to a python numeric value, since we only copy values in the numeric type caster:
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400217// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
218 // But we can work around it like this:
219 private:
220 std::string _tmp;
221 std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
222 public:
223 std::string &str_ref() override { return _tmp = str_ref_helper(); }
224
Jason Rhinelander56f71772016-09-07 13:32:49 -0400225 A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
226 A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
227 };
228 py::class_<OverrideTest::A>(m2, "OverrideTest_A")
229 .def_readwrite("value", &OverrideTest::A::value);
230 py::class_<OverrideTest, PyOverrideTest>(m2, "OverrideTest")
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400231 .def(py::init<const std::string &>())
232 .def("str_value", &OverrideTest::str_value)
233// .def("str_ref", &OverrideTest::str_ref)
Jason Rhinelander56f71772016-09-07 13:32:49 -0400234 .def("A_value", &OverrideTest::A_value)
235 .def("A_ref", &OverrideTest::A_ref);
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400236
Wenzel Jakob382484a2016-09-10 15:28:37 +0900237 /// Issue 393: need to return NotSupported to ensure correct arithmetic operator behavior
238 py::class_<OpTest1>(m2, "OpTest1")
239 .def(py::init<>())
240 .def(py::self + py::self);
241
242 py::class_<OpTest2>(m2, "OpTest2")
243 .def(py::init<>())
244 .def(py::self + py::self)
245 .def("__add__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; })
246 .def("__radd__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; });
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900247
248 // Issue 388: Can't make iterators via make_iterator() with different r/v policies
249 static std::vector<int> list = { 1, 2, 3 };
250 m2.def("make_iterator_1", []() { return py::make_iterator<py::return_value_policy::copy>(list); });
251 m2.def("make_iterator_2", []() { return py::make_iterator<py::return_value_policy::automatic>(list); });
Jason Rhinelander6873c202016-10-24 21:58:22 -0400252
253 static std::vector<std::string> nothrows;
254 // Issue 461: registering two things with the same name:
255 py::class_<Dupe1>(m2, "Dupe1")
256 .def("get_value", &Dupe1::get_value)
257 ;
258 m2.def("dupe1_factory", [](int v) { return new Dupe1(v); });
259
260 py::class_<Dupe2>(m2, "Dupe2");
261 py::exception<DupeException>(m2, "DupeException");
262
263 try {
264 m2.def("Dupe1", [](int v) { return new Dupe1(v); });
265 nothrows.emplace_back("Dupe1");
266 }
267 catch (std::runtime_error &) {}
268 try {
269 py::class_<Dupe3>(m2, "dupe1_factory");
270 nothrows.emplace_back("dupe1_factory");
271 }
272 catch (std::runtime_error &) {}
273 try {
274 py::exception<Dupe3>(m2, "Dupe2");
275 nothrows.emplace_back("Dupe2");
276 }
277 catch (std::runtime_error &) {}
278 try {
279 m2.def("DupeException", []() { return 30; });
280 nothrows.emplace_back("DupeException1");
281 }
282 catch (std::runtime_error &) {}
283 try {
284 py::class_<DupeException>(m2, "DupeException");
285 nothrows.emplace_back("DupeException2");
286 }
287 catch (std::runtime_error &) {}
288 m2.def("dupe_exception_failures", []() {
289 py::list l;
290 for (auto &e : nothrows) l.append(py::cast(e));
291 return l;
292 });
Wenzel Jakobbd560ac2016-11-03 11:53:35 +0100293
294 /// Issue #471: shared pointer instance not dellocated
295 class SharedChild : public std::enable_shared_from_this<SharedChild> {
296 public:
297 SharedChild() { print_created(this); }
298 ~SharedChild() { print_destroyed(this); }
299 };
300
301 class SharedParent {
302 public:
303 SharedParent() : child(std::make_shared<SharedChild>()) { }
304 const SharedChild &get_child() const { return *child; }
305
306 private:
307 std::shared_ptr<SharedChild> child;
308 };
309
310 py::class_<SharedChild, std::shared_ptr<SharedChild>>(m, "SharedChild");
311 py::class_<SharedParent, std::shared_ptr<SharedParent>>(m, "SharedParent")
312 .def(py::init<>())
313 .def("get_child", &SharedParent::get_child, py::return_value_policy::reference);
314};
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400315
Jason Rhinelander6873c202016-10-24 21:58:22 -0400316
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400317// MSVC workaround: trying to use a lambda here crashes MSCV
318test_initializer issues(&init_issues);