blob: 032b6ddf24d4cde615e5de732ea2664b53e8cf57 [file] [log] [blame]
Wenzel Jakob17cdb062016-03-10 13:24:10 +01001/*
2 example/issues.cpp -- collection of testcases for miscellaneous issues
3
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
10#include "example.h"
Wenzel Jakobf54ded72016-04-20 17:00:57 +020011#include <pybind11/stl.h>
Jason Rhinelander1b05ce52016-08-09 17:57:59 -040012#include <pybind11/operators.h>
Wenzel Jakob17cdb062016-03-10 13:24:10 +010013
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020014PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
15
Wenzel Jakob17cdb062016-03-10 13:24:10 +010016void init_issues(py::module &m) {
17 py::module m2 = m.def_submodule("issues");
18
Wenzel Jakob9059bd82016-05-01 10:39:45 +020019#if !defined(_MSC_VER)
20 // Visual Studio 2015 currently cannot compile this test
21 // (see the comment in type_caster_base::make_copy_constructor)
22 // #70 compilation issue if operator new is not public
23 class NonConstructible { private: void *operator new(size_t bytes) throw(); };
24 py::class_<NonConstructible>(m, "Foo");
Wenzel Jakobbd57eb42016-05-01 14:42:20 +020025 m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
Wenzel Jakob9059bd82016-05-01 10:39:45 +020026 py::return_value_policy::reference);
27#endif
28
Wenzel Jakob17cdb062016-03-10 13:24:10 +010029 // #137: const char* isn't handled properly
30 m2.def("print_cchar", [](const char *string) { std::cout << string << std::endl; });
Wenzel Jakobd3349af2016-03-26 23:04:10 +010031
32 // #150: char bindings broken
33 m2.def("print_char", [](char c) { std::cout << c << std::endl; });
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020034
35 // #159: virtual function dispatch has problems with similar-named functions
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020036 struct Base { virtual void dispatch(void) const {
37 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
38 }; };
Wenzel Jakobe7074972016-04-30 22:44:00 +020039
40 struct DispatchIssue : Base {
41 virtual void dispatch(void) const {
42 PYBIND11_OVERLOAD_PURE(void, Base, dispatch, /* no arguments */);
43 }
44 };
45
Wenzel Jakob86d825f2016-05-26 13:19:27 +020046 py::class_<Base, std::unique_ptr<Base>, DispatchIssue>(m2, "DispatchIssue")
Wenzel Jakobf54ded72016-04-20 17:00:57 +020047 .def(py::init<>())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020048 .def("dispatch", &Base::dispatch);
49
Wenzel Jakobe7074972016-04-30 22:44:00 +020050 m2.def("dispatch_issue_go", [](const Base * b) { b->dispatch(); });
51
52 struct Placeholder { int i; Placeholder(int i) : i(i) { } };
Wenzel Jakobf54ded72016-04-20 17:00:57 +020053
54 py::class_<Placeholder>(m2, "Placeholder")
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020055 .def(py::init<int>())
Wenzel Jakobf54ded72016-04-20 17:00:57 +020056 .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
57
58 // #171: Can't return reference wrappers (or STL datastructures containing them)
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020059 m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
Wenzel Jakobf54ded72016-04-20 17:00:57 +020060 Placeholder *p1 = new Placeholder{1};
61 Placeholder *p2 = new Placeholder{2};
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020062 Placeholder *p3 = new Placeholder{3};
Wenzel Jakobf54ded72016-04-20 17:00:57 +020063 std::vector<std::reference_wrapper<Placeholder>> v;
64 v.push_back(std::ref(*p1));
65 v.push_back(std::ref(*p2));
66 v.push_back(std::ref(*p3));
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020067 v.push_back(p4);
Wenzel Jakobf54ded72016-04-20 17:00:57 +020068 return v;
69 });
Wenzel Jakob6ca6e822016-04-27 14:33:52 +020070
71 // #181: iterator passthrough did not compile
72 m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
73 return py::make_iterator(std::begin(s), std::end(s));
74 });
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020075
76 // #187: issue involving std::shared_ptr<> return value policy & garbage collection
77 struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
78 struct ElementA : ElementBase {
79 ElementA(int v) : v(v) { }
80 int value() { return v; }
81 int v;
82 };
83
84 struct ElementList {
85 void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
86 std::vector<std::shared_ptr<ElementBase>> l;
87 };
88
89 py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
90
91 py::class_<ElementA, std::shared_ptr<ElementA>>(m2, "ElementA", py::base<ElementBase>())
92 .def(py::init<int>())
93 .def("value", &ElementA::value);
94
95 py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
96 .def(py::init<>())
97 .def("add", &ElementList::add)
98 .def("get", [](ElementList &el){
99 py::list list;
100 for (auto &e : el.l)
101 list.append(py::cast(e));
102 return list;
103 });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200104
105 // (no id): should not be able to pass 'None' to a reference argument
106 m2.def("print_element", [](ElementA &el) { std::cout << el.value() << std::endl; });
Wenzel Jakob3f200fa2016-05-17 15:35:29 +0200107
108 // (no id): don't cast doubles to ints
109 m2.def("expect_float", [](float f) { return f; });
110 m2.def("expect_int", [](int i) { return i; });
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200111
112 // (no id): don't invoke Python dispatch code when instantiating C++
113 // classes that were not extended on the Python side
114 struct A {
115 virtual ~A() {}
116 virtual void f() { std::cout << "A.f()" << std::endl; }
117 };
118
119 struct PyA : A {
120 PyA() { std::cout << "PyA.PyA()" << std::endl; }
121
122 void f() override {
123 std::cout << "PyA.f()" << std::endl;
124 PYBIND11_OVERLOAD(void, A, f);
125 }
126 };
127
128 auto call_f = [](A *a) { a->f(); };
129
Wenzel Jakobfb0e2e52016-07-01 14:54:24 +0200130 pybind11::class_<A, std::unique_ptr<A>, PyA>(m2, "A")
131 .def(py::init<>())
132 .def("f", &A::f);
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200133
Wenzel Jakobfb0e2e52016-07-01 14:54:24 +0200134 m2.def("call_f", call_f);
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200135
136 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<>())
155 .def("__str__", [](const StrIssue &si) {
156 std::cout << "StrIssue.__str__ called" << std::endl;
157 return "StrIssue[" + std::to_string(si.value()) + "]";
158 })
159 ;
160
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400161 // Issue #328: first member in a class can't be used in operators
162#define TRACKERS(CLASS) CLASS() { std::cout << #CLASS "@" << this << " constructor\n"; } \
163 ~CLASS() { std::cout << #CLASS "@" << this << " destructor\n"; }
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -0400164 struct NestABase { int value = -2; TRACKERS(NestABase) };
165 struct NestA : NestABase { int value = 3; NestA& operator+=(int i) { value += i; return *this; } TRACKERS(NestA) };
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400166 struct NestB { NestA a; int value = 4; NestB& operator-=(int i) { value -= i; return *this; } TRACKERS(NestB) };
167 struct NestC { NestB b; int value = 5; NestC& operator*=(int i) { value *= i; return *this; } TRACKERS(NestC) };
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -0400168 py::class_<NestABase>(m2, "NestABase").def(py::init<>()).def_readwrite("value", &NestABase::value);
169 py::class_<NestA>(m2, "NestA").def(py::init<>()).def(py::self += int())
170 .def("as_base", [](NestA &a) -> NestABase& { return (NestABase&) a; }, py::return_value_policy::reference_internal);
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400171 py::class_<NestB>(m2, "NestB").def(py::init<>()).def(py::self -= int()).def_readwrite("a", &NestB::a);
172 py::class_<NestC>(m2, "NestC").def(py::init<>()).def(py::self *= int()).def_readwrite("b", &NestC::b);
173 m2.def("print_NestA", [](const NestA &a) { std::cout << a.value << std::endl; });
174 m2.def("print_NestB", [](const NestB &b) { std::cout << b.value << std::endl; });
175 m2.def("print_NestC", [](const NestC &c) { std::cout << c.value << std::endl; });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200176}