blob: d1a1941b7d3d492aecb0b07f3a007b7ba9cb243f [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 Jakobf54ded72016-04-20 17:00:57 +020045 py::class_<DispatchIssue> base(m2, "DispatchIssue");
Wenzel Jakobf5c154a2016-04-11 18:13:08 +020046 base.alias<Base>()
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 Jakobbd57eb42016-05-01 14:42:20 +0200111}