blob: c9a561c09f336a58ff040e2b265e9059d06a3c51 [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_virtual_functions.cpp -- overriding virtual functions from Python
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04003
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040012#include <pybind11/functional.h>
Wenzel Jakob44e39e02018-09-11 09:32:45 +020013#include <thread>
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040014
15/* This is an example class that we'll want to be able to extend from Python */
16class ExampleVirt {
17public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040018 ExampleVirt(int state) : state(state) { print_created(this, state); }
19 ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); }
20 ExampleVirt(ExampleVirt &&e) : state(e.state) { print_move_created(this); e.state = 0; }
Jason Rhinelandere763f042018-05-18 12:48:32 -030021 virtual ~ExampleVirt() { print_destroyed(this); }
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040022
23 virtual int run(int value) {
Dean Moldovan81511be2016-09-07 00:50:10 +020024 py::print("Original implementation of "
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040025 "ExampleVirt::run(state={}, value={}, str1={}, str2={})"_s.format(state, value, get_string1(), *get_string2()));
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040026 return state + value;
27 }
28
29 virtual bool run_bool() = 0;
30 virtual void pure_virtual() = 0;
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040031
32 // Returning a reference/pointer to a type converted from python (numbers, strings, etc.) is a
33 // bit trickier, because the actual int& or std::string& or whatever only exists temporarily, so
34 // we have to handle it specially in the trampoline class (see below).
35 virtual const std::string &get_string1() { return str1; }
36 virtual const std::string *get_string2() { return &str2; }
37
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040038private:
39 int state;
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040040 const std::string str1{"default1"}, str2{"default2"};
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040041};
42
43/* This is a wrapper class that must be generated */
44class PyExampleVirt : public ExampleVirt {
45public:
46 using ExampleVirt::ExampleVirt; /* Inherit constructors */
47
Jason Rhinelander116d37c2016-09-08 14:47:05 -040048 int run(int value) override {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040049 /* Generate wrapping code that enables native function overloading */
50 PYBIND11_OVERLOAD(
51 int, /* Return type */
52 ExampleVirt, /* Parent class */
53 run, /* Name of function */
54 value /* Argument(s) */
55 );
56 }
57
Jason Rhinelander116d37c2016-09-08 14:47:05 -040058 bool run_bool() override {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040059 PYBIND11_OVERLOAD_PURE(
60 bool, /* Return type */
61 ExampleVirt, /* Parent class */
62 run_bool, /* Name of function */
63 /* This function has no arguments. The trailing comma
64 in the previous line is needed for some compilers */
65 );
66 }
67
Jason Rhinelander116d37c2016-09-08 14:47:05 -040068 void pure_virtual() override {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040069 PYBIND11_OVERLOAD_PURE(
70 void, /* Return type */
71 ExampleVirt, /* Parent class */
72 pure_virtual, /* Name of function */
73 /* This function has no arguments. The trailing comma
74 in the previous line is needed for some compilers */
75 );
76 }
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040077
78 // We can return reference types for compatibility with C++ virtual interfaces that do so, but
79 // note they have some significant limitations (see the documentation).
80 const std::string &get_string1() override {
81 PYBIND11_OVERLOAD(
82 const std::string &, /* Return type */
83 ExampleVirt, /* Parent class */
84 get_string1, /* Name of function */
85 /* (no arguments) */
86 );
87 }
88
89 const std::string *get_string2() override {
90 PYBIND11_OVERLOAD(
91 const std::string *, /* Return type */
92 ExampleVirt, /* Parent class */
93 get_string2, /* Name of function */
94 /* (no arguments) */
95 );
96 }
97
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040098};
99
Jason Rhinelandered148792016-07-21 21:31:05 -0400100class NonCopyable {
101public:
Jason Rhinelander3f589372016-08-07 13:05:26 -0400102 NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); }
103 NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400104 NonCopyable(const NonCopyable &) = delete;
105 NonCopyable() = delete;
106 void operator=(const NonCopyable &) = delete;
107 void operator=(NonCopyable &&) = delete;
108 std::string get_value() const {
109 if (value) return std::to_string(*value); else return "(null)";
110 }
Jason Rhinelander3f589372016-08-07 13:05:26 -0400111 ~NonCopyable() { print_destroyed(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400112
113private:
114 std::unique_ptr<int> value;
115};
116
117// This is like the above, but is both copy and movable. In effect this means it should get moved
118// when it is not referenced elsewhere, but copied if it is still referenced.
119class Movable {
120public:
Jason Rhinelander3f589372016-08-07 13:05:26 -0400121 Movable(int a, int b) : value{a+b} { print_created(this, a, b); }
122 Movable(const Movable &m) { value = m.value; print_copy_created(this); }
123 Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200124 std::string get_value() const { return std::to_string(value); }
Jason Rhinelander3f589372016-08-07 13:05:26 -0400125 ~Movable() { print_destroyed(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400126private:
127 int value;
128};
129
130class NCVirt {
131public:
132 virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); }
133 virtual Movable get_movable(int a, int b) = 0;
134
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200135 std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); }
136 std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400137};
138class NCVirtTrampoline : public NCVirt {
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200139#if !defined(__INTEL_COMPILER)
Jason Rhinelander116d37c2016-09-08 14:47:05 -0400140 NonCopyable get_noncopyable(int a, int b) override {
Jason Rhinelandered148792016-07-21 21:31:05 -0400141 PYBIND11_OVERLOAD(NonCopyable, NCVirt, get_noncopyable, a, b);
142 }
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200143#endif
Jason Rhinelander116d37c2016-09-08 14:47:05 -0400144 Movable get_movable(int a, int b) override {
Jason Rhinelandered148792016-07-21 21:31:05 -0400145 PYBIND11_OVERLOAD_PURE(Movable, NCVirt, get_movable, a, b);
146 }
147};
148
Jason Rhinelander391c7542017-07-25 16:47:36 -0400149struct Base {
150 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
151 virtual std::string dispatch() const { return {}; };
Jason Rhinelanderc4e18002017-08-17 00:01:42 -0400152 virtual ~Base() = default;
Jason Rhinelander391c7542017-07-25 16:47:36 -0400153};
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400154
Jason Rhinelander391c7542017-07-25 16:47:36 -0400155struct DispatchIssue : Base {
156 virtual std::string dispatch() const {
157 PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
158 }
159};
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400160
Wenzel Jakob44e39e02018-09-11 09:32:45 +0200161static void test_gil() {
162 {
163 py::gil_scoped_acquire lock;
164 py::print("1st lock acquired");
165
166 }
167
168 {
169 py::gil_scoped_acquire lock;
170 py::print("2nd lock acquired");
171 }
172
173}
174
175static void test_gil_from_thread() {
176 py::gil_scoped_release release;
177
178 std::thread t(test_gil);
179 t.join();
180}
181
182
Jason Rhinelander391c7542017-07-25 16:47:36 -0400183// Forward declaration (so that we can put the main tests here; the inherited virtual approaches are
184// rather long).
185void initialize_inherited_virtuals(py::module &m);
186
187TEST_SUBMODULE(virtual_functions, m) {
188 // test_override
189 py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
190 .def(py::init<int>())
191 /* Reference original class in function definitions */
192 .def("run", &ExampleVirt::run)
193 .def("run_bool", &ExampleVirt::run_bool)
194 .def("pure_virtual", &ExampleVirt::pure_virtual);
195
196 py::class_<NonCopyable>(m, "NonCopyable")
197 .def(py::init<int, int>());
198
199 py::class_<Movable>(m, "Movable")
200 .def(py::init<int, int>());
201
202 // test_move_support
203#if !defined(__INTEL_COMPILER)
204 py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
205 .def(py::init<>())
206 .def("get_noncopyable", &NCVirt::get_noncopyable)
207 .def("get_movable", &NCVirt::get_movable)
208 .def("print_nc", &NCVirt::print_nc)
209 .def("print_movable", &NCVirt::print_movable);
210#endif
211
212 m.def("runExampleVirt", [](ExampleVirt *ex, int value) { return ex->run(value); });
213 m.def("runExampleVirtBool", [](ExampleVirt* ex) { return ex->run_bool(); });
214 m.def("runExampleVirtVirtual", [](ExampleVirt *ex) { ex->pure_virtual(); });
215
216 m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
217 initialize_inherited_virtuals(m);
218
219 // test_alias_delay_initialization1
220 // don't invoke Python dispatch classes by default when instantiating C++ classes
221 // that were not extended on the Python side
222 struct A {
223 virtual ~A() {}
224 virtual void f() { py::print("A.f()"); }
225 };
226
227 struct PyA : A {
228 PyA() { py::print("PyA.PyA()"); }
229 ~PyA() { py::print("PyA.~PyA()"); }
230
231 void f() override {
232 py::print("PyA.f()");
Jason Rhinelandere88656a2018-02-27 22:33:41 -0400233 // This convolution just gives a `void`, but tests that PYBIND11_TYPE() works to protect
234 // a type containing a ,
235 PYBIND11_OVERLOAD(PYBIND11_TYPE(typename std::enable_if<true, void>::type), A, f);
Jason Rhinelander391c7542017-07-25 16:47:36 -0400236 }
237 };
238
239 py::class_<A, PyA>(m, "A")
240 .def(py::init<>())
241 .def("f", &A::f);
242
243 m.def("call_f", [](A *a) { a->f(); });
244
245 // test_alias_delay_initialization2
246 // ... unless we explicitly request it, as in this example:
247 struct A2 {
248 virtual ~A2() {}
249 virtual void f() { py::print("A2.f()"); }
250 };
251
252 struct PyA2 : A2 {
253 PyA2() { py::print("PyA2.PyA2()"); }
254 ~PyA2() { py::print("PyA2.~PyA2()"); }
255 void f() override {
256 py::print("PyA2.f()");
257 PYBIND11_OVERLOAD(void, A2, f);
258 }
259 };
260
261 py::class_<A2, PyA2>(m, "A2")
262 .def(py::init_alias<>())
Jason Rhinelander464d9892017-06-12 21:52:48 -0400263 .def(py::init([](int) { return new PyA2(); }))
Jason Rhinelander391c7542017-07-25 16:47:36 -0400264 .def("f", &A2::f);
265
266 m.def("call_f", [](A2 *a2) { a2->f(); });
267
268 // test_dispatch_issue
269 // #159: virtual function dispatch has problems with similar-named functions
270 py::class_<Base, DispatchIssue>(m, "DispatchIssue")
271 .def(py::init<>())
272 .def("dispatch", &Base::dispatch);
273
274 m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
275
276 // test_override_ref
Unknown0b3f44e2017-11-01 21:08:06 -0400277 // #392/397: overriding reference-returning functions
Jason Rhinelander391c7542017-07-25 16:47:36 -0400278 class OverrideTest {
279 public:
280 struct A { std::string value = "hi"; };
281 std::string v;
282 A a;
283 explicit OverrideTest(const std::string &v) : v{v} {}
284 virtual std::string str_value() { return v; }
285 virtual std::string &str_ref() { return v; }
286 virtual A A_value() { return a; }
287 virtual A &A_ref() { return a; }
Jason Rhinelanderc4e18002017-08-17 00:01:42 -0400288 virtual ~OverrideTest() = default;
Jason Rhinelander391c7542017-07-25 16:47:36 -0400289 };
290
291 class PyOverrideTest : public OverrideTest {
292 public:
293 using OverrideTest::OverrideTest;
294 std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
295 // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
296 // to a python numeric value, since we only copy values in the numeric type caster:
297// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
298 // But we can work around it like this:
299 private:
300 std::string _tmp;
301 std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
302 public:
303 std::string &str_ref() override { return _tmp = str_ref_helper(); }
304
305 A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
306 A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
307 };
308
309 py::class_<OverrideTest::A>(m, "OverrideTest_A")
310 .def_readwrite("value", &OverrideTest::A::value);
311 py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest")
312 .def(py::init<const std::string &>())
313 .def("str_value", &OverrideTest::str_value)
314// .def("str_ref", &OverrideTest::str_ref)
315 .def("A_value", &OverrideTest::A_value)
316 .def("A_ref", &OverrideTest::A_ref);
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400317}
318
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400319
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400320// Inheriting virtual methods. We do two versions here: the repeat-everything version and the
321// templated trampoline versions mentioned in docs/advanced.rst.
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400322//
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400323// These base classes are exactly the same, but we technically need distinct
324// classes for this example code because we need to be able to bind them
325// properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400326// multiple python classes).
327class A_Repeat {
328#define A_METHODS \
329public: \
330 virtual int unlucky_number() = 0; \
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200331 virtual std::string say_something(unsigned times) { \
332 std::string s = ""; \
333 for (unsigned i = 0; i < times; ++i) \
334 s += "hi"; \
335 return s; \
Jason Rhinelander20978262016-08-29 18:16:46 -0400336 } \
337 std::string say_everything() { \
338 return say_something(1) + " " + std::to_string(unlucky_number()); \
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400339 }
340A_METHODS
Jason Rhinelanderc4e18002017-08-17 00:01:42 -0400341 virtual ~A_Repeat() = default;
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400342};
343class B_Repeat : public A_Repeat {
344#define B_METHODS \
345public: \
346 int unlucky_number() override { return 13; } \
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200347 std::string say_something(unsigned times) override { \
348 return "B says hi " + std::to_string(times) + " times"; \
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400349 } \
350 virtual double lucky_number() { return 7.0; }
351B_METHODS
352};
353class C_Repeat : public B_Repeat {
354#define C_METHODS \
355public: \
356 int unlucky_number() override { return 4444; } \
357 double lucky_number() override { return 888; }
358C_METHODS
359};
360class D_Repeat : public C_Repeat {
361#define D_METHODS // Nothing overridden.
362D_METHODS
363};
364
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400365// Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
Jason Rhinelanderc4e18002017-08-17 00:01:42 -0400366class A_Tpl { A_METHODS; virtual ~A_Tpl() = default; };
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400367class B_Tpl : public A_Tpl { B_METHODS };
368class C_Tpl : public B_Tpl { C_METHODS };
369class D_Tpl : public C_Tpl { D_METHODS };
370
371
372// Inheritance approach 1: each trampoline gets every virtual method (11 in total)
373class PyA_Repeat : public A_Repeat {
374public:
375 using A_Repeat::A_Repeat;
376 int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, A_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200377 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, A_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400378};
379class PyB_Repeat : public B_Repeat {
380public:
381 using B_Repeat::B_Repeat;
382 int unlucky_number() override { PYBIND11_OVERLOAD(int, B_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200383 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, B_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400384 double lucky_number() override { PYBIND11_OVERLOAD(double, B_Repeat, lucky_number, ); }
385};
386class PyC_Repeat : public C_Repeat {
387public:
388 using C_Repeat::C_Repeat;
389 int unlucky_number() override { PYBIND11_OVERLOAD(int, C_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200390 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, C_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400391 double lucky_number() override { PYBIND11_OVERLOAD(double, C_Repeat, lucky_number, ); }
392};
393class PyD_Repeat : public D_Repeat {
394public:
395 using D_Repeat::D_Repeat;
396 int unlucky_number() override { PYBIND11_OVERLOAD(int, D_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200397 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, D_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400398 double lucky_number() override { PYBIND11_OVERLOAD(double, D_Repeat, lucky_number, ); }
399};
400
401// Inheritance approach 2: templated trampoline classes.
402//
403// Advantages:
404// - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one for
405// any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes and 11
406// methods (repeat).
407// - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and can
408// properly inherit constructors.
409//
410// Disadvantage:
411// - the compiler must still generate and compile 14 different methods (more, even, than the 11
412// required for the repeat approach) instead of the 6 required for MI. (If there was no pure
413// method (or no pure method override), the number would drop down to the same 11 as the repeat
414// approach).
415template <class Base = A_Tpl>
416class PyA_Tpl : public Base {
417public:
418 using Base::Base; // Inherit constructors
419 int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, Base, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200420 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, Base, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400421};
422template <class Base = B_Tpl>
423class PyB_Tpl : public PyA_Tpl<Base> {
424public:
425 using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors)
426 int unlucky_number() override { PYBIND11_OVERLOAD(int, Base, unlucky_number, ); }
Wenzel Jakob216df0d2016-08-12 00:59:57 +0200427 double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400428};
429// Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can
430// use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead):
431/*
432template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
433public:
434 using PyB_Tpl<Base>::PyB_Tpl;
435};
436template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
437public:
438 using PyC_Tpl<Base>::PyC_Tpl;
439};
440*/
441
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400442void initialize_inherited_virtuals(py::module &m) {
Jason Rhinelander391c7542017-07-25 16:47:36 -0400443 // test_inherited_virtuals
444
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400445 // Method 1: repeat
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400446 py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400447 .def(py::init<>())
448 .def("unlucky_number", &A_Repeat::unlucky_number)
Jason Rhinelander20978262016-08-29 18:16:46 -0400449 .def("say_something", &A_Repeat::say_something)
450 .def("say_everything", &A_Repeat::say_everything);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400451 py::class_<B_Repeat, A_Repeat, PyB_Repeat>(m, "B_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400452 .def(py::init<>())
453 .def("lucky_number", &B_Repeat::lucky_number);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400454 py::class_<C_Repeat, B_Repeat, PyC_Repeat>(m, "C_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400455 .def(py::init<>());
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400456 py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400457 .def(py::init<>());
458
Jason Rhinelander391c7542017-07-25 16:47:36 -0400459 // test_
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400460 // Method 2: Templated trampolines
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400461 py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400462 .def(py::init<>())
463 .def("unlucky_number", &A_Tpl::unlucky_number)
Jason Rhinelander20978262016-08-29 18:16:46 -0400464 .def("say_something", &A_Tpl::say_something)
465 .def("say_everything", &A_Tpl::say_everything);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400466 py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(m, "B_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400467 .def(py::init<>())
468 .def("lucky_number", &B_Tpl::lucky_number);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400469 py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(m, "C_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400470 .def(py::init<>());
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400471 py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(m, "D_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400472 .def(py::init<>());
473
Wenzel Jakob44e39e02018-09-11 09:32:45 +0200474
475 // Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7)
476 m.def("test_gil", &test_gil);
477 m.def("test_gil_from_thread", &test_gil_from_thread);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400478};