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.

141 lines
4.8 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_<Base, std::unique_ptr<Base>, DispatchIssue>(m2, "DispatchIssue")
  35. .def(py::init<>())
  36. .def("dispatch", &Base::dispatch);
  37. m2.def("dispatch_issue_go", [](const Base * b) { b->dispatch(); });
  38. struct Placeholder { int i; Placeholder(int i) : i(i) { } };
  39. py::class_<Placeholder>(m2, "Placeholder")
  40. .def(py::init<int>())
  41. .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
  42. // #171: Can't return reference wrappers (or STL datastructures containing them)
  43. m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
  44. Placeholder *p1 = new Placeholder{1};
  45. Placeholder *p2 = new Placeholder{2};
  46. Placeholder *p3 = new Placeholder{3};
  47. std::vector<std::reference_wrapper<Placeholder>> v;
  48. v.push_back(std::ref(*p1));
  49. v.push_back(std::ref(*p2));
  50. v.push_back(std::ref(*p3));
  51. v.push_back(p4);
  52. return v;
  53. });
  54. // #181: iterator passthrough did not compile
  55. m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
  56. return py::make_iterator(std::begin(s), std::end(s));
  57. });
  58. // #187: issue involving std::shared_ptr<> return value policy & garbage collection
  59. struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
  60. struct ElementA : ElementBase {
  61. ElementA(int v) : v(v) { }
  62. int value() { return v; }
  63. int v;
  64. };
  65. struct ElementList {
  66. void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
  67. std::vector<std::shared_ptr<ElementBase>> l;
  68. };
  69. py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
  70. py::class_<ElementA, std::shared_ptr<ElementA>>(m2, "ElementA", py::base<ElementBase>())
  71. .def(py::init<int>())
  72. .def("value", &ElementA::value);
  73. py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
  74. .def(py::init<>())
  75. .def("add", &ElementList::add)
  76. .def("get", [](ElementList &el){
  77. py::list list;
  78. for (auto &e : el.l)
  79. list.append(py::cast(e));
  80. return list;
  81. });
  82. // (no id): should not be able to pass 'None' to a reference argument
  83. m2.def("print_element", [](ElementA &el) { std::cout << el.value() << std::endl; });
  84. // (no id): don't cast doubles to ints
  85. m2.def("expect_float", [](float f) { return f; });
  86. m2.def("expect_int", [](int i) { return i; });
  87. // (no id): don't invoke Python dispatch code when instantiating C++
  88. // classes that were not extended on the Python side
  89. struct A {
  90. virtual ~A() {}
  91. virtual void f() { std::cout << "A.f()" << std::endl; }
  92. };
  93. struct PyA : A {
  94. PyA() { std::cout << "PyA.PyA()" << std::endl; }
  95. void f() override {
  96. std::cout << "PyA.f()" << std::endl;
  97. PYBIND11_OVERLOAD(void, A, f);
  98. }
  99. };
  100. auto call_f = [](A *a) { a->f(); };
  101. pybind11::class_<A, std::unique_ptr<A>, PyA>(m2, "A")
  102. .def(py::init<>())
  103. .def("f", &A::f);
  104. m2.def("call_f", call_f);
  105. try {
  106. py::class_<Placeholder>(m2, "Placeholder");
  107. throw std::logic_error("Expected an exception!");
  108. } catch (std::runtime_error &) {
  109. /* All good */
  110. }
  111. }