You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

  1. /*
  2. example/issues.cpp -- collection of testcases for miscellaneous issues
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "example.h"
  8. #include <pybind11/stl.h>
  9. PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
  10. void init_issues(py::module &m) {
  11. py::module m2 = m.def_submodule("issues");
  12. #if !defined(_MSC_VER)
  13. // Visual Studio 2015 currently cannot compile this test
  14. // (see the comment in type_caster_base::make_copy_constructor)
  15. // #70 compilation issue if operator new is not public
  16. class NonConstructible { private: void *operator new(size_t bytes) throw(); };
  17. py::class_<NonConstructible>(m, "Foo");
  18. m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
  19. py::return_value_policy::reference);
  20. #endif
  21. // #137: const char* isn't handled properly
  22. m2.def("print_cchar", [](const char *string) { std::cout << string << std::endl; });
  23. // #150: char bindings broken
  24. m2.def("print_char", [](char c) { std::cout << c << std::endl; });
  25. // #159: virtual function dispatch has problems with similar-named functions
  26. struct Base { virtual void dispatch(void) const {
  27. /* for some reason MSVC2015 can't compile this if the function is pure virtual */
  28. }; };
  29. struct DispatchIssue : Base {
  30. virtual void dispatch(void) const {
  31. PYBIND11_OVERLOAD_PURE(void, Base, dispatch, /* no arguments */);
  32. }
  33. };
  34. py::class_<DispatchIssue> base(m2, "DispatchIssue");
  35. base.alias<Base>()
  36. .def(py::init<>())
  37. .def("dispatch", &Base::dispatch);
  38. m2.def("dispatch_issue_go", [](const Base * b) { b->dispatch(); });
  39. struct Placeholder { int i; Placeholder(int i) : i(i) { } };
  40. py::class_<Placeholder>(m2, "Placeholder")
  41. .def(py::init<int>())
  42. .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
  43. // #171: Can't return reference wrappers (or STL datastructures containing them)
  44. m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
  45. Placeholder *p1 = new Placeholder{1};
  46. Placeholder *p2 = new Placeholder{2};
  47. Placeholder *p3 = new Placeholder{3};
  48. std::vector<std::reference_wrapper<Placeholder>> v;
  49. v.push_back(std::ref(*p1));
  50. v.push_back(std::ref(*p2));
  51. v.push_back(std::ref(*p3));
  52. v.push_back(p4);
  53. return v;
  54. });
  55. // #181: iterator passthrough did not compile
  56. m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
  57. return py::make_iterator(std::begin(s), std::end(s));
  58. });
  59. // #187: issue involving std::shared_ptr<> return value policy & garbage collection
  60. struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
  61. struct ElementA : ElementBase {
  62. ElementA(int v) : v(v) { }
  63. int value() { return v; }
  64. int v;
  65. };
  66. struct ElementList {
  67. void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
  68. std::vector<std::shared_ptr<ElementBase>> l;
  69. };
  70. py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
  71. py::class_<ElementA, std::shared_ptr<ElementA>>(m2, "ElementA", py::base<ElementBase>())
  72. .def(py::init<int>())
  73. .def("value", &ElementA::value);
  74. py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
  75. .def(py::init<>())
  76. .def("add", &ElementList::add)
  77. .def("get", [](ElementList &el){
  78. py::list list;
  79. for (auto &e : el.l)
  80. list.append(py::cast(e));
  81. return list;
  82. });
  83. // (no id): should not be able to pass 'None' to a reference argument
  84. m2.def("print_element", [](ElementA &el) { std::cout << el.value() << std::endl; });
  85. // (no id): don't cast doubles to ints
  86. m2.def("expect_float", [](float f) { return f; });
  87. m2.def("expect_int", [](int i) { return i; });
  88. }