test_virtual_functions.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. tests/test_virtual_functions.cpp -- overriding virtual functions from Python
  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 "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. #include <pybind11/functional.h>
  10. #include <thread>
  11. /* This is an example class that we'll want to be able to extend from Python */
  12. class ExampleVirt {
  13. public:
  14. ExampleVirt(int state) : state(state) { print_created(this, state); }
  15. ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); }
  16. ExampleVirt(ExampleVirt &&e) : state(e.state) { print_move_created(this); e.state = 0; }
  17. virtual ~ExampleVirt() { print_destroyed(this); }
  18. virtual int run(int value) {
  19. py::print("Original implementation of "
  20. "ExampleVirt::run(state={}, value={}, str1={}, str2={})"_s.format(state, value, get_string1(), *get_string2()));
  21. return state + value;
  22. }
  23. virtual bool run_bool() = 0;
  24. virtual void pure_virtual() = 0;
  25. // Returning a reference/pointer to a type converted from python (numbers, strings, etc.) is a
  26. // bit trickier, because the actual int& or std::string& or whatever only exists temporarily, so
  27. // we have to handle it specially in the trampoline class (see below).
  28. virtual const std::string &get_string1() { return str1; }
  29. virtual const std::string *get_string2() { return &str2; }
  30. private:
  31. int state;
  32. const std::string str1{"default1"}, str2{"default2"};
  33. };
  34. /* This is a wrapper class that must be generated */
  35. class PyExampleVirt : public ExampleVirt {
  36. public:
  37. using ExampleVirt::ExampleVirt; /* Inherit constructors */
  38. int run(int value) override {
  39. /* Generate wrapping code that enables native function overloading */
  40. PYBIND11_OVERRIDE(
  41. int, /* Return type */
  42. ExampleVirt, /* Parent class */
  43. run, /* Name of function */
  44. value /* Argument(s) */
  45. );
  46. }
  47. bool run_bool() override {
  48. PYBIND11_OVERRIDE_PURE(
  49. bool, /* Return type */
  50. ExampleVirt, /* Parent class */
  51. run_bool, /* Name of function */
  52. /* This function has no arguments. The trailing comma
  53. in the previous line is needed for some compilers */
  54. );
  55. }
  56. void pure_virtual() override {
  57. PYBIND11_OVERRIDE_PURE(
  58. void, /* Return type */
  59. ExampleVirt, /* Parent class */
  60. pure_virtual, /* Name of function */
  61. /* This function has no arguments. The trailing comma
  62. in the previous line is needed for some compilers */
  63. );
  64. }
  65. // We can return reference types for compatibility with C++ virtual interfaces that do so, but
  66. // note they have some significant limitations (see the documentation).
  67. const std::string &get_string1() override {
  68. PYBIND11_OVERRIDE(
  69. const std::string &, /* Return type */
  70. ExampleVirt, /* Parent class */
  71. get_string1, /* Name of function */
  72. /* (no arguments) */
  73. );
  74. }
  75. const std::string *get_string2() override {
  76. PYBIND11_OVERRIDE(
  77. const std::string *, /* Return type */
  78. ExampleVirt, /* Parent class */
  79. get_string2, /* Name of function */
  80. /* (no arguments) */
  81. );
  82. }
  83. };
  84. class NonCopyable {
  85. public:
  86. NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); }
  87. NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); }
  88. NonCopyable(const NonCopyable &) = delete;
  89. NonCopyable() = delete;
  90. void operator=(const NonCopyable &) = delete;
  91. void operator=(NonCopyable &&) = delete;
  92. std::string get_value() const {
  93. if (value) return std::to_string(*value); else return "(null)";
  94. }
  95. ~NonCopyable() { print_destroyed(this); }
  96. private:
  97. std::unique_ptr<int> value;
  98. };
  99. // This is like the above, but is both copy and movable. In effect this means it should get moved
  100. // when it is not referenced elsewhere, but copied if it is still referenced.
  101. class Movable {
  102. public:
  103. Movable(int a, int b) : value{a+b} { print_created(this, a, b); }
  104. Movable(const Movable &m) { value = m.value; print_copy_created(this); }
  105. Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); }
  106. std::string get_value() const { return std::to_string(value); }
  107. ~Movable() { print_destroyed(this); }
  108. private:
  109. int value;
  110. };
  111. class NCVirt {
  112. public:
  113. virtual ~NCVirt() = default;
  114. NCVirt() = default;
  115. NCVirt(const NCVirt&) = delete;
  116. virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); }
  117. virtual Movable get_movable(int a, int b) = 0;
  118. std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); }
  119. std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); }
  120. };
  121. class NCVirtTrampoline : public NCVirt {
  122. #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
  123. NonCopyable get_noncopyable(int a, int b) override {
  124. PYBIND11_OVERRIDE(NonCopyable, NCVirt, get_noncopyable, a, b);
  125. }
  126. #endif
  127. Movable get_movable(int a, int b) override {
  128. PYBIND11_OVERRIDE_PURE(Movable, NCVirt, get_movable, a, b);
  129. }
  130. };
  131. struct Base {
  132. /* for some reason MSVC2015 can't compile this if the function is pure virtual */
  133. virtual std::string dispatch() const { return {}; };
  134. virtual ~Base() = default;
  135. Base() = default;
  136. Base(const Base&) = delete;
  137. };
  138. struct DispatchIssue : Base {
  139. std::string dispatch() const override {
  140. PYBIND11_OVERRIDE_PURE(std::string, Base, dispatch, /* no arguments */);
  141. }
  142. };
  143. static void test_gil() {
  144. {
  145. py::gil_scoped_acquire lock;
  146. py::print("1st lock acquired");
  147. }
  148. {
  149. py::gil_scoped_acquire lock;
  150. py::print("2nd lock acquired");
  151. }
  152. }
  153. static void test_gil_from_thread() {
  154. py::gil_scoped_release release;
  155. std::thread t(test_gil);
  156. t.join();
  157. }
  158. // Forward declaration (so that we can put the main tests here; the inherited virtual approaches are
  159. // rather long).
  160. void initialize_inherited_virtuals(py::module_ &m);
  161. TEST_SUBMODULE(virtual_functions, m) {
  162. // test_override
  163. py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
  164. .def(py::init<int>())
  165. /* Reference original class in function definitions */
  166. .def("run", &ExampleVirt::run)
  167. .def("run_bool", &ExampleVirt::run_bool)
  168. .def("pure_virtual", &ExampleVirt::pure_virtual);
  169. py::class_<NonCopyable>(m, "NonCopyable")
  170. .def(py::init<int, int>());
  171. py::class_<Movable>(m, "Movable")
  172. .def(py::init<int, int>());
  173. // test_move_support
  174. #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
  175. py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
  176. .def(py::init<>())
  177. .def("get_noncopyable", &NCVirt::get_noncopyable)
  178. .def("get_movable", &NCVirt::get_movable)
  179. .def("print_nc", &NCVirt::print_nc)
  180. .def("print_movable", &NCVirt::print_movable);
  181. #endif
  182. m.def("runExampleVirt", [](ExampleVirt *ex, int value) { return ex->run(value); });
  183. m.def("runExampleVirtBool", [](ExampleVirt* ex) { return ex->run_bool(); });
  184. m.def("runExampleVirtVirtual", [](ExampleVirt *ex) { ex->pure_virtual(); });
  185. m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
  186. initialize_inherited_virtuals(m);
  187. // test_alias_delay_initialization1
  188. // don't invoke Python dispatch classes by default when instantiating C++ classes
  189. // that were not extended on the Python side
  190. struct A {
  191. A() = default;
  192. A(const A&) = delete;
  193. virtual ~A() = default;
  194. virtual void f() { py::print("A.f()"); }
  195. };
  196. struct PyA : A {
  197. PyA() { py::print("PyA.PyA()"); }
  198. PyA(const PyA&) = delete;
  199. ~PyA() override { py::print("PyA.~PyA()"); }
  200. void f() override {
  201. py::print("PyA.f()");
  202. // This convolution just gives a `void`, but tests that PYBIND11_TYPE() works to protect
  203. // a type containing a ,
  204. PYBIND11_OVERRIDE(PYBIND11_TYPE(typename std::enable_if<true, void>::type), A, f);
  205. }
  206. };
  207. py::class_<A, PyA>(m, "A")
  208. .def(py::init<>())
  209. .def("f", &A::f);
  210. m.def("call_f", [](A *a) { a->f(); });
  211. // test_alias_delay_initialization2
  212. // ... unless we explicitly request it, as in this example:
  213. struct A2 {
  214. A2() = default;
  215. A2(const A2&) = delete;
  216. virtual ~A2() = default;
  217. virtual void f() { py::print("A2.f()"); }
  218. };
  219. struct PyA2 : A2 {
  220. PyA2() { py::print("PyA2.PyA2()"); }
  221. PyA2(const PyA2&) = delete;
  222. ~PyA2() override { py::print("PyA2.~PyA2()"); }
  223. void f() override {
  224. py::print("PyA2.f()");
  225. PYBIND11_OVERRIDE(void, A2, f);
  226. }
  227. };
  228. py::class_<A2, PyA2>(m, "A2")
  229. .def(py::init_alias<>())
  230. .def(py::init([](int) { return new PyA2(); }))
  231. .def("f", &A2::f);
  232. m.def("call_f", [](A2 *a2) { a2->f(); });
  233. // test_dispatch_issue
  234. // #159: virtual function dispatch has problems with similar-named functions
  235. py::class_<Base, DispatchIssue>(m, "DispatchIssue")
  236. .def(py::init<>())
  237. .def("dispatch", &Base::dispatch);
  238. m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
  239. // test_override_ref
  240. // #392/397: overriding reference-returning functions
  241. class OverrideTest {
  242. public:
  243. struct A { std::string value = "hi"; };
  244. std::string v;
  245. A a;
  246. explicit OverrideTest(const std::string &v) : v{v} {}
  247. OverrideTest() = default;
  248. OverrideTest(const OverrideTest&) = delete;
  249. virtual std::string str_value() { return v; }
  250. virtual std::string &str_ref() { return v; }
  251. virtual A A_value() { return a; }
  252. virtual A &A_ref() { return a; }
  253. virtual ~OverrideTest() = default;
  254. };
  255. class PyOverrideTest : public OverrideTest {
  256. public:
  257. using OverrideTest::OverrideTest;
  258. std::string str_value() override { PYBIND11_OVERRIDE(std::string, OverrideTest, str_value); }
  259. // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
  260. // to a python numeric value, since we only copy values in the numeric type caster:
  261. // std::string &str_ref() override { PYBIND11_OVERRIDE(std::string &, OverrideTest, str_ref); }
  262. // But we can work around it like this:
  263. private:
  264. std::string _tmp;
  265. std::string str_ref_helper() { PYBIND11_OVERRIDE(std::string, OverrideTest, str_ref); }
  266. public:
  267. std::string &str_ref() override { return _tmp = str_ref_helper(); }
  268. A A_value() override { PYBIND11_OVERRIDE(A, OverrideTest, A_value); }
  269. A &A_ref() override { PYBIND11_OVERRIDE(A &, OverrideTest, A_ref); }
  270. };
  271. py::class_<OverrideTest::A>(m, "OverrideTest_A")
  272. .def_readwrite("value", &OverrideTest::A::value);
  273. py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest")
  274. .def(py::init<const std::string &>())
  275. .def("str_value", &OverrideTest::str_value)
  276. // .def("str_ref", &OverrideTest::str_ref)
  277. .def("A_value", &OverrideTest::A_value)
  278. .def("A_ref", &OverrideTest::A_ref);
  279. }
  280. // Inheriting virtual methods. We do two versions here: the repeat-everything version and the
  281. // templated trampoline versions mentioned in docs/advanced.rst.
  282. //
  283. // These base classes are exactly the same, but we technically need distinct
  284. // classes for this example code because we need to be able to bind them
  285. // properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to
  286. // multiple python classes).
  287. class A_Repeat {
  288. #define A_METHODS \
  289. public: \
  290. virtual int unlucky_number() = 0; \
  291. virtual std::string say_something(unsigned times) { \
  292. std::string s = ""; \
  293. for (unsigned i = 0; i < times; ++i) \
  294. s += "hi"; \
  295. return s; \
  296. } \
  297. std::string say_everything() { \
  298. return say_something(1) + " " + std::to_string(unlucky_number()); \
  299. }
  300. A_METHODS
  301. A_Repeat() = default;
  302. A_Repeat(const A_Repeat&) = delete;
  303. virtual ~A_Repeat() = default;
  304. };
  305. class B_Repeat : public A_Repeat {
  306. #define B_METHODS \
  307. public: \
  308. int unlucky_number() override { return 13; } \
  309. std::string say_something(unsigned times) override { \
  310. return "B says hi " + std::to_string(times) + " times"; \
  311. } \
  312. virtual double lucky_number() { return 7.0; }
  313. B_METHODS
  314. };
  315. class C_Repeat : public B_Repeat {
  316. #define C_METHODS \
  317. public: \
  318. int unlucky_number() override { return 4444; } \
  319. double lucky_number() override { return 888; }
  320. C_METHODS
  321. };
  322. class D_Repeat : public C_Repeat {
  323. #define D_METHODS // Nothing overridden.
  324. D_METHODS
  325. };
  326. // Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
  327. class A_Tpl {
  328. A_METHODS;
  329. A_Tpl() = default;
  330. A_Tpl(const A_Tpl&) = delete;
  331. virtual ~A_Tpl() = default;
  332. };
  333. class B_Tpl : public A_Tpl { B_METHODS };
  334. class C_Tpl : public B_Tpl { C_METHODS };
  335. class D_Tpl : public C_Tpl { D_METHODS };
  336. // Inheritance approach 1: each trampoline gets every virtual method (11 in total)
  337. class PyA_Repeat : public A_Repeat {
  338. public:
  339. using A_Repeat::A_Repeat;
  340. int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, A_Repeat, unlucky_number, ); }
  341. std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, A_Repeat, say_something, times); }
  342. };
  343. class PyB_Repeat : public B_Repeat {
  344. public:
  345. using B_Repeat::B_Repeat;
  346. int unlucky_number() override { PYBIND11_OVERRIDE(int, B_Repeat, unlucky_number, ); }
  347. std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, B_Repeat, say_something, times); }
  348. double lucky_number() override { PYBIND11_OVERRIDE(double, B_Repeat, lucky_number, ); }
  349. };
  350. class PyC_Repeat : public C_Repeat {
  351. public:
  352. using C_Repeat::C_Repeat;
  353. int unlucky_number() override { PYBIND11_OVERRIDE(int, C_Repeat, unlucky_number, ); }
  354. std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, C_Repeat, say_something, times); }
  355. double lucky_number() override { PYBIND11_OVERRIDE(double, C_Repeat, lucky_number, ); }
  356. };
  357. class PyD_Repeat : public D_Repeat {
  358. public:
  359. using D_Repeat::D_Repeat;
  360. int unlucky_number() override { PYBIND11_OVERRIDE(int, D_Repeat, unlucky_number, ); }
  361. std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, D_Repeat, say_something, times); }
  362. double lucky_number() override { PYBIND11_OVERRIDE(double, D_Repeat, lucky_number, ); }
  363. };
  364. // Inheritance approach 2: templated trampoline classes.
  365. //
  366. // Advantages:
  367. // - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one for
  368. // any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes and 11
  369. // methods (repeat).
  370. // - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and can
  371. // properly inherit constructors.
  372. //
  373. // Disadvantage:
  374. // - the compiler must still generate and compile 14 different methods (more, even, than the 11
  375. // required for the repeat approach) instead of the 6 required for MI. (If there was no pure
  376. // method (or no pure method override), the number would drop down to the same 11 as the repeat
  377. // approach).
  378. template <class Base = A_Tpl>
  379. class PyA_Tpl : public Base {
  380. public:
  381. using Base::Base; // Inherit constructors
  382. int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, Base, unlucky_number, ); }
  383. std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, Base, say_something, times); }
  384. };
  385. template <class Base = B_Tpl>
  386. class PyB_Tpl : public PyA_Tpl<Base> {
  387. public:
  388. using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors)
  389. int unlucky_number() override { PYBIND11_OVERRIDE(int, Base, unlucky_number, ); }
  390. double lucky_number() override { PYBIND11_OVERRIDE(double, Base, lucky_number, ); }
  391. };
  392. // Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can
  393. // use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead):
  394. /*
  395. template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
  396. public:
  397. using PyB_Tpl<Base>::PyB_Tpl;
  398. };
  399. template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
  400. public:
  401. using PyC_Tpl<Base>::PyC_Tpl;
  402. };
  403. */
  404. void initialize_inherited_virtuals(py::module_ &m) {
  405. // test_inherited_virtuals
  406. // Method 1: repeat
  407. py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat")
  408. .def(py::init<>())
  409. .def("unlucky_number", &A_Repeat::unlucky_number)
  410. .def("say_something", &A_Repeat::say_something)
  411. .def("say_everything", &A_Repeat::say_everything);
  412. py::class_<B_Repeat, A_Repeat, PyB_Repeat>(m, "B_Repeat")
  413. .def(py::init<>())
  414. .def("lucky_number", &B_Repeat::lucky_number);
  415. py::class_<C_Repeat, B_Repeat, PyC_Repeat>(m, "C_Repeat")
  416. .def(py::init<>());
  417. py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat")
  418. .def(py::init<>());
  419. // test_
  420. // Method 2: Templated trampolines
  421. py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl")
  422. .def(py::init<>())
  423. .def("unlucky_number", &A_Tpl::unlucky_number)
  424. .def("say_something", &A_Tpl::say_something)
  425. .def("say_everything", &A_Tpl::say_everything);
  426. py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(m, "B_Tpl")
  427. .def(py::init<>())
  428. .def("lucky_number", &B_Tpl::lucky_number);
  429. py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(m, "C_Tpl")
  430. .def(py::init<>());
  431. py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(m, "D_Tpl")
  432. .def(py::init<>());
  433. // Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7)
  434. m.def("test_gil", &test_gil);
  435. m.def("test_gil_from_thread", &test_gil_from_thread);
  436. };