blob: 29c4057f1b0b137956e9d4d557ac8a2c1bb3ad33 [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
Wenzel Jakob17cdb062016-03-10 13:24:10 +010039void init_issues(py::module &m) {
40 py::module m2 = m.def_submodule("issues");
41
Wenzel Jakob9059bd82016-05-01 10:39:45 +020042#if !defined(_MSC_VER)
43 // Visual Studio 2015 currently cannot compile this test
44 // (see the comment in type_caster_base::make_copy_constructor)
45 // #70 compilation issue if operator new is not public
46 class NonConstructible { private: void *operator new(size_t bytes) throw(); };
47 py::class_<NonConstructible>(m, "Foo");
Wenzel Jakobbd57eb42016-05-01 14:42:20 +020048 m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
Wenzel Jakob9059bd82016-05-01 10:39:45 +020049 py::return_value_policy::reference);
50#endif
51
Wenzel Jakob17cdb062016-03-10 13:24:10 +010052 // #137: const char* isn't handled properly
Dean Moldovan665e8802016-08-12 22:28:31 +020053 m2.def("print_cchar", [](const char *s) { return std::string(s); });
Wenzel Jakobd3349af2016-03-26 23:04:10 +010054
55 // #150: char bindings broken
Dean Moldovan665e8802016-08-12 22:28:31 +020056 m2.def("print_char", [](char c) { return std::string(1, c); });
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020057
58 // #159: virtual function dispatch has problems with similar-named functions
Dean Moldovan665e8802016-08-12 22:28:31 +020059 struct Base { virtual std::string dispatch() const {
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020060 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
Dean Moldovan665e8802016-08-12 22:28:31 +020061 return {};
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020062 }; };
Wenzel Jakobe7074972016-04-30 22:44:00 +020063
64 struct DispatchIssue : Base {
Dean Moldovan665e8802016-08-12 22:28:31 +020065 virtual std::string dispatch() const {
66 PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
Wenzel Jakobe7074972016-04-30 22:44:00 +020067 }
68 };
69
Jason Rhinelander5fffe202016-09-06 12:17:06 -040070 py::class_<Base, DispatchIssue>(m2, "DispatchIssue")
Wenzel Jakobf54ded72016-04-20 17:00:57 +020071 .def(py::init<>())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020072 .def("dispatch", &Base::dispatch);
73
Dean Moldovan665e8802016-08-12 22:28:31 +020074 m2.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
Wenzel Jakobe7074972016-04-30 22:44:00 +020075
76 struct Placeholder { int i; Placeholder(int i) : i(i) { } };
Wenzel Jakobf54ded72016-04-20 17:00:57 +020077
78 py::class_<Placeholder>(m2, "Placeholder")
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020079 .def(py::init<int>())
Wenzel Jakobf54ded72016-04-20 17:00:57 +020080 .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
81
82 // #171: Can't return reference wrappers (or STL datastructures containing them)
Wenzel Jakob85f07e12016-09-04 23:00:49 +090083 m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4) {
Wenzel Jakobf54ded72016-04-20 17:00:57 +020084 Placeholder *p1 = new Placeholder{1};
85 Placeholder *p2 = new Placeholder{2};
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020086 Placeholder *p3 = new Placeholder{3};
Wenzel Jakobf54ded72016-04-20 17:00:57 +020087 std::vector<std::reference_wrapper<Placeholder>> v;
88 v.push_back(std::ref(*p1));
89 v.push_back(std::ref(*p2));
90 v.push_back(std::ref(*p3));
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020091 v.push_back(p4);
Wenzel Jakobf54ded72016-04-20 17:00:57 +020092 return v;
93 });
Wenzel Jakob6ca6e822016-04-27 14:33:52 +020094
95 // #181: iterator passthrough did not compile
96 m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
97 return py::make_iterator(std::begin(s), std::end(s));
98 });
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020099
100 // #187: issue involving std::shared_ptr<> return value policy & garbage collection
101 struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
102 struct ElementA : ElementBase {
103 ElementA(int v) : v(v) { }
104 int value() { return v; }
105 int v;
106 };
107
108 struct ElementList {
109 void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
110 std::vector<std::shared_ptr<ElementBase>> l;
111 };
112
113 py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
114
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400115 py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m2, "ElementA")
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200116 .def(py::init<int>())
117 .def("value", &ElementA::value);
118
119 py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
120 .def(py::init<>())
121 .def("add", &ElementList::add)
Wenzel Jakob85f07e12016-09-04 23:00:49 +0900122 .def("get", [](ElementList &el) {
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200123 py::list list;
124 for (auto &e : el.l)
125 list.append(py::cast(e));
126 return list;
127 });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200128
129 // (no id): should not be able to pass 'None' to a reference argument
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200130 m2.def("get_element", [](ElementA &el) { return el.value(); });
Wenzel Jakob3f200fa2016-05-17 15:35:29 +0200131
132 // (no id): don't cast doubles to ints
133 m2.def("expect_float", [](float f) { return f; });
134 m2.def("expect_int", [](int i) { return i; });
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200135
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200136 try {
137 py::class_<Placeholder>(m2, "Placeholder");
138 throw std::logic_error("Expected an exception!");
Dean Moldovanf2b36c22016-06-01 23:03:10 +0200139 } catch (std::runtime_error &) {
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200140 /* All good */
141 }
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400142
143 // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
144 class StrIssue {
145 public:
146 StrIssue(int i) : val{i} {}
147 StrIssue() : StrIssue(-1) {}
148 int value() const { return val; }
149 private:
150 int val;
151 };
152 py::class_<StrIssue> si(m2, "StrIssue");
153 si .def(py::init<int>())
154 .def(py::init<>())
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200155 .def("__str__", [](const StrIssue &si) { return "StrIssue[" + std::to_string(si.value()) + "]"; })
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400156 ;
157
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400158 // Issue #328: first member in a class can't be used in operators
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -0400159 py::class_<NestABase>(m2, "NestABase").def(py::init<>()).def_readwrite("value", &NestABase::value);
160 py::class_<NestA>(m2, "NestA").def(py::init<>()).def(py::self += int())
161 .def("as_base", [](NestA &a) -> NestABase& { return (NestABase&) a; }, py::return_value_policy::reference_internal);
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400162 py::class_<NestB>(m2, "NestB").def(py::init<>()).def(py::self -= int()).def_readwrite("a", &NestB::a);
163 py::class_<NestC>(m2, "NestC").def(py::init<>()).def(py::self *= int()).def_readwrite("b", &NestC::b);
Dean Moldovan665e8802016-08-12 22:28:31 +0200164 m2.def("get_NestA", [](const NestA &a) { return a.value; });
165 m2.def("get_NestB", [](const NestB &b) { return b.value; });
166 m2.def("get_NestC", [](const NestC &c) { return c.value; });
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900167
168 // Issue 389: r_v_p::move should fall-through to copy on non-movable objects
169 class MoveIssue1 {
170 public:
171 MoveIssue1(int v) : v{v} {}
Dean Moldovan81511be2016-09-07 00:50:10 +0200172 MoveIssue1(const MoveIssue1 &c) { v = c.v; }
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900173 MoveIssue1(MoveIssue1 &&) = delete;
174 int v;
175 };
176 class MoveIssue2 {
177 public:
178 MoveIssue2(int v) : v{v} {}
179 MoveIssue2(MoveIssue2 &&) = default;
180 int v;
181 };
182 py::class_<MoveIssue1>(m2, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
183 py::class_<MoveIssue2>(m2, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
184 m2.def("get_moveissue1", [](int i) -> MoveIssue1 * { return new MoveIssue1(i); }, py::return_value_policy::move);
185 m2.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
Jason Rhinelander56f71772016-09-07 13:32:49 -0400186
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400187 // Issues 392/397: overridding reference-returning functions
Jason Rhinelander56f71772016-09-07 13:32:49 -0400188 class OverrideTest {
189 public:
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400190 struct A { std::string value = "hi"; };
191 std::string v;
Jason Rhinelander56f71772016-09-07 13:32:49 -0400192 A a;
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400193 explicit OverrideTest(const std::string &v) : v{v} {}
194 virtual std::string str_value() { return v; }
195 virtual std::string &str_ref() { return v; }
Jason Rhinelander56f71772016-09-07 13:32:49 -0400196 virtual A A_value() { return a; }
197 virtual A &A_ref() { return a; }
198 };
199 class PyOverrideTest : public OverrideTest {
200 public:
201 using OverrideTest::OverrideTest;
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400202 std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400203 // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
204 // to a python numeric value, since we only copy values in the numeric type caster:
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400205// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
206 // But we can work around it like this:
207 private:
208 std::string _tmp;
209 std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
210 public:
211 std::string &str_ref() override { return _tmp = str_ref_helper(); }
212
Jason Rhinelander56f71772016-09-07 13:32:49 -0400213 A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
214 A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
215 };
216 py::class_<OverrideTest::A>(m2, "OverrideTest_A")
217 .def_readwrite("value", &OverrideTest::A::value);
218 py::class_<OverrideTest, PyOverrideTest>(m2, "OverrideTest")
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400219 .def(py::init<const std::string &>())
220 .def("str_value", &OverrideTest::str_value)
221// .def("str_ref", &OverrideTest::str_ref)
Jason Rhinelander56f71772016-09-07 13:32:49 -0400222 .def("A_value", &OverrideTest::A_value)
223 .def("A_ref", &OverrideTest::A_ref);
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400224
Wenzel Jakob382484a2016-09-10 15:28:37 +0900225 /// Issue 393: need to return NotSupported to ensure correct arithmetic operator behavior
226 py::class_<OpTest1>(m2, "OpTest1")
227 .def(py::init<>())
228 .def(py::self + py::self);
229
230 py::class_<OpTest2>(m2, "OpTest2")
231 .def(py::init<>())
232 .def(py::self + py::self)
233 .def("__add__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; })
234 .def("__radd__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; });
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900235
236 // Issue 388: Can't make iterators via make_iterator() with different r/v policies
237 static std::vector<int> list = { 1, 2, 3 };
238 m2.def("make_iterator_1", []() { return py::make_iterator<py::return_value_policy::copy>(list); });
239 m2.def("make_iterator_2", []() { return py::make_iterator<py::return_value_policy::automatic>(list); });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200240}
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400241
242// MSVC workaround: trying to use a lambda here crashes MSCV
243test_initializer issues(&init_issues);