blob: 843978effa5317432539bc5c11afda2e410fda1c [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 +020015PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
16
Jason Rhinelander3f589372016-08-07 13:05:26 -040017#define TRACKERS(CLASS) CLASS() { print_default_created(this); } ~CLASS() { print_destroyed(this); }
18struct NestABase { int value = -2; TRACKERS(NestABase) };
19struct NestA : NestABase { int value = 3; NestA& operator+=(int i) { value += i; return *this; } TRACKERS(NestA) };
20struct NestB { NestA a; int value = 4; NestB& operator-=(int i) { value -= i; return *this; } TRACKERS(NestB) };
21struct NestC { NestB b; int value = 5; NestC& operator*=(int i) { value *= i; return *this; } TRACKERS(NestC) };
22
Wenzel Jakob382484a2016-09-10 15:28:37 +090023/// #393
24class OpTest1 {};
25class OpTest2 {};
26
27OpTest1 operator+(const OpTest1 &, const OpTest1 &) {
28 py::print("Add OpTest1 with OpTest1");
29 return OpTest1();
30}
31OpTest2 operator+(const OpTest2 &, const OpTest2 &) {
32 py::print("Add OpTest2 with OpTest2");
33 return OpTest2();
34}
35OpTest2 operator+(const OpTest2 &, const OpTest1 &) {
36 py::print("Add OpTest2 with OpTest1");
37 return OpTest2();
38}
39
Wenzel Jakob17cdb062016-03-10 13:24:10 +010040void init_issues(py::module &m) {
41 py::module m2 = m.def_submodule("issues");
42
Wenzel Jakob9059bd82016-05-01 10:39:45 +020043#if !defined(_MSC_VER)
44 // Visual Studio 2015 currently cannot compile this test
45 // (see the comment in type_caster_base::make_copy_constructor)
46 // #70 compilation issue if operator new is not public
47 class NonConstructible { private: void *operator new(size_t bytes) throw(); };
48 py::class_<NonConstructible>(m, "Foo");
Wenzel Jakobbd57eb42016-05-01 14:42:20 +020049 m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
Wenzel Jakob9059bd82016-05-01 10:39:45 +020050 py::return_value_policy::reference);
51#endif
52
Wenzel Jakob17cdb062016-03-10 13:24:10 +010053 // #137: const char* isn't handled properly
Dean Moldovan665e8802016-08-12 22:28:31 +020054 m2.def("print_cchar", [](const char *s) { return std::string(s); });
Wenzel Jakobd3349af2016-03-26 23:04:10 +010055
56 // #150: char bindings broken
Dean Moldovan665e8802016-08-12 22:28:31 +020057 m2.def("print_char", [](char c) { return std::string(1, c); });
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020058
59 // #159: virtual function dispatch has problems with similar-named functions
Dean Moldovan665e8802016-08-12 22:28:31 +020060 struct Base { virtual std::string dispatch() const {
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020061 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
Dean Moldovan665e8802016-08-12 22:28:31 +020062 return {};
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020063 }; };
Wenzel Jakobe7074972016-04-30 22:44:00 +020064
65 struct DispatchIssue : Base {
Dean Moldovan665e8802016-08-12 22:28:31 +020066 virtual std::string dispatch() const {
67 PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
Wenzel Jakobe7074972016-04-30 22:44:00 +020068 }
69 };
70
Jason Rhinelander5fffe202016-09-06 12:17:06 -040071 py::class_<Base, DispatchIssue>(m2, "DispatchIssue")
Wenzel Jakobf54ded72016-04-20 17:00:57 +020072 .def(py::init<>())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020073 .def("dispatch", &Base::dispatch);
74
Dean Moldovan665e8802016-08-12 22:28:31 +020075 m2.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
Wenzel Jakobe7074972016-04-30 22:44:00 +020076
77 struct Placeholder { int i; Placeholder(int i) : i(i) { } };
Wenzel Jakobf54ded72016-04-20 17:00:57 +020078
79 py::class_<Placeholder>(m2, "Placeholder")
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020080 .def(py::init<int>())
Wenzel Jakobf54ded72016-04-20 17:00:57 +020081 .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
82
83 // #171: Can't return reference wrappers (or STL datastructures containing them)
Wenzel Jakob85f07e12016-09-04 23:00:49 +090084 m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4) {
Wenzel Jakobf54ded72016-04-20 17:00:57 +020085 Placeholder *p1 = new Placeholder{1};
86 Placeholder *p2 = new Placeholder{2};
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020087 Placeholder *p3 = new Placeholder{3};
Wenzel Jakobf54ded72016-04-20 17:00:57 +020088 std::vector<std::reference_wrapper<Placeholder>> v;
89 v.push_back(std::ref(*p1));
90 v.push_back(std::ref(*p2));
91 v.push_back(std::ref(*p3));
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020092 v.push_back(p4);
Wenzel Jakobf54ded72016-04-20 17:00:57 +020093 return v;
94 });
Wenzel Jakob6ca6e822016-04-27 14:33:52 +020095
96 // #181: iterator passthrough did not compile
97 m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
98 return py::make_iterator(std::begin(s), std::end(s));
99 });
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200100
101 // #187: issue involving std::shared_ptr<> return value policy & garbage collection
102 struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
103 struct ElementA : ElementBase {
104 ElementA(int v) : v(v) { }
105 int value() { return v; }
106 int v;
107 };
108
109 struct ElementList {
110 void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
111 std::vector<std::shared_ptr<ElementBase>> l;
112 };
113
114 py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
115
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400116 py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m2, "ElementA")
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200117 .def(py::init<int>())
118 .def("value", &ElementA::value);
119
120 py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
121 .def(py::init<>())
122 .def("add", &ElementList::add)
Wenzel Jakob85f07e12016-09-04 23:00:49 +0900123 .def("get", [](ElementList &el) {
Wenzel Jakobd2b628b2016-04-30 23:02:39 +0200124 py::list list;
125 for (auto &e : el.l)
126 list.append(py::cast(e));
127 return list;
128 });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200129
130 // (no id): should not be able to pass 'None' to a reference argument
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200131 m2.def("get_element", [](ElementA &el) { return el.value(); });
Wenzel Jakob3f200fa2016-05-17 15:35:29 +0200132
133 // (no id): don't cast doubles to ints
134 m2.def("expect_float", [](float f) { return f; });
135 m2.def("expect_int", [](int i) { return i; });
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200136
137 // (no id): don't invoke Python dispatch code when instantiating C++
138 // classes that were not extended on the Python side
139 struct A {
140 virtual ~A() {}
Dean Moldovan81511be2016-09-07 00:50:10 +0200141 virtual void f() { py::print("A.f()"); }
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200142 };
143
144 struct PyA : A {
Dean Moldovan81511be2016-09-07 00:50:10 +0200145 PyA() { py::print("PyA.PyA()"); }
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200146
147 void f() override {
Dean Moldovan81511be2016-09-07 00:50:10 +0200148 py::print("PyA.f()");
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200149 PYBIND11_OVERLOAD(void, A, f);
150 }
151 };
152
153 auto call_f = [](A *a) { a->f(); };
154
Wenzel Jakobfb0e2e52016-07-01 14:54:24 +0200155 pybind11::class_<A, std::unique_ptr<A>, PyA>(m2, "A")
156 .def(py::init<>())
157 .def("f", &A::f);
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200158
Wenzel Jakobfb0e2e52016-07-01 14:54:24 +0200159 m2.def("call_f", call_f);
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200160
161 try {
162 py::class_<Placeholder>(m2, "Placeholder");
163 throw std::logic_error("Expected an exception!");
Dean Moldovanf2b36c22016-06-01 23:03:10 +0200164 } catch (std::runtime_error &) {
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200165 /* All good */
166 }
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400167
168 // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
169 class StrIssue {
170 public:
171 StrIssue(int i) : val{i} {}
172 StrIssue() : StrIssue(-1) {}
173 int value() const { return val; }
174 private:
175 int val;
176 };
177 py::class_<StrIssue> si(m2, "StrIssue");
178 si .def(py::init<int>())
179 .def(py::init<>())
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200180 .def("__str__", [](const StrIssue &si) { return "StrIssue[" + std::to_string(si.value()) + "]"; })
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400181 ;
182
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400183 // Issue #328: first member in a class can't be used in operators
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -0400184 py::class_<NestABase>(m2, "NestABase").def(py::init<>()).def_readwrite("value", &NestABase::value);
185 py::class_<NestA>(m2, "NestA").def(py::init<>()).def(py::self += int())
186 .def("as_base", [](NestA &a) -> NestABase& { return (NestABase&) a; }, py::return_value_policy::reference_internal);
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400187 py::class_<NestB>(m2, "NestB").def(py::init<>()).def(py::self -= int()).def_readwrite("a", &NestB::a);
188 py::class_<NestC>(m2, "NestC").def(py::init<>()).def(py::self *= int()).def_readwrite("b", &NestC::b);
Dean Moldovan665e8802016-08-12 22:28:31 +0200189 m2.def("get_NestA", [](const NestA &a) { return a.value; });
190 m2.def("get_NestB", [](const NestB &b) { return b.value; });
191 m2.def("get_NestC", [](const NestC &c) { return c.value; });
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900192
193 // Issue 389: r_v_p::move should fall-through to copy on non-movable objects
194 class MoveIssue1 {
195 public:
196 MoveIssue1(int v) : v{v} {}
Dean Moldovan81511be2016-09-07 00:50:10 +0200197 MoveIssue1(const MoveIssue1 &c) { v = c.v; }
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900198 MoveIssue1(MoveIssue1 &&) = delete;
199 int v;
200 };
201 class MoveIssue2 {
202 public:
203 MoveIssue2(int v) : v{v} {}
204 MoveIssue2(MoveIssue2 &&) = default;
205 int v;
206 };
207 py::class_<MoveIssue1>(m2, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
208 py::class_<MoveIssue2>(m2, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
209 m2.def("get_moveissue1", [](int i) -> MoveIssue1 * { return new MoveIssue1(i); }, py::return_value_policy::move);
210 m2.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
Jason Rhinelander56f71772016-09-07 13:32:49 -0400211
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400212 // Issues 392/397: overridding reference-returning functions
Jason Rhinelander56f71772016-09-07 13:32:49 -0400213 class OverrideTest {
214 public:
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400215 struct A { std::string value = "hi"; };
216 std::string v;
Jason Rhinelander56f71772016-09-07 13:32:49 -0400217 A a;
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400218 explicit OverrideTest(const std::string &v) : v{v} {}
219 virtual std::string str_value() { return v; }
220 virtual std::string &str_ref() { return v; }
Jason Rhinelander56f71772016-09-07 13:32:49 -0400221 virtual A A_value() { return a; }
222 virtual A &A_ref() { return a; }
223 };
224 class PyOverrideTest : public OverrideTest {
225 public:
226 using OverrideTest::OverrideTest;
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400227 std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400228 // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
229 // to a python numeric value, since we only copy values in the numeric type caster:
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400230// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
231 // But we can work around it like this:
232 private:
233 std::string _tmp;
234 std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
235 public:
236 std::string &str_ref() override { return _tmp = str_ref_helper(); }
237
Jason Rhinelander56f71772016-09-07 13:32:49 -0400238 A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
239 A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
240 };
241 py::class_<OverrideTest::A>(m2, "OverrideTest_A")
242 .def_readwrite("value", &OverrideTest::A::value);
243 py::class_<OverrideTest, PyOverrideTest>(m2, "OverrideTest")
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400244 .def(py::init<const std::string &>())
245 .def("str_value", &OverrideTest::str_value)
246// .def("str_ref", &OverrideTest::str_ref)
Jason Rhinelander56f71772016-09-07 13:32:49 -0400247 .def("A_value", &OverrideTest::A_value)
248 .def("A_ref", &OverrideTest::A_ref);
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400249
Wenzel Jakob382484a2016-09-10 15:28:37 +0900250 /// Issue 393: need to return NotSupported to ensure correct arithmetic operator behavior
251 py::class_<OpTest1>(m2, "OpTest1")
252 .def(py::init<>())
253 .def(py::self + py::self);
254
255 py::class_<OpTest2>(m2, "OpTest2")
256 .def(py::init<>())
257 .def(py::self + py::self)
258 .def("__add__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; })
259 .def("__radd__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200260}
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400261
262// MSVC workaround: trying to use a lambda here crashes MSCV
263test_initializer issues(&init_issues);