blob: e1647081f7ad696de4950904bd21b33276367ce4 [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>
Wenzel Jakob17cdb062016-03-10 13:24:10 +010012
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020013PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
14
Wenzel Jakob17cdb062016-03-10 13:24:10 +010015void init_issues(py::module &m) {
16 py::module m2 = m.def_submodule("issues");
17
Wenzel Jakob9059bd82016-05-01 10:39:45 +020018#if !defined(_MSC_VER)
19 // Visual Studio 2015 currently cannot compile this test
20 // (see the comment in type_caster_base::make_copy_constructor)
21 // #70 compilation issue if operator new is not public
22 class NonConstructible { private: void *operator new(size_t bytes) throw(); };
23 py::class_<NonConstructible>(m, "Foo");
Wenzel Jakobbd57eb42016-05-01 14:42:20 +020024 m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
Wenzel Jakob9059bd82016-05-01 10:39:45 +020025 py::return_value_policy::reference);
26#endif
27
Wenzel Jakob17cdb062016-03-10 13:24:10 +010028 // #137: const char* isn't handled properly
29 m2.def("print_cchar", [](const char *string) { std::cout << string << std::endl; });
Wenzel Jakobd3349af2016-03-26 23:04:10 +010030
31 // #150: char bindings broken
32 m2.def("print_char", [](char c) { std::cout << c << std::endl; });
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020033
34 // #159: virtual function dispatch has problems with similar-named functions
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020035 struct Base { virtual void dispatch(void) const {
36 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
37 }; };
Wenzel Jakobe7074972016-04-30 22:44:00 +020038
39 struct DispatchIssue : Base {
40 virtual void dispatch(void) const {
41 PYBIND11_OVERLOAD_PURE(void, Base, dispatch, /* no arguments */);
42 }
43 };
44
Wenzel Jakob86d825f2016-05-26 13:19:27 +020045 py::class_<Base, std::unique_ptr<Base>, DispatchIssue>(m2, "DispatchIssue")
Wenzel Jakobf54ded72016-04-20 17:00:57 +020046 .def(py::init<>())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020047 .def("dispatch", &Base::dispatch);
48
Wenzel Jakobe7074972016-04-30 22:44:00 +020049 m2.def("dispatch_issue_go", [](const Base * b) { b->dispatch(); });
50
51 struct Placeholder { int i; Placeholder(int i) : i(i) { } };
Wenzel Jakobf54ded72016-04-20 17:00:57 +020052
53 py::class_<Placeholder>(m2, "Placeholder")
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020054 .def(py::init<int>())
Wenzel Jakobf54ded72016-04-20 17:00:57 +020055 .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
56
57 // #171: Can't return reference wrappers (or STL datastructures containing them)
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020058 m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
Wenzel Jakobf54ded72016-04-20 17:00:57 +020059 Placeholder *p1 = new Placeholder{1};
60 Placeholder *p2 = new Placeholder{2};
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020061 Placeholder *p3 = new Placeholder{3};
Wenzel Jakobf54ded72016-04-20 17:00:57 +020062 std::vector<std::reference_wrapper<Placeholder>> v;
63 v.push_back(std::ref(*p1));
64 v.push_back(std::ref(*p2));
65 v.push_back(std::ref(*p3));
Wenzel Jakobdbe43ff2016-04-21 12:21:14 +020066 v.push_back(p4);
Wenzel Jakobf54ded72016-04-20 17:00:57 +020067 return v;
68 });
Wenzel Jakob6ca6e822016-04-27 14:33:52 +020069
70 // #181: iterator passthrough did not compile
71 m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
72 return py::make_iterator(std::begin(s), std::end(s));
73 });
Wenzel Jakobd2b628b2016-04-30 23:02:39 +020074
75 // #187: issue involving std::shared_ptr<> return value policy & garbage collection
76 struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
77 struct ElementA : ElementBase {
78 ElementA(int v) : v(v) { }
79 int value() { return v; }
80 int v;
81 };
82
83 struct ElementList {
84 void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
85 std::vector<std::shared_ptr<ElementBase>> l;
86 };
87
88 py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
89
90 py::class_<ElementA, std::shared_ptr<ElementA>>(m2, "ElementA", py::base<ElementBase>())
91 .def(py::init<int>())
92 .def("value", &ElementA::value);
93
94 py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
95 .def(py::init<>())
96 .def("add", &ElementList::add)
97 .def("get", [](ElementList &el){
98 py::list list;
99 for (auto &e : el.l)
100 list.append(py::cast(e));
101 return list;
102 });
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200103
104 // (no id): should not be able to pass 'None' to a reference argument
105 m2.def("print_element", [](ElementA &el) { std::cout << el.value() << std::endl; });
Wenzel Jakob3f200fa2016-05-17 15:35:29 +0200106
107 // (no id): don't cast doubles to ints
108 m2.def("expect_float", [](float f) { return f; });
109 m2.def("expect_int", [](int i) { return i; });
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200110
111 // (no id): don't invoke Python dispatch code when instantiating C++
112 // classes that were not extended on the Python side
113 struct A {
114 virtual ~A() {}
115 virtual void f() { std::cout << "A.f()" << std::endl; }
116 };
117
118 struct PyA : A {
119 PyA() { std::cout << "PyA.PyA()" << std::endl; }
120
121 void f() override {
122 std::cout << "PyA.f()" << std::endl;
123 PYBIND11_OVERLOAD(void, A, f);
124 }
125 };
126
127 auto call_f = [](A *a) { a->f(); };
128
129 pybind11::class_<A, std::unique_ptr<A>, PyA>(m2, "A")
130 .def(py::init<>())
131 .def("f", &A::f);
132
133 m2.def("call_f", call_f);
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200134}