Wenzel Jakob | 17cdb06 | 2016-03-10 13:24:10 +0100 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/test_issues.cpp -- collection of testcases for miscellaneous issues |
Wenzel Jakob | 17cdb06 | 2016-03-10 13:24:10 +0100 | [diff] [blame] | 3 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | 17cdb06 | 2016-03-10 13:24:10 +0100 | [diff] [blame] | 5 | |
| 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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | #include "pybind11_tests.h" |
| 11 | #include "constructor_stats.h" |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 12 | #include <pybind11/stl.h> |
Jason Rhinelander | 1b05ce5 | 2016-08-09 17:57:59 -0400 | [diff] [blame] | 13 | #include <pybind11/operators.h> |
Wenzel Jakob | fe40dfe | 2016-11-07 15:59:01 +0100 | [diff] [blame] | 14 | #include <pybind11/complex.h> |
Wenzel Jakob | d2b628b | 2016-04-30 23:02:39 +0200 | [diff] [blame] | 15 | |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 16 | #define TRACKERS(CLASS) CLASS() { print_default_created(this); } ~CLASS() { print_destroyed(this); } |
| 17 | struct NestABase { int value = -2; TRACKERS(NestABase) }; |
| 18 | struct NestA : NestABase { int value = 3; NestA& operator+=(int i) { value += i; return *this; } TRACKERS(NestA) }; |
| 19 | struct NestB { NestA a; int value = 4; NestB& operator-=(int i) { value -= i; return *this; } TRACKERS(NestB) }; |
| 20 | struct NestC { NestB b; int value = 5; NestC& operator*=(int i) { value *= i; return *this; } TRACKERS(NestC) }; |
| 21 | |
Wenzel Jakob | 382484a | 2016-09-10 15:28:37 +0900 | [diff] [blame] | 22 | /// #393 |
| 23 | class OpTest1 {}; |
| 24 | class OpTest2 {}; |
| 25 | |
| 26 | OpTest1 operator+(const OpTest1 &, const OpTest1 &) { |
| 27 | py::print("Add OpTest1 with OpTest1"); |
| 28 | return OpTest1(); |
| 29 | } |
| 30 | OpTest2 operator+(const OpTest2 &, const OpTest2 &) { |
| 31 | py::print("Add OpTest2 with OpTest2"); |
| 32 | return OpTest2(); |
| 33 | } |
| 34 | OpTest2 operator+(const OpTest2 &, const OpTest1 &) { |
| 35 | py::print("Add OpTest2 with OpTest1"); |
| 36 | return OpTest2(); |
| 37 | } |
| 38 | |
Jason Rhinelander | 6873c20 | 2016-10-24 21:58:22 -0400 | [diff] [blame] | 39 | // #461 |
| 40 | class Dupe1 { |
| 41 | public: |
| 42 | Dupe1(int v) : v_{v} {} |
| 43 | int get_value() const { return v_; } |
| 44 | private: |
| 45 | int v_; |
| 46 | }; |
| 47 | class Dupe2 {}; |
| 48 | class Dupe3 {}; |
| 49 | class DupeException : public std::runtime_error {}; |
| 50 | |
Jason Rhinelander | c07ec31 | 2016-11-06 13:12:48 -0500 | [diff] [blame] | 51 | // #478 |
| 52 | template <typename T> class custom_unique_ptr { |
| 53 | public: |
| 54 | custom_unique_ptr() { print_default_created(this); } |
| 55 | custom_unique_ptr(T *ptr) : _ptr{ptr} { print_created(this, ptr); } |
| 56 | custom_unique_ptr(custom_unique_ptr<T> &&move) : _ptr{move._ptr} { move._ptr = nullptr; print_move_created(this); } |
| 57 | custom_unique_ptr &operator=(custom_unique_ptr<T> &&move) { print_move_assigned(this); if (_ptr) destruct_ptr(); _ptr = move._ptr; move._ptr = nullptr; return *this; } |
| 58 | custom_unique_ptr(const custom_unique_ptr<T> &) = delete; |
| 59 | void operator=(const custom_unique_ptr<T> ©) = delete; |
| 60 | ~custom_unique_ptr() { print_destroyed(this); if (_ptr) destruct_ptr(); } |
| 61 | private: |
| 62 | T *_ptr = nullptr; |
| 63 | void destruct_ptr() { delete _ptr; } |
| 64 | }; |
| 65 | PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>); |
| 66 | |
Jason Rhinelander | f200493 | 2016-11-25 07:06:18 -0500 | [diff] [blame] | 67 | /// Issue #528: templated constructor |
| 68 | struct TplConstrClass { |
| 69 | template <typename T> TplConstrClass(const T &arg) : str{arg} {} |
| 70 | std::string str; |
| 71 | bool operator==(const TplConstrClass &t) const { return t.str == str; } |
| 72 | }; |
| 73 | namespace std { |
| 74 | template <> struct hash<TplConstrClass> { size_t operator()(const TplConstrClass &t) const { return std::hash<std::string>()(t.str); } }; |
| 75 | } |
Jason Rhinelander | c07ec31 | 2016-11-06 13:12:48 -0500 | [diff] [blame] | 76 | |
Wenzel Jakob | 17cdb06 | 2016-03-10 13:24:10 +0100 | [diff] [blame] | 77 | void init_issues(py::module &m) { |
| 78 | py::module m2 = m.def_submodule("issues"); |
| 79 | |
Wenzel Jakob | 9059bd8 | 2016-05-01 10:39:45 +0200 | [diff] [blame] | 80 | #if !defined(_MSC_VER) |
| 81 | // Visual Studio 2015 currently cannot compile this test |
| 82 | // (see the comment in type_caster_base::make_copy_constructor) |
| 83 | // #70 compilation issue if operator new is not public |
| 84 | class NonConstructible { private: void *operator new(size_t bytes) throw(); }; |
| 85 | py::class_<NonConstructible>(m, "Foo"); |
Wenzel Jakob | bd57eb4 | 2016-05-01 14:42:20 +0200 | [diff] [blame] | 86 | m2.def("getstmt", []() -> NonConstructible * { return nullptr; }, |
Wenzel Jakob | 9059bd8 | 2016-05-01 10:39:45 +0200 | [diff] [blame] | 87 | py::return_value_policy::reference); |
| 88 | #endif |
| 89 | |
Wenzel Jakob | 17cdb06 | 2016-03-10 13:24:10 +0100 | [diff] [blame] | 90 | // #137: const char* isn't handled properly |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 91 | m2.def("print_cchar", [](const char *s) { return std::string(s); }); |
Wenzel Jakob | d3349af | 2016-03-26 23:04:10 +0100 | [diff] [blame] | 92 | |
| 93 | // #150: char bindings broken |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 94 | m2.def("print_char", [](char c) { return std::string(1, c); }); |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 95 | |
| 96 | // #159: virtual function dispatch has problems with similar-named functions |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 97 | struct Base { virtual std::string dispatch() const { |
Wenzel Jakob | d2b628b | 2016-04-30 23:02:39 +0200 | [diff] [blame] | 98 | /* for some reason MSVC2015 can't compile this if the function is pure virtual */ |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 99 | return {}; |
Wenzel Jakob | d2b628b | 2016-04-30 23:02:39 +0200 | [diff] [blame] | 100 | }; }; |
Wenzel Jakob | e707497 | 2016-04-30 22:44:00 +0200 | [diff] [blame] | 101 | |
| 102 | struct DispatchIssue : Base { |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 103 | virtual std::string dispatch() const { |
| 104 | PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */); |
Wenzel Jakob | e707497 | 2016-04-30 22:44:00 +0200 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 108 | py::class_<Base, DispatchIssue>(m2, "DispatchIssue") |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 109 | .def(py::init<>()) |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 110 | .def("dispatch", &Base::dispatch); |
| 111 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 112 | m2.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); }); |
Wenzel Jakob | e707497 | 2016-04-30 22:44:00 +0200 | [diff] [blame] | 113 | |
| 114 | struct Placeholder { int i; Placeholder(int i) : i(i) { } }; |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 115 | |
| 116 | py::class_<Placeholder>(m2, "Placeholder") |
Wenzel Jakob | dbe43ff | 2016-04-21 12:21:14 +0200 | [diff] [blame] | 117 | .def(py::init<int>()) |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 118 | .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; }); |
| 119 | |
| 120 | // #171: Can't return reference wrappers (or STL datastructures containing them) |
Wenzel Jakob | 85f07e1 | 2016-09-04 23:00:49 +0900 | [diff] [blame] | 121 | m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4) { |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 122 | Placeholder *p1 = new Placeholder{1}; |
| 123 | Placeholder *p2 = new Placeholder{2}; |
Wenzel Jakob | dbe43ff | 2016-04-21 12:21:14 +0200 | [diff] [blame] | 124 | Placeholder *p3 = new Placeholder{3}; |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 125 | std::vector<std::reference_wrapper<Placeholder>> v; |
| 126 | v.push_back(std::ref(*p1)); |
| 127 | v.push_back(std::ref(*p2)); |
| 128 | v.push_back(std::ref(*p3)); |
Wenzel Jakob | dbe43ff | 2016-04-21 12:21:14 +0200 | [diff] [blame] | 129 | v.push_back(p4); |
Wenzel Jakob | f54ded7 | 2016-04-20 17:00:57 +0200 | [diff] [blame] | 130 | return v; |
| 131 | }); |
Wenzel Jakob | 6ca6e82 | 2016-04-27 14:33:52 +0200 | [diff] [blame] | 132 | |
| 133 | // #181: iterator passthrough did not compile |
| 134 | m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator { |
| 135 | return py::make_iterator(std::begin(s), std::end(s)); |
| 136 | }); |
Wenzel Jakob | d2b628b | 2016-04-30 23:02:39 +0200 | [diff] [blame] | 137 | |
| 138 | // #187: issue involving std::shared_ptr<> return value policy & garbage collection |
| 139 | struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ }; |
| 140 | struct ElementA : ElementBase { |
| 141 | ElementA(int v) : v(v) { } |
| 142 | int value() { return v; } |
| 143 | int v; |
| 144 | }; |
| 145 | |
| 146 | struct ElementList { |
| 147 | void add(std::shared_ptr<ElementBase> e) { l.push_back(e); } |
| 148 | std::vector<std::shared_ptr<ElementBase>> l; |
| 149 | }; |
| 150 | |
| 151 | py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase"); |
| 152 | |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 153 | py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m2, "ElementA") |
Wenzel Jakob | d2b628b | 2016-04-30 23:02:39 +0200 | [diff] [blame] | 154 | .def(py::init<int>()) |
| 155 | .def("value", &ElementA::value); |
| 156 | |
| 157 | py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList") |
| 158 | .def(py::init<>()) |
| 159 | .def("add", &ElementList::add) |
Wenzel Jakob | 85f07e1 | 2016-09-04 23:00:49 +0900 | [diff] [blame] | 160 | .def("get", [](ElementList &el) { |
Wenzel Jakob | d2b628b | 2016-04-30 23:02:39 +0200 | [diff] [blame] | 161 | py::list list; |
| 162 | for (auto &e : el.l) |
| 163 | list.append(py::cast(e)); |
| 164 | return list; |
| 165 | }); |
Wenzel Jakob | bd57eb4 | 2016-05-01 14:42:20 +0200 | [diff] [blame] | 166 | |
| 167 | // (no id): should not be able to pass 'None' to a reference argument |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 168 | m2.def("get_element", [](ElementA &el) { return el.value(); }); |
Wenzel Jakob | 3f200fa | 2016-05-17 15:35:29 +0200 | [diff] [blame] | 169 | |
| 170 | // (no id): don't cast doubles to ints |
| 171 | m2.def("expect_float", [](float f) { return f; }); |
| 172 | m2.def("expect_int", [](int i) { return i; }); |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 173 | |
Wenzel Jakob | 38d8b8c | 2016-05-31 09:53:28 +0200 | [diff] [blame] | 174 | try { |
| 175 | py::class_<Placeholder>(m2, "Placeholder"); |
| 176 | throw std::logic_error("Expected an exception!"); |
Dean Moldovan | f2b36c2 | 2016-06-01 23:03:10 +0200 | [diff] [blame] | 177 | } catch (std::runtime_error &) { |
Wenzel Jakob | 38d8b8c | 2016-05-31 09:53:28 +0200 | [diff] [blame] | 178 | /* All good */ |
| 179 | } |
Jason Rhinelander | 4e45e18 | 2016-07-17 17:43:00 -0400 | [diff] [blame] | 180 | |
| 181 | // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid |
| 182 | class StrIssue { |
| 183 | public: |
| 184 | StrIssue(int i) : val{i} {} |
| 185 | StrIssue() : StrIssue(-1) {} |
| 186 | int value() const { return val; } |
| 187 | private: |
| 188 | int val; |
| 189 | }; |
| 190 | py::class_<StrIssue> si(m2, "StrIssue"); |
| 191 | si .def(py::init<int>()) |
| 192 | .def(py::init<>()) |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 193 | .def("__str__", [](const StrIssue &si) { return "StrIssue[" + std::to_string(si.value()) + "]"; }) |
Jason Rhinelander | 4e45e18 | 2016-07-17 17:43:00 -0400 | [diff] [blame] | 194 | ; |
| 195 | |
Jason Rhinelander | 1b05ce5 | 2016-08-09 17:57:59 -0400 | [diff] [blame] | 196 | // Issue #328: first member in a class can't be used in operators |
Jason Rhinelander | f2ecd89 | 2016-08-10 12:08:04 -0400 | [diff] [blame] | 197 | py::class_<NestABase>(m2, "NestABase").def(py::init<>()).def_readwrite("value", &NestABase::value); |
| 198 | py::class_<NestA>(m2, "NestA").def(py::init<>()).def(py::self += int()) |
| 199 | .def("as_base", [](NestA &a) -> NestABase& { return (NestABase&) a; }, py::return_value_policy::reference_internal); |
Jason Rhinelander | 1b05ce5 | 2016-08-09 17:57:59 -0400 | [diff] [blame] | 200 | py::class_<NestB>(m2, "NestB").def(py::init<>()).def(py::self -= int()).def_readwrite("a", &NestB::a); |
| 201 | py::class_<NestC>(m2, "NestC").def(py::init<>()).def(py::self *= int()).def_readwrite("b", &NestC::b); |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 202 | m2.def("get_NestA", [](const NestA &a) { return a.value; }); |
| 203 | m2.def("get_NestB", [](const NestB &b) { return b.value; }); |
| 204 | m2.def("get_NestC", [](const NestC &c) { return c.value; }); |
Wenzel Jakob | c84b37b | 2016-09-07 00:47:17 +0900 | [diff] [blame] | 205 | |
| 206 | // Issue 389: r_v_p::move should fall-through to copy on non-movable objects |
| 207 | class MoveIssue1 { |
| 208 | public: |
| 209 | MoveIssue1(int v) : v{v} {} |
Dean Moldovan | 81511be | 2016-09-07 00:50:10 +0200 | [diff] [blame] | 210 | MoveIssue1(const MoveIssue1 &c) { v = c.v; } |
Wenzel Jakob | c84b37b | 2016-09-07 00:47:17 +0900 | [diff] [blame] | 211 | MoveIssue1(MoveIssue1 &&) = delete; |
| 212 | int v; |
| 213 | }; |
| 214 | class MoveIssue2 { |
| 215 | public: |
| 216 | MoveIssue2(int v) : v{v} {} |
| 217 | MoveIssue2(MoveIssue2 &&) = default; |
| 218 | int v; |
| 219 | }; |
| 220 | py::class_<MoveIssue1>(m2, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v); |
| 221 | py::class_<MoveIssue2>(m2, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v); |
| 222 | m2.def("get_moveissue1", [](int i) -> MoveIssue1 * { return new MoveIssue1(i); }, py::return_value_policy::move); |
| 223 | m2.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move); |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 224 | |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 225 | // Issues 392/397: overridding reference-returning functions |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 226 | class OverrideTest { |
| 227 | public: |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 228 | struct A { std::string value = "hi"; }; |
| 229 | std::string v; |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 230 | A a; |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 231 | explicit OverrideTest(const std::string &v) : v{v} {} |
| 232 | virtual std::string str_value() { return v; } |
| 233 | virtual std::string &str_ref() { return v; } |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 234 | virtual A A_value() { return a; } |
| 235 | virtual A &A_ref() { return a; } |
| 236 | }; |
| 237 | class PyOverrideTest : public OverrideTest { |
| 238 | public: |
| 239 | using OverrideTest::OverrideTest; |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 240 | std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); } |
Jason Rhinelander | c03db9b | 2016-09-07 13:38:32 -0400 | [diff] [blame] | 241 | // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference |
| 242 | // to a python numeric value, since we only copy values in the numeric type caster: |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 243 | // std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); } |
| 244 | // But we can work around it like this: |
| 245 | private: |
| 246 | std::string _tmp; |
| 247 | std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); } |
| 248 | public: |
| 249 | std::string &str_ref() override { return _tmp = str_ref_helper(); } |
| 250 | |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 251 | A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); } |
| 252 | A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); } |
| 253 | }; |
| 254 | py::class_<OverrideTest::A>(m2, "OverrideTest_A") |
| 255 | .def_readwrite("value", &OverrideTest::A::value); |
| 256 | py::class_<OverrideTest, PyOverrideTest>(m2, "OverrideTest") |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 257 | .def(py::init<const std::string &>()) |
| 258 | .def("str_value", &OverrideTest::str_value) |
| 259 | // .def("str_ref", &OverrideTest::str_ref) |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 260 | .def("A_value", &OverrideTest::A_value) |
| 261 | .def("A_ref", &OverrideTest::A_ref); |
Jason Rhinelander | c03db9b | 2016-09-07 13:38:32 -0400 | [diff] [blame] | 262 | |
Wenzel Jakob | 382484a | 2016-09-10 15:28:37 +0900 | [diff] [blame] | 263 | /// Issue 393: need to return NotSupported to ensure correct arithmetic operator behavior |
| 264 | py::class_<OpTest1>(m2, "OpTest1") |
| 265 | .def(py::init<>()) |
| 266 | .def(py::self + py::self); |
| 267 | |
| 268 | py::class_<OpTest2>(m2, "OpTest2") |
| 269 | .def(py::init<>()) |
| 270 | .def(py::self + py::self) |
| 271 | .def("__add__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; }) |
| 272 | .def("__radd__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; }); |
Wenzel Jakob | b212f6c | 2016-09-10 16:00:50 +0900 | [diff] [blame] | 273 | |
| 274 | // Issue 388: Can't make iterators via make_iterator() with different r/v policies |
| 275 | static std::vector<int> list = { 1, 2, 3 }; |
| 276 | m2.def("make_iterator_1", []() { return py::make_iterator<py::return_value_policy::copy>(list); }); |
| 277 | m2.def("make_iterator_2", []() { return py::make_iterator<py::return_value_policy::automatic>(list); }); |
Jason Rhinelander | 6873c20 | 2016-10-24 21:58:22 -0400 | [diff] [blame] | 278 | |
| 279 | static std::vector<std::string> nothrows; |
| 280 | // Issue 461: registering two things with the same name: |
| 281 | py::class_<Dupe1>(m2, "Dupe1") |
| 282 | .def("get_value", &Dupe1::get_value) |
| 283 | ; |
| 284 | m2.def("dupe1_factory", [](int v) { return new Dupe1(v); }); |
| 285 | |
| 286 | py::class_<Dupe2>(m2, "Dupe2"); |
| 287 | py::exception<DupeException>(m2, "DupeException"); |
| 288 | |
| 289 | try { |
| 290 | m2.def("Dupe1", [](int v) { return new Dupe1(v); }); |
| 291 | nothrows.emplace_back("Dupe1"); |
| 292 | } |
| 293 | catch (std::runtime_error &) {} |
| 294 | try { |
| 295 | py::class_<Dupe3>(m2, "dupe1_factory"); |
| 296 | nothrows.emplace_back("dupe1_factory"); |
| 297 | } |
| 298 | catch (std::runtime_error &) {} |
| 299 | try { |
| 300 | py::exception<Dupe3>(m2, "Dupe2"); |
| 301 | nothrows.emplace_back("Dupe2"); |
| 302 | } |
| 303 | catch (std::runtime_error &) {} |
| 304 | try { |
| 305 | m2.def("DupeException", []() { return 30; }); |
| 306 | nothrows.emplace_back("DupeException1"); |
| 307 | } |
| 308 | catch (std::runtime_error &) {} |
| 309 | try { |
| 310 | py::class_<DupeException>(m2, "DupeException"); |
| 311 | nothrows.emplace_back("DupeException2"); |
| 312 | } |
| 313 | catch (std::runtime_error &) {} |
| 314 | m2.def("dupe_exception_failures", []() { |
| 315 | py::list l; |
| 316 | for (auto &e : nothrows) l.append(py::cast(e)); |
| 317 | return l; |
| 318 | }); |
Wenzel Jakob | bd560ac | 2016-11-03 11:53:35 +0100 | [diff] [blame] | 319 | |
| 320 | /// Issue #471: shared pointer instance not dellocated |
| 321 | class SharedChild : public std::enable_shared_from_this<SharedChild> { |
| 322 | public: |
| 323 | SharedChild() { print_created(this); } |
| 324 | ~SharedChild() { print_destroyed(this); } |
| 325 | }; |
| 326 | |
| 327 | class SharedParent { |
| 328 | public: |
| 329 | SharedParent() : child(std::make_shared<SharedChild>()) { } |
| 330 | const SharedChild &get_child() const { return *child; } |
| 331 | |
| 332 | private: |
| 333 | std::shared_ptr<SharedChild> child; |
| 334 | }; |
| 335 | |
| 336 | py::class_<SharedChild, std::shared_ptr<SharedChild>>(m, "SharedChild"); |
| 337 | py::class_<SharedParent, std::shared_ptr<SharedParent>>(m, "SharedParent") |
| 338 | .def(py::init<>()) |
| 339 | .def("get_child", &SharedParent::get_child, py::return_value_policy::reference); |
Jason Rhinelander | c07ec31 | 2016-11-06 13:12:48 -0500 | [diff] [blame] | 340 | |
| 341 | /// Issue/PR #478: unique ptrs constructed and freed without destruction |
| 342 | class SpecialHolderObj { |
| 343 | public: |
| 344 | int val = 0; |
| 345 | SpecialHolderObj *ch = nullptr; |
| 346 | SpecialHolderObj(int v, bool make_child = true) : val{v}, ch{make_child ? new SpecialHolderObj(val+1, false) : nullptr} |
| 347 | { print_created(this, val); } |
| 348 | ~SpecialHolderObj() { delete ch; print_destroyed(this); } |
| 349 | SpecialHolderObj *child() { return ch; } |
| 350 | }; |
| 351 | |
| 352 | py::class_<SpecialHolderObj, custom_unique_ptr<SpecialHolderObj>>(m, "SpecialHolderObj") |
| 353 | .def(py::init<int>()) |
| 354 | .def("child", &SpecialHolderObj::child, pybind11::return_value_policy::reference_internal) |
| 355 | .def_readwrite("val", &SpecialHolderObj::val) |
| 356 | .def_static("holder_cstats", &ConstructorStats::get<custom_unique_ptr<SpecialHolderObj>>, |
Wenzel Jakob | fe40dfe | 2016-11-07 15:59:01 +0100 | [diff] [blame] | 357 | py::return_value_policy::reference); |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 358 | |
Wenzel Jakob | fe40dfe | 2016-11-07 15:59:01 +0100 | [diff] [blame] | 359 | /// Issue #484: number conversion generates unhandled exceptions |
| 360 | m2.def("test_complex", [](float x) { py::print("{}"_s.format(x)); }); |
| 361 | m2.def("test_complex", [](std::complex<float> x) { py::print("({}, {})"_s.format(x.real(), x.imag())); }); |
Wenzel Jakob | 7c2461e | 2016-11-20 05:26:02 +0100 | [diff] [blame] | 362 | |
| 363 | /// Issue #511: problem with inheritance + overwritten def_static |
| 364 | struct MyBase { |
| 365 | static std::unique_ptr<MyBase> make() { |
| 366 | return std::unique_ptr<MyBase>(new MyBase()); |
| 367 | } |
| 368 | }; |
| 369 | |
| 370 | struct MyDerived : MyBase { |
| 371 | static std::unique_ptr<MyDerived> make() { |
| 372 | return std::unique_ptr<MyDerived>(new MyDerived()); |
| 373 | } |
| 374 | }; |
| 375 | |
| 376 | py::class_<MyBase>(m2, "MyBase") |
| 377 | .def_static("make", &MyBase::make); |
| 378 | |
| 379 | py::class_<MyDerived, MyBase>(m2, "MyDerived") |
| 380 | .def_static("make", &MyDerived::make) |
| 381 | .def_static("make2", &MyDerived::make); |
Jason Rhinelander | f200493 | 2016-11-25 07:06:18 -0500 | [diff] [blame] | 382 | |
Jason Rhinelander | 3f1ff3f | 2016-12-12 17:42:52 -0500 | [diff] [blame] | 383 | py::dict d; |
| 384 | std::string bar = "bar"; |
| 385 | d["str"] = bar; |
| 386 | d["num"] = 3.7; |
| 387 | |
Jason Rhinelander | f200493 | 2016-11-25 07:06:18 -0500 | [diff] [blame] | 388 | /// Issue #528: templated constructor |
| 389 | m2.def("tpl_constr_vector", [](std::vector<TplConstrClass> &) {}); |
| 390 | m2.def("tpl_constr_map", [](std::unordered_map<TplConstrClass, TplConstrClass> &) {}); |
| 391 | m2.def("tpl_constr_set", [](std::unordered_set<TplConstrClass> &) {}); |
| 392 | #if defined(PYBIND11_HAS_OPTIONAL) |
| 393 | m2.def("tpl_constr_optional", [](std::optional<TplConstrClass> &) {}); |
| 394 | #elif defined(PYBIND11_HAS_EXP_OPTIONAL) |
| 395 | m2.def("tpl_constr_optional", [](std::experimental::optional<TplConstrClass> &) {}); |
| 396 | #endif |
Wenzel Jakob | fe40dfe | 2016-11-07 15:59:01 +0100 | [diff] [blame] | 397 | } |
Jason Rhinelander | 6873c20 | 2016-10-24 21:58:22 -0400 | [diff] [blame] | 398 | |
Jason Rhinelander | 1eaacd1 | 2017-02-08 02:45:51 -0500 | [diff] [blame] | 399 | // MSVC workaround: trying to use a lambda here crashes MSVC |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 400 | test_initializer issues(&init_issues); |