test_methods_and_attributes.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <[email protected]>
  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() { return "()"; }
  43. py::str overloaded(int) { return "(int)"; }
  44. py::str overloaded(int, float) { return "(int, float)"; }
  45. py::str overloaded(float, int) { return "(float, int)"; }
  46. py::str overloaded(int, int) { return "(int, int)"; }
  47. py::str overloaded(float, float) { return "(float, float)"; }
  48. py::str overloaded(int) const { return "(int) const"; }
  49. py::str overloaded(int, float) const { return "(int, float) const"; }
  50. py::str overloaded(float, int) const { return "(float, int) const"; }
  51. py::str overloaded(int, int) const { return "(int, int) const"; }
  52. py::str overloaded(float, float) const { return "(float, float) const"; }
  53. static py::str overloaded(float) { return "static float"; }
  54. int value = 0;
  55. };
  56. struct TestProperties {
  57. int value = 1;
  58. static int static_value;
  59. int get() const { return value; }
  60. void set(int v) { value = v; }
  61. static int static_get() { return static_value; }
  62. static void static_set(int v) { static_value = v; }
  63. };
  64. int TestProperties::static_value = 1;
  65. struct TestPropertiesOverride : TestProperties {
  66. int value = 99;
  67. static int static_value;
  68. };
  69. int TestPropertiesOverride::static_value = 99;
  70. struct TestPropRVP {
  71. UserType v1{1};
  72. UserType v2{1};
  73. static UserType sv1;
  74. static UserType sv2;
  75. const UserType &get1() const { return v1; }
  76. const UserType &get2() const { return v2; }
  77. UserType get_rvalue() const { return v2; }
  78. void set1(int v) { v1.set(v); }
  79. void set2(int v) { v2.set(v); }
  80. };
  81. UserType TestPropRVP::sv1(1);
  82. UserType TestPropRVP::sv2(1);
  83. // py::arg/py::arg_v testing: these arguments just record their argument when invoked
  84. class ArgInspector1 { public: std::string arg = "(default arg inspector 1)"; };
  85. class ArgInspector2 { public: std::string arg = "(default arg inspector 2)"; };
  86. class ArgAlwaysConverts { };
  87. namespace pybind11 { namespace detail {
  88. template <> struct type_caster<ArgInspector1> {
  89. public:
  90. PYBIND11_TYPE_CASTER(ArgInspector1, _("ArgInspector1"));
  91. bool load(handle src, bool convert) {
  92. value.arg = "loading ArgInspector1 argument " +
  93. std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed. "
  94. "Argument value = " + (std::string) str(src);
  95. return true;
  96. }
  97. static handle cast(const ArgInspector1 &src, return_value_policy, handle) {
  98. return str(src.arg).release();
  99. }
  100. };
  101. template <> struct type_caster<ArgInspector2> {
  102. public:
  103. PYBIND11_TYPE_CASTER(ArgInspector2, _("ArgInspector2"));
  104. bool load(handle src, bool convert) {
  105. value.arg = "loading ArgInspector2 argument " +
  106. std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed. "
  107. "Argument value = " + (std::string) str(src);
  108. return true;
  109. }
  110. static handle cast(const ArgInspector2 &src, return_value_policy, handle) {
  111. return str(src.arg).release();
  112. }
  113. };
  114. template <> struct type_caster<ArgAlwaysConverts> {
  115. public:
  116. PYBIND11_TYPE_CASTER(ArgAlwaysConverts, _("ArgAlwaysConverts"));
  117. bool load(handle, bool convert) {
  118. return convert;
  119. }
  120. static handle cast(const ArgAlwaysConverts &, return_value_policy, handle) {
  121. return py::none().release();
  122. }
  123. };
  124. }}
  125. // test_custom_caster_destruction
  126. class DestructionTester {
  127. public:
  128. DestructionTester() { print_default_created(this); }
  129. ~DestructionTester() { print_destroyed(this); }
  130. DestructionTester(const DestructionTester &) { print_copy_created(this); }
  131. DestructionTester(DestructionTester &&) { print_move_created(this); }
  132. DestructionTester &operator=(const DestructionTester &) { print_copy_assigned(this); return *this; }
  133. DestructionTester &operator=(DestructionTester &&) { print_move_assigned(this); return *this; }
  134. };
  135. namespace pybind11 { namespace detail {
  136. template <> struct type_caster<DestructionTester> {
  137. PYBIND11_TYPE_CASTER(DestructionTester, _("DestructionTester"));
  138. bool load(handle, bool) { return true; }
  139. static handle cast(const DestructionTester &, return_value_policy, handle) {
  140. return py::bool_(true).release();
  141. }
  142. };
  143. }}
  144. // Test None-allowed py::arg argument policy
  145. class NoneTester { public: int answer = 42; };
  146. int none1(const NoneTester &obj) { return obj.answer; }
  147. int none2(NoneTester *obj) { return obj ? obj->answer : -1; }
  148. int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
  149. int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
  150. int none5(std::shared_ptr<NoneTester> obj) { return obj ? obj->answer : -1; }
  151. struct StrIssue {
  152. int val = -1;
  153. StrIssue() = default;
  154. StrIssue(int i) : val{i} {}
  155. };
  156. // Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class
  157. class UnregisteredBase {
  158. public:
  159. void do_nothing() const {}
  160. void increase_value() { rw_value++; ro_value += 0.25; }
  161. void set_int(int v) { rw_value = v; }
  162. int get_int() const { return rw_value; }
  163. double get_double() const { return ro_value; }
  164. int rw_value = 42;
  165. double ro_value = 1.25;
  166. };
  167. class RegisteredDerived : public UnregisteredBase {
  168. public:
  169. using UnregisteredBase::UnregisteredBase;
  170. double sum() const { return rw_value + ro_value; }
  171. };
  172. TEST_SUBMODULE(methods_and_attributes, m) {
  173. // test_methods_and_attributes
  174. py::class_<ExampleMandA> emna(m, "ExampleMandA");
  175. emna.def(py::init<>())
  176. .def(py::init<int>())
  177. .def(py::init<const ExampleMandA&>())
  178. .def("add1", &ExampleMandA::add1)
  179. .def("add2", &ExampleMandA::add2)
  180. .def("add3", &ExampleMandA::add3)
  181. .def("add4", &ExampleMandA::add4)
  182. .def("add5", &ExampleMandA::add5)
  183. .def("add6", &ExampleMandA::add6)
  184. .def("add7", &ExampleMandA::add7)
  185. .def("add8", &ExampleMandA::add8)
  186. .def("add9", &ExampleMandA::add9)
  187. .def("add10", &ExampleMandA::add10)
  188. .def("self1", &ExampleMandA::self1)
  189. .def("self2", &ExampleMandA::self2)
  190. .def("self3", &ExampleMandA::self3)
  191. .def("self4", &ExampleMandA::self4)
  192. .def("self5", &ExampleMandA::self5)
  193. .def("internal1", &ExampleMandA::internal1)
  194. .def("internal2", &ExampleMandA::internal2)
  195. .def("internal3", &ExampleMandA::internal3)
  196. .def("internal4", &ExampleMandA::internal4)
  197. .def("internal5", &ExampleMandA::internal5)
  198. #if defined(PYBIND11_OVERLOAD_CAST)
  199. .def("overloaded", py::overload_cast<>(&ExampleMandA::overloaded))
  200. .def("overloaded", py::overload_cast<int>(&ExampleMandA::overloaded))
  201. .def("overloaded", py::overload_cast<int, float>(&ExampleMandA::overloaded))
  202. .def("overloaded", py::overload_cast<float, int>(&ExampleMandA::overloaded))
  203. .def("overloaded", py::overload_cast<int, int>(&ExampleMandA::overloaded))
  204. .def("overloaded", py::overload_cast<float, float>(&ExampleMandA::overloaded))
  205. .def("overloaded_float", py::overload_cast<float, float>(&ExampleMandA::overloaded))
  206. .def("overloaded_const", py::overload_cast<int >(&ExampleMandA::overloaded, py::const_))
  207. .def("overloaded_const", py::overload_cast<int, float>(&ExampleMandA::overloaded, py::const_))
  208. .def("overloaded_const", py::overload_cast<float, int>(&ExampleMandA::overloaded, py::const_))
  209. .def("overloaded_const", py::overload_cast<int, int>(&ExampleMandA::overloaded, py::const_))
  210. .def("overloaded_const", py::overload_cast<float, float>(&ExampleMandA::overloaded, py::const_))
  211. #else
  212. .def("overloaded", static_cast<py::str (ExampleMandA::*)()>(&ExampleMandA::overloaded))
  213. .def("overloaded", static_cast<py::str (ExampleMandA::*)(int)>(&ExampleMandA::overloaded))
  214. .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, float)>(&ExampleMandA::overloaded))
  215. .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, int)>(&ExampleMandA::overloaded))
  216. .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded))
  217. .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
  218. .def("overloaded_float", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
  219. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int ) const>(&ExampleMandA::overloaded))
  220. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, float) const>(&ExampleMandA::overloaded))
  221. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, int) const>(&ExampleMandA::overloaded))
  222. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, int) const>(&ExampleMandA::overloaded))
  223. .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, float) const>(&ExampleMandA::overloaded))
  224. #endif
  225. // test_no_mixed_overloads
  226. // Raise error if trying to mix static/non-static overloads on the same name:
  227. .def_static("add_mixed_overloads1", []() {
  228. auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests.methods_and_attributes").attr("ExampleMandA"));
  229. emna.def ("overload_mixed1", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded))
  230. .def_static("overload_mixed1", static_cast<py::str ( *)(float )>(&ExampleMandA::overloaded));
  231. })
  232. .def_static("add_mixed_overloads2", []() {
  233. auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests.methods_and_attributes").attr("ExampleMandA"));
  234. emna.def_static("overload_mixed2", static_cast<py::str ( *)(float )>(&ExampleMandA::overloaded))
  235. .def ("overload_mixed2", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded));
  236. })
  237. .def("__str__", &ExampleMandA::toString)
  238. .def_readwrite("value", &ExampleMandA::value);
  239. // test_copy_method
  240. // Issue #443: can't call copied methods in Python 3
  241. emna.attr("add2b") = emna.attr("add2");
  242. // test_properties, test_static_properties, test_static_cls
  243. py::class_<TestProperties>(m, "TestProperties")
  244. .def(py::init<>())
  245. .def_readonly("def_readonly", &TestProperties::value)
  246. .def_readwrite("def_readwrite", &TestProperties::value)
  247. .def_property_readonly("def_property_readonly", &TestProperties::get)
  248. .def_property("def_property", &TestProperties::get, &TestProperties::set)
  249. .def_readonly_static("def_readonly_static", &TestProperties::static_value)
  250. .def_readwrite_static("def_readwrite_static", &TestProperties::static_value)
  251. .def_property_readonly_static("def_property_readonly_static",
  252. [](py::object) { return TestProperties::static_get(); })
  253. .def_property_static("def_property_static",
  254. [](py::object) { return TestProperties::static_get(); },
  255. [](py::object, int v) { TestProperties::static_set(v); })
  256. .def_property_static("static_cls",
  257. [](py::object cls) { return cls; },
  258. [](py::object cls, py::function f) { f(cls); });
  259. py::class_<TestPropertiesOverride, TestProperties>(m, "TestPropertiesOverride")
  260. .def(py::init<>())
  261. .def_readonly("def_readonly", &TestPropertiesOverride::value)
  262. .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value);
  263. auto static_get1 = [](py::object) -> const UserType & { return TestPropRVP::sv1; };
  264. auto static_get2 = [](py::object) -> const UserType & { return TestPropRVP::sv2; };
  265. auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.set(v); };
  266. auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.set(v); };
  267. auto rvp_copy = py::return_value_policy::copy;
  268. // test_property_return_value_policies
  269. py::class_<TestPropRVP>(m, "TestPropRVP")
  270. .def(py::init<>())
  271. .def_property_readonly("ro_ref", &TestPropRVP::get1)
  272. .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy)
  273. .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy))
  274. .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1)
  275. .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy)
  276. .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
  277. .def_property_readonly_static("static_ro_ref", static_get1)
  278. .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy)
  279. .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy))
  280. .def_property_static("static_rw_ref", static_get1, static_set1)
  281. .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
  282. .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
  283. // test_property_rvalue_policy
  284. .def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
  285. .def_property_readonly_static("static_rvalue", [](py::object) { return UserType(1); });
  286. // test_metaclass_override
  287. struct MetaclassOverride { };
  288. py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
  289. .def_property_readonly_static("readonly", [](py::object) { return 1; });
  290. #if !defined(PYPY_VERSION)
  291. // test_dynamic_attributes
  292. class DynamicClass {
  293. public:
  294. DynamicClass() { print_default_created(this); }
  295. ~DynamicClass() { print_destroyed(this); }
  296. };
  297. py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
  298. .def(py::init());
  299. class CppDerivedDynamicClass : public DynamicClass { };
  300. py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
  301. .def(py::init());
  302. #endif
  303. // test_noconvert_args
  304. //
  305. // Test converting. The ArgAlwaysConverts is just there to make the first no-conversion pass
  306. // fail so that our call always ends up happening via the second dispatch (the one that allows
  307. // some conversion).
  308. class ArgInspector {
  309. public:
  310. ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; }
  311. std::string g(ArgInspector1 a, const ArgInspector1 &b, int c, ArgInspector2 *d, ArgAlwaysConverts) {
  312. return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg;
  313. }
  314. static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; }
  315. };
  316. py::class_<ArgInspector>(m, "ArgInspector")
  317. .def(py::init<>())
  318. .def("f", &ArgInspector::f, py::arg(), py::arg() = ArgAlwaysConverts())
  319. .def("g", &ArgInspector::g, "a"_a.noconvert(), "b"_a, "c"_a.noconvert()=13, "d"_a=ArgInspector2(), py::arg() = ArgAlwaysConverts())
  320. .def_static("h", &ArgInspector::h, py::arg().noconvert(), py::arg() = ArgAlwaysConverts())
  321. ;
  322. m.def("arg_inspect_func", [](ArgInspector2 a, ArgInspector1 b, ArgAlwaysConverts) { return a.arg + "\n" + b.arg; },
  323. py::arg().noconvert(false), py::arg_v(nullptr, ArgInspector1()).noconvert(true), py::arg() = ArgAlwaysConverts());
  324. m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
  325. m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
  326. m.def("ints_preferred", [](int i) { return i / 2; }, py::arg("i"));
  327. m.def("ints_only", [](int i) { return i / 2; }, py::arg("i").noconvert());
  328. // test_bad_arg_default
  329. // Issue/PR #648: bad arg default debugging output
  330. #if !defined(NDEBUG)
  331. m.attr("debug_enabled") = true;
  332. #else
  333. m.attr("debug_enabled") = false;
  334. #endif
  335. m.def("bad_arg_def_named", []{
  336. auto m = py::module::import("pybind11_tests");
  337. m.def("should_fail", [](int, UnregisteredType) {}, py::arg(), py::arg("a") = UnregisteredType());
  338. });
  339. m.def("bad_arg_def_unnamed", []{
  340. auto m = py::module::import("pybind11_tests");
  341. m.def("should_fail", [](int, UnregisteredType) {}, py::arg(), py::arg() = UnregisteredType());
  342. });
  343. // test_accepts_none
  344. py::class_<NoneTester, std::shared_ptr<NoneTester>>(m, "NoneTester")
  345. .def(py::init<>());
  346. m.def("no_none1", &none1, py::arg().none(false));
  347. m.def("no_none2", &none2, py::arg().none(false));
  348. m.def("no_none3", &none3, py::arg().none(false));
  349. m.def("no_none4", &none4, py::arg().none(false));
  350. m.def("no_none5", &none5, py::arg().none(false));
  351. m.def("ok_none1", &none1);
  352. m.def("ok_none2", &none2, py::arg().none(true));
  353. m.def("ok_none3", &none3);
  354. m.def("ok_none4", &none4, py::arg().none(true));
  355. m.def("ok_none5", &none5);
  356. // test_str_issue
  357. // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
  358. py::class_<StrIssue>(m, "StrIssue")
  359. .def(py::init<int>())
  360. .def(py::init<>())
  361. .def("__str__", [](const StrIssue &si) {
  362. return "StrIssue[" + std::to_string(si.val) + "]"; }
  363. );
  364. // test_unregistered_base_implementations
  365. //
  366. // Issues #854/910: incompatible function args when member function/pointer is in unregistered
  367. // base class The methods and member pointers below actually resolve to members/pointers in
  368. // UnregisteredBase; before this test/fix they would be registered via lambda with a first
  369. // argument of an unregistered type, and thus uncallable.
  370. py::class_<RegisteredDerived>(m, "RegisteredDerived")
  371. .def(py::init<>())
  372. .def("do_nothing", &RegisteredDerived::do_nothing)
  373. .def("increase_value", &RegisteredDerived::increase_value)
  374. .def_readwrite("rw_value", &RegisteredDerived::rw_value)
  375. .def_readonly("ro_value", &RegisteredDerived::ro_value)
  376. // These should trigger a static_assert if uncommented
  377. //.def_readwrite("fails", &UserType::value) // should trigger a static_assert if uncommented
  378. //.def_readonly("fails", &UserType::value) // should trigger a static_assert if uncommented
  379. .def_property("rw_value_prop", &RegisteredDerived::get_int, &RegisteredDerived::set_int)
  380. .def_property_readonly("ro_value_prop", &RegisteredDerived::get_double)
  381. // This one is in the registered class:
  382. .def("sum", &RegisteredDerived::sum)
  383. ;
  384. using Adapted = decltype(py::method_adaptor<RegisteredDerived>(&RegisteredDerived::do_nothing));
  385. static_assert(std::is_same<Adapted, void (RegisteredDerived::*)() const>::value, "");
  386. // test_custom_caster_destruction
  387. // Test that `take_ownership` works on types with a custom type caster when given a pointer
  388. // default policy: don't take ownership:
  389. m.def("custom_caster_no_destroy", []() { static auto *dt = new DestructionTester(); return dt; });
  390. m.def("custom_caster_destroy", []() { return new DestructionTester(); },
  391. py::return_value_policy::take_ownership); // Takes ownership: destroy when finished
  392. m.def("custom_caster_destroy_const", []() -> const DestructionTester * { return new DestructionTester(); },
  393. py::return_value_policy::take_ownership); // Likewise (const doesn't inhibit destruction)
  394. m.def("destruction_tester_cstats", &ConstructorStats::get<DestructionTester>, py::return_value_policy::reference);
  395. }