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.

297 lines
14 KiB

8 years ago
  1. /*
  2. tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access,
  3. __str__, argument and return value conventions
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include "constructor_stats.h"
  10. class ExampleMandA {
  11. public:
  12. ExampleMandA() { print_default_created(this); }
  13. ExampleMandA(int value) : value(value) { print_created(this, value); }
  14. ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
  15. ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
  16. ~ExampleMandA() { print_destroyed(this); }
  17. std::string toString() {
  18. return "ExampleMandA[value=" + std::to_string(value) + "]";
  19. }
  20. void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
  21. void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
  22. void add1(ExampleMandA other) { value += other.value; } // passing by value
  23. void add2(ExampleMandA &other) { value += other.value; } // passing by reference
  24. void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
  25. void add4(ExampleMandA *other) { value += other->value; } // passing by pointer
  26. void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
  27. void add6(int other) { value += other; } // passing by value
  28. void add7(int &other) { value += other; } // passing by reference
  29. void add8(const int &other) { value += other; } // passing by const reference
  30. void add9(int *other) { value += *other; } // passing by pointer
  31. void add10(const int *other) { value += *other; } // passing by const pointer
  32. ExampleMandA self1() { return *this; } // return by value
  33. ExampleMandA &self2() { return *this; } // return by reference
  34. const ExampleMandA &self3() { return *this; } // return by const reference
  35. ExampleMandA *self4() { return this; } // return by pointer
  36. const ExampleMandA *self5() { return this; } // return by const pointer
  37. int internal1() { return value; } // return by value
  38. int &internal2() { return value; } // return by reference
  39. const int &internal3() { return value; } // return by const reference
  40. int *internal4() { return &value; } // return by pointer
  41. const int *internal5() { return &value; } // return by const pointer
  42. py::str overloaded(int, float) { return "(int, float)"; }
  43. py::str overloaded(float, int) { return "(float, int)"; }
  44. py::str overloaded(int, int) { return "(int, int)"; }
  45. py::str overloaded(float, float) { return "(float, float)"; }
  46. py::str overloaded(int, float) const { return "(int, float) const"; }
  47. py::str overloaded(float, int) const { return "(float, int) const"; }
  48. py::str overloaded(int, int) const { return "(int, int) const"; }
  49. py::str overloaded(float, float) const { return "(float, float) const"; }
  50. int value = 0;
  51. };
  52. struct TestProperties {
  53. int value = 1;
  54. static int static_value;
  55. int get() const { return value; }
  56. void set(int v) { value = v; }
  57. static int static_get() { return static_value; }
  58. static void static_set(int v) { static_value = v; }
  59. };
  60. int TestProperties::static_value = 1;
  61. struct SimpleValue { int value = 1; };
  62. struct TestPropRVP {
  63. SimpleValue v1;
  64. SimpleValue v2;
  65. static SimpleValue sv1;
  66. static SimpleValue sv2;
  67. const SimpleValue &get1() const { return v1; }
  68. const SimpleValue &get2() const { return v2; }
  69. SimpleValue get_rvalue() const { return v2; }
  70. void set1(int v) { v1.value = v; }
  71. void set2(int v) { v2.value = v; }
  72. };
  73. SimpleValue TestPropRVP::sv1{};
  74. SimpleValue TestPropRVP::sv2{};
  75. class DynamicClass {
  76. public:
  77. DynamicClass() { print_default_created(this); }
  78. ~DynamicClass() { print_destroyed(this); }
  79. };
  80. class CppDerivedDynamicClass : public DynamicClass { };
  81. // py::arg/py::arg_v testing: these arguments just record their argument when invoked
  82. class ArgInspector1 { public: std::string arg = "(default arg inspector 1)"; };
  83. class ArgInspector2 { public: std::string arg = "(default arg inspector 2)"; };
  84. class ArgAlwaysConverts { };
  85. namespace pybind11 { namespace detail {
  86. template <> struct type_caster<ArgInspector1> {
  87. public:
  88. PYBIND11_TYPE_CASTER(ArgInspector1, _("ArgInspector1"));
  89. bool load(handle src, bool convert) {
  90. value.arg = "loading ArgInspector1 argument " +
  91. std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed. "
  92. "Argument value = " + (std::string) str(src);
  93. return true;
  94. }
  95. static handle cast(const ArgInspector1 &src, return_value_policy, handle) {
  96. return str(src.arg).release();
  97. }
  98. };
  99. template <> struct type_caster<ArgInspector2> {
  100. public:
  101. PYBIND11_TYPE_CASTER(ArgInspector2, _("ArgInspector2"));
  102. bool load(handle src, bool convert) {
  103. value.arg = "loading ArgInspector2 argument " +
  104. std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed. "
  105. "Argument value = " + (std::string) str(src);
  106. return true;
  107. }
  108. static handle cast(const ArgInspector2 &src, return_value_policy, handle) {
  109. return str(src.arg).release();
  110. }
  111. };
  112. template <> struct type_caster<ArgAlwaysConverts> {
  113. public:
  114. PYBIND11_TYPE_CASTER(ArgAlwaysConverts, _("ArgAlwaysConverts"));
  115. bool load(handle, bool convert) {
  116. return convert;
  117. }
  118. static handle cast(const ArgAlwaysConverts &, return_value_policy, handle) {
  119. return py::none();
  120. }
  121. };
  122. }}
  123. /// Issue/PR #648: bad arg default debugging output
  124. class NotRegistered {};
  125. test_initializer methods_and_attributes([](py::module &m) {
  126. py::class_<ExampleMandA>(m, "ExampleMandA")
  127. .def(py::init<>())
  128. .def(py::init<int>())
  129. .def(py::init<const ExampleMandA&>())
  130. .def("add1", &ExampleMandA::add1)
  131. .def("add2", &ExampleMandA::add2)
  132. .def("add3", &ExampleMandA::add3)
  133. .def("add4", &ExampleMandA::add4)
  134. .def("add5", &ExampleMandA::add5)
  135. .def("add6", &ExampleMandA::add6)
  136. .def("add7", &ExampleMandA::add7)
  137. .def("add8", &ExampleMandA::add8)
  138. .def("add9", &ExampleMandA::add9)
  139. .def("add10", &ExampleMandA::add10)
  140. .def("self1", &ExampleMandA::self1)
  141. .def("self2", &ExampleMandA::self2)
  142. .def("self3", &ExampleMandA::self3)
  143. .def("self4", &ExampleMandA::self4)
  144. .def("self5", &ExampleMandA::self5)
  145. .def("internal1", &ExampleMandA::internal1)
  146. .def("internal2", &ExampleMandA::internal2)
  147. .def("internal3", &ExampleMandA::internal3)
  148. .def("internal4", &ExampleMandA::internal4)
  149. .def("internal5", &ExampleMandA::internal5)
  150. #if defined(PYBIND11_OVERLOAD_CAST)
  151. .def("overloaded", py::overload_cast<int, float>(&ExampleMandA::overloaded))
  152. .def("overloaded", py::overload_cast<float, int>(&ExampleMandA::overloaded))
  153. .def("overloaded", py::overload_cast<int, int>(&ExampleMandA::overloaded))
  154. .def("overloaded", py::overload_cast<float, float>(&ExampleMandA::overloaded))
  155. .def("overloaded_float", py::overload_cast<float, float>(&ExampleMandA::overloaded))
  156. .def("overloaded_const", py::overload_cast<int, float>(&ExampleMandA::overloaded, py::const_))
  157. .def("overloaded_const", py::overload_cast<float, int>(&ExampleMandA::overloaded, py::const_))
  158. .def("overloaded_const", py::overload_cast<int, int>(&ExampleMandA::overloaded, py::const_))
  159. .def("overloaded_const", py::overload_cast<float, float>(&ExampleMandA::overloaded, py::const_))
  160. #else
  161. .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, float)>(&ExampleMandA::overloaded))
  162. .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, int)>(&ExampleMandA::overloaded))
  163. .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded))
  164. .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
  165. .def("overloaded_float", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
  166. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, float) const>(&ExampleMandA::overloaded))
  167. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, int) const>(&ExampleMandA::overloaded))
  168. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, int) const>(&ExampleMandA::overloaded))
  169. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, float) const>(&ExampleMandA::overloaded))
  170. #endif
  171. .def("__str__", &ExampleMandA::toString)
  172. .def_readwrite("value", &ExampleMandA::value);
  173. py::class_<TestProperties>(m, "TestProperties")
  174. .def(py::init<>())
  175. .def_readonly("def_readonly", &TestProperties::value)
  176. .def_readwrite("def_readwrite", &TestProperties::value)
  177. .def_property_readonly("def_property_readonly", &TestProperties::get)
  178. .def_property("def_property", &TestProperties::get, &TestProperties::set)
  179. .def_readonly_static("def_readonly_static", &TestProperties::static_value)
  180. .def_readwrite_static("def_readwrite_static", &TestProperties::static_value)
  181. .def_property_readonly_static("def_property_readonly_static",
  182. [](py::object) { return TestProperties::static_get(); })
  183. .def_property_static("def_property_static",
  184. [](py::object) { return TestProperties::static_get(); },
  185. [](py::object, int v) { TestProperties::static_set(v); })
  186. .def_property_static("static_cls",
  187. [](py::object cls) { return cls; },
  188. [](py::object cls, py::function f) { f(cls); });
  189. py::class_<SimpleValue>(m, "SimpleValue")
  190. .def_readwrite("value", &SimpleValue::value);
  191. auto static_get1 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv1; };
  192. auto static_get2 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv2; };
  193. auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.value = v; };
  194. auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.value = v; };
  195. auto rvp_copy = py::return_value_policy::copy;
  196. py::class_<TestPropRVP>(m, "TestPropRVP")
  197. .def(py::init<>())
  198. .def_property_readonly("ro_ref", &TestPropRVP::get1)
  199. .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy)
  200. .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy))
  201. .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1)
  202. .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy)
  203. .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
  204. .def_property_readonly_static("static_ro_ref", static_get1)
  205. .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy)
  206. .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy))
  207. .def_property_static("static_rw_ref", static_get1, static_set1)
  208. .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
  209. .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
  210. .def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
  211. .def_property_readonly_static("static_rvalue", [](py::object) { return SimpleValue(); });
  212. struct MetaclassOverride { };
  213. py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
  214. .def_property_readonly_static("readonly", [](py::object) { return 1; });
  215. #if !defined(PYPY_VERSION)
  216. py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
  217. .def(py::init());
  218. py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
  219. .def(py::init());
  220. #endif
  221. // Test converting. The ArgAlwaysConverts is just there to make the first no-conversion pass
  222. // fail so that our call always ends up happening via the second dispatch (the one that allows
  223. // some conversion).
  224. class ArgInspector {
  225. public:
  226. ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; }
  227. std::string g(ArgInspector1 a, const ArgInspector1 &b, int c, ArgInspector2 *d, ArgAlwaysConverts) {
  228. return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg;
  229. }
  230. static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; }
  231. };
  232. py::class_<ArgInspector>(m, "ArgInspector")
  233. .def(py::init<>())
  234. .def("f", &ArgInspector::f, py::arg(), py::arg() = ArgAlwaysConverts())
  235. .def("g", &ArgInspector::g, "a"_a.noconvert(), "b"_a, "c"_a.noconvert()=13, "d"_a=ArgInspector2(), py::arg() = ArgAlwaysConverts())
  236. .def_static("h", &ArgInspector::h, py::arg().noconvert(), py::arg() = ArgAlwaysConverts())
  237. ;
  238. m.def("arg_inspect_func", [](ArgInspector2 a, ArgInspector1 b, ArgAlwaysConverts) { return a.arg + "\n" + b.arg; },
  239. py::arg().noconvert(false), py::arg_v(nullptr, ArgInspector1()).noconvert(true), py::arg() = ArgAlwaysConverts());
  240. m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
  241. m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
  242. /// Issue/PR #648: bad arg default debugging output
  243. #if !defined(NDEBUG)
  244. m.attr("debug_enabled") = true;
  245. #else
  246. m.attr("debug_enabled") = false;
  247. #endif
  248. m.def("bad_arg_def_named", []{
  249. auto m = py::module::import("pybind11_tests.issues");
  250. m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg("a") = NotRegistered());
  251. });
  252. m.def("bad_arg_def_unnamed", []{
  253. auto m = py::module::import("pybind11_tests.issues");
  254. m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg() = NotRegistered());
  255. });
  256. });