test_virtual_functions.cpp 22.5 KB
Newer Older
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/test_virtual_functions.cpp -- overriding virtual functions from Python
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6
7
8
9

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

10
#include <pybind11/functional.h>
11
12
13
14

#include "constructor_stats.h"
#include "pybind11_tests.h"

15
#include <thread>
16
17

/* This is an example class that we'll want to be able to extend from Python */
18
class ExampleVirt {
19
public:
20
    explicit ExampleVirt(int state) : state(state) { print_created(this, state); }
21
    ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); }
22
23
24
25
    ExampleVirt(ExampleVirt &&e) noexcept : state(e.state) {
        print_move_created(this);
        e.state = 0;
    }
26
    virtual ~ExampleVirt() { print_destroyed(this); }
27
28

    virtual int run(int value) {
29
        py::print("Original implementation of "
30
31
                  "ExampleVirt::run(state={}, value={}, str1={}, str2={})"_s.format(
                      state, value, get_string1(), *get_string2()));
32
33
34
        return state + value;
    }

35
    virtual bool run_bool() = 0;
36
    virtual void pure_virtual() = 0;
37
38

    // Returning a reference/pointer to a type converted from python (numbers, strings, etc.) is a
39
40
    // bit trickier, because the actual int& or std::string& or whatever only exists temporarily,
    // so we have to handle it specially in the trampoline class (see below).
41
42
43
    virtual const std::string &get_string1() { return str1; }
    virtual const std::string *get_string2() { return &str2; }

44
45
private:
    int state;
46
    const std::string str1{"default1"}, str2{"default2"};
47
48
49
};

/* This is a wrapper class that must be generated */
50
class PyExampleVirt : public ExampleVirt {
51
public:
52
    using ExampleVirt::ExampleVirt; /* Inherit constructors */
53

54
    int run(int value) override {
55
        /* Generate wrapping code that enables native function overloading */
56
57
58
59
        PYBIND11_OVERRIDE(int,         /* Return type */
                          ExampleVirt, /* Parent class */
                          run,         /* Name of function */
                          value        /* Argument(s) */
60
61
62
        );
    }

63
    bool run_bool() override {
64
65
66
67
68
        PYBIND11_OVERRIDE_PURE(bool,        /* Return type */
                               ExampleVirt, /* Parent class */
                               run_bool,    /* Name of function */
                                            /* This function has no arguments. The trailing comma
                                               in the previous line is needed for some compilers */
69
70
71
        );
    }

72
    void pure_virtual() override {
73
74
75
76
77
        PYBIND11_OVERRIDE_PURE(void,         /* Return type */
                               ExampleVirt,  /* Parent class */
                               pure_virtual, /* Name of function */
                                             /* This function has no arguments. The trailing comma
                                                in the previous line is needed for some compilers */
78
79
        );
    }
80
81
82
83

    // We can return reference types for compatibility with C++ virtual interfaces that do so, but
    // note they have some significant limitations (see the documentation).
    const std::string &get_string1() override {
84
85
86
87
        PYBIND11_OVERRIDE(const std::string &, /* Return type */
                          ExampleVirt,         /* Parent class */
                          get_string1,         /* Name of function */
                                               /* (no arguments) */
88
89
90
91
        );
    }

    const std::string *get_string2() override {
92
93
94
95
        PYBIND11_OVERRIDE(const std::string *, /* Return type */
                          ExampleVirt,         /* Parent class */
                          get_string2,         /* Name of function */
                                               /* (no arguments) */
96
97
        );
    }
98
99
};

100
101
class NonCopyable {
public:
102
    NonCopyable(int a, int b) : value{new int(a * b)} { print_created(this, a, b); }
103
    NonCopyable(NonCopyable &&o) noexcept : value{std::move(o.value)} { print_move_created(this); }
104
105
106
107
108
    NonCopyable(const NonCopyable &) = delete;
    NonCopyable() = delete;
    void operator=(const NonCopyable &) = delete;
    void operator=(NonCopyable &&) = delete;
    std::string get_value() const {
109
        if (value) {
110
            return std::to_string(*value);
111
        }
112
        return "(null)";
113
    }
114
    ~NonCopyable() { print_destroyed(this); }
115
116
117
118
119
120
121
122
123

private:
    std::unique_ptr<int> value;
};

// This is like the above, but is both copy and movable.  In effect this means it should get moved
// when it is not referenced elsewhere, but copied if it is still referenced.
class Movable {
public:
124
    Movable(int a, int b) : value{a + b} { print_created(this, a, b); }
125
126
    Movable(const Movable &m) : value{m.value} { print_copy_created(this); }
    Movable(Movable &&m) noexcept : value{m.value} { print_move_created(this); }
127
    std::string get_value() const { return std::to_string(value); }
128
    ~Movable() { print_destroyed(this); }
129

130
131
132
133
134
135
private:
    int value;
};

class NCVirt {
public:
136
    virtual ~NCVirt() = default;
Henry Schreiner's avatar
Henry Schreiner committed
137
    NCVirt() = default;
138
    NCVirt(const NCVirt &) = delete;
139
140
141
    virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); }
    virtual Movable get_movable(int a, int b) = 0;

142
143
    std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); }
    std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); }
144
145
};
class NCVirtTrampoline : public NCVirt {
146
#if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
147
    NonCopyable get_noncopyable(int a, int b) override {
148
        PYBIND11_OVERRIDE(NonCopyable, NCVirt, get_noncopyable, a, b);
149
    }
150
#endif
151
    Movable get_movable(int a, int b) override {
152
        PYBIND11_OVERRIDE_PURE(Movable, NCVirt, get_movable, a, b);
153
154
155
    }
};

156
struct Base {
157
    virtual std::string dispatch() const = 0;
158
    virtual ~Base() = default;
Henry Schreiner's avatar
Henry Schreiner committed
159
    Base() = default;
160
    Base(const Base &) = delete;
161
};
162

163
struct DispatchIssue : Base {
164
    std::string dispatch() const override {
165
        PYBIND11_OVERRIDE_PURE(std::string, Base, dispatch, /* no arguments */);
166
167
168
    }
};

169
170
171
172
// An abstract adder class that uses visitor pattern to add two data
// objects and send the result to the visitor functor
struct AdderBase {
    struct Data {};
173
    using DataVisitor = std::function<void(const Data &)>;
174

175
    virtual void
176
177
    operator()(const Data &first, const Data &second, const DataVisitor &visitor) const
        = 0;
178
179
    virtual ~AdderBase() = default;
    AdderBase() = default;
180
    AdderBase(const AdderBase &) = delete;
181
182
183
};

struct Adder : AdderBase {
184
185
186
187
    void
    operator()(const Data &first, const Data &second, const DataVisitor &visitor) const override {
        PYBIND11_OVERRIDE_PURE_NAME(
            void, AdderBase, "__call__", operator(), first, second, visitor);
188
189
190
    }
};

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
static void test_gil() {
    {
        py::gil_scoped_acquire lock;
        py::print("1st lock acquired");
    }

    {
        py::gil_scoped_acquire lock;
        py::print("2nd lock acquired");
    }
}

static void test_gil_from_thread() {
    py::gil_scoped_release release;

    std::thread t(test_gil);
    t.join();
}

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
class test_override_cache_helper {

public:
    virtual int func() { return 0; }

    test_override_cache_helper() = default;
    virtual ~test_override_cache_helper() = default;
    // Non-copyable
    test_override_cache_helper &operator=(test_override_cache_helper const &Right) = delete;
    test_override_cache_helper(test_override_cache_helper const &Copy) = delete;
};

class test_override_cache_helper_trampoline : public test_override_cache_helper {
    int func() override { PYBIND11_OVERRIDE(int, test_override_cache_helper, func); }
};

226
227
228
inline int test_override_cache(std::shared_ptr<test_override_cache_helper> const &instance) {
    return instance->func();
}
229

230
231
// Forward declaration (so that we can put the main tests here; the inherited virtual approaches
// are rather long).
232
void initialize_inherited_virtuals(py::module_ &m);
233
234
235
236
237
238
239
240
241
242

TEST_SUBMODULE(virtual_functions, m) {
    // test_override
    py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
        .def(py::init<int>())
        /* Reference original class in function definitions */
        .def("run", &ExampleVirt::run)
        .def("run_bool", &ExampleVirt::run_bool)
        .def("pure_virtual", &ExampleVirt::pure_virtual);

243
    py::class_<NonCopyable>(m, "NonCopyable").def(py::init<int, int>());
244

245
    py::class_<Movable>(m, "Movable").def(py::init<int, int>());
246
247

    // test_move_support
248
#if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
249
250
251
252
253
254
255
256
257
    py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
        .def(py::init<>())
        .def("get_noncopyable", &NCVirt::get_noncopyable)
        .def("get_movable", &NCVirt::get_movable)
        .def("print_nc", &NCVirt::print_nc)
        .def("print_movable", &NCVirt::print_movable);
#endif

    m.def("runExampleVirt", [](ExampleVirt *ex, int value) { return ex->run(value); });
258
    m.def("runExampleVirtBool", [](ExampleVirt *ex) { return ex->run_bool(); });
259
260
261
262
263
264
265
266
267
    m.def("runExampleVirtVirtual", [](ExampleVirt *ex) { ex->pure_virtual(); });

    m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
    initialize_inherited_virtuals(m);

    // test_alias_delay_initialization1
    // don't invoke Python dispatch classes by default when instantiating C++ classes
    // that were not extended on the Python side
    struct A {
Henry Schreiner's avatar
Henry Schreiner committed
268
        A() = default;
269
        A(const A &) = delete;
270
        virtual ~A() = default;
271
272
273
274
275
        virtual void f() { py::print("A.f()"); }
    };

    struct PyA : A {
        PyA() { py::print("PyA.PyA()"); }
276
        PyA(const PyA &) = delete;
277
        ~PyA() override { py::print("PyA.~PyA()"); }
278
279
280

        void f() override {
            py::print("PyA.f()");
281
282
            // This convolution just gives a `void`, but tests that PYBIND11_TYPE() works to
            // protect a type containing a ,
283
            PYBIND11_OVERRIDE(PYBIND11_TYPE(typename std::enable_if<true, void>::type), A, f);
284
285
286
        }
    };

287
    py::class_<A, PyA>(m, "A").def(py::init<>()).def("f", &A::f);
288
289
290
291
292
293

    m.def("call_f", [](A *a) { a->f(); });

    // test_alias_delay_initialization2
    // ... unless we explicitly request it, as in this example:
    struct A2 {
Henry Schreiner's avatar
Henry Schreiner committed
294
        A2() = default;
295
        A2(const A2 &) = delete;
296
        virtual ~A2() = default;
297
298
299
300
301
        virtual void f() { py::print("A2.f()"); }
    };

    struct PyA2 : A2 {
        PyA2() { py::print("PyA2.PyA2()"); }
302
        PyA2(const PyA2 &) = delete;
303
        ~PyA2() override { py::print("PyA2.~PyA2()"); }
304
305
        void f() override {
            py::print("PyA2.f()");
306
            PYBIND11_OVERRIDE(void, A2, f);
307
308
309
310
311
        }
    };

    py::class_<A2, PyA2>(m, "A2")
        .def(py::init_alias<>())
312
        .def(py::init([](int) { return new PyA2(); }))
313
314
315
316
317
318
319
320
321
322
        .def("f", &A2::f);

    m.def("call_f", [](A2 *a2) { a2->f(); });

    // test_dispatch_issue
    // #159: virtual function dispatch has problems with similar-named functions
    py::class_<Base, DispatchIssue>(m, "DispatchIssue")
        .def(py::init<>())
        .def("dispatch", &Base::dispatch);

323
    m.def("dispatch_issue_go", [](const Base *b) { return b->dispatch(); });
324

325
326
327
328
329
330
    // test_recursive_dispatch_issue
    // #3357: Recursive dispatch fails to find python function override
    pybind11::class_<AdderBase, Adder>(m, "Adder")
        .def(pybind11::init<>())
        .def("__call__", &AdderBase::operator());

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    pybind11::class_<AdderBase::Data>(m, "Data").def(pybind11::init<>());

    m.def("add2",
          [](const AdderBase::Data &first,
             const AdderBase::Data &second,
             const AdderBase &adder,
             const AdderBase::DataVisitor &visitor) { adder(first, second, visitor); });

    m.def("add3",
          [](const AdderBase::Data &first,
             const AdderBase::Data &second,
             const AdderBase::Data &third,
             const AdderBase &adder,
             const AdderBase::DataVisitor &visitor) {
              adder(first, second, [&](const AdderBase::Data &first_plus_second) {
346
347
                  // NOLINTNEXTLINE(readability-suspicious-call-argument)
                  adder(first_plus_second, third, visitor);
348
349
              });
          });
350

351
    // test_override_ref
Unknown's avatar
Unknown committed
352
    // #392/397: overriding reference-returning functions
353
354
    class OverrideTest {
    public:
355
356
357
        struct A {
            std::string value = "hi";
        };
358
359
360
        std::string v;
        A a;
        explicit OverrideTest(const std::string &v) : v{v} {}
Henry Schreiner's avatar
Henry Schreiner committed
361
        OverrideTest() = default;
362
        OverrideTest(const OverrideTest &) = delete;
363
364
365
366
        virtual std::string str_value() { return v; }
        virtual std::string &str_ref() { return v; }
        virtual A A_value() { return a; }
        virtual A &A_ref() { return a; }
367
        virtual ~OverrideTest() = default;
368
    };
369

370
371
372
    class PyOverrideTest : public OverrideTest {
    public:
        using OverrideTest::OverrideTest;
373
374
375
        std::string str_value() override {
            PYBIND11_OVERRIDE(std::string, OverrideTest, str_value);
        }
376
377
378
379
        // Not allowed (enabling the below should hit a static_assert failure): we can't get a
        // reference to a python numeric value, since we only copy values in the numeric type
        // caster:
#ifdef PYBIND11_NEVER_DEFINED_EVER
380
381
382
        std::string &str_ref() override {
            PYBIND11_OVERRIDE(std::string &, OverrideTest, str_ref);
        }
383
#endif
384
385
386
        // But we can work around it like this:
    private:
        std::string _tmp;
387
        std::string str_ref_helper() { PYBIND11_OVERRIDE(std::string, OverrideTest, str_ref); }
388

389
390
391
    public:
        std::string &str_ref() override { return _tmp = str_ref_helper(); }

392
393
        A A_value() override { PYBIND11_OVERRIDE(A, OverrideTest, A_value); }
        A &A_ref() override { PYBIND11_OVERRIDE(A &, OverrideTest, A_ref); }
394
395
396
397
398
399
400
    };

    py::class_<OverrideTest::A>(m, "OverrideTest_A")
        .def_readwrite("value", &OverrideTest::A::value);
    py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest")
        .def(py::init<const std::string &>())
        .def("str_value", &OverrideTest::str_value)
401
402
403
#ifdef PYBIND11_NEVER_DEFINED_EVER
        .def("str_ref", &OverrideTest::str_ref)
#endif
404
405
        .def("A_value", &OverrideTest::A_value)
        .def("A_ref", &OverrideTest::A_ref);
406

407
408
409
    py::class_<test_override_cache_helper,
               test_override_cache_helper_trampoline,
               std::shared_ptr<test_override_cache_helper>>(m, "test_override_cache_helper")
410
411
412
413
        .def(py::init_alias<>())
        .def("func", &test_override_cache_helper::func);

    m.def("test_override_cache", test_override_cache);
414
415
}

416
417
// Inheriting virtual methods.  We do two versions here: the repeat-everything version and the
// templated trampoline versions mentioned in docs/advanced.rst.
418
//
419
420
421
// These base classes are exactly the same, but we technically need distinct
// classes for this example code because we need to be able to bind them
// properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to
422
423
// multiple python classes).
class A_Repeat {
424
425
426
427
428
429
430
431
432
433
434
#define A_METHODS                                                                                 \
public:                                                                                           \
    virtual int unlucky_number() = 0;                                                             \
    virtual std::string say_something(unsigned times) {                                           \
        std::string s = "";                                                                       \
        for (unsigned i = 0; i < times; ++i)                                                      \
            s += "hi";                                                                            \
        return s;                                                                                 \
    }                                                                                             \
    std::string say_everything() {                                                                \
        return say_something(1) + " " + std::to_string(unlucky_number());                         \
435
    }
436
    A_METHODS
Henry Schreiner's avatar
Henry Schreiner committed
437
    A_Repeat() = default;
438
    A_Repeat(const A_Repeat &) = delete;
439
    virtual ~A_Repeat() = default;
440
441
};
class B_Repeat : public A_Repeat {
442
443
444
445
446
447
#define B_METHODS                                                                                 \
public:                                                                                           \
    int unlucky_number() override { return 13; }                                                  \
    std::string say_something(unsigned times) override {                                          \
        return "B says hi " + std::to_string(times) + " times";                                   \
    }                                                                                             \
448
    virtual double lucky_number() { return 7.0; }
449
    B_METHODS
450
451
};
class C_Repeat : public B_Repeat {
452
453
454
#define C_METHODS                                                                                 \
public:                                                                                           \
    int unlucky_number() override { return 4444; }                                                \
455
    double lucky_number() override { return 888; }
456
    C_METHODS
457
458
459
};
class D_Repeat : public C_Repeat {
#define D_METHODS // Nothing overridden.
460
    D_METHODS
461
462
463
};

// Base classes for templated inheritance trampolines.  Identical to the repeat-everything version:
Henry Schreiner's avatar
Henry Schreiner committed
464
465
466
class A_Tpl {
    A_METHODS;
    A_Tpl() = default;
467
    A_Tpl(const A_Tpl &) = delete;
Henry Schreiner's avatar
Henry Schreiner committed
468
469
    virtual ~A_Tpl() = default;
};
470
471
472
473
474
475
476
477
478
class B_Tpl : public A_Tpl {
    B_METHODS
};
class C_Tpl : public B_Tpl {
    C_METHODS
};
class D_Tpl : public C_Tpl {
    D_METHODS
};
479
480
481
482
483

// Inheritance approach 1: each trampoline gets every virtual method (11 in total)
class PyA_Repeat : public A_Repeat {
public:
    using A_Repeat::A_Repeat;
484
    int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, A_Repeat, unlucky_number, ); }
485
486
487
    std::string say_something(unsigned times) override {
        PYBIND11_OVERRIDE(std::string, A_Repeat, say_something, times);
    }
488
489
490
491
};
class PyB_Repeat : public B_Repeat {
public:
    using B_Repeat::B_Repeat;
492
    int unlucky_number() override { PYBIND11_OVERRIDE(int, B_Repeat, unlucky_number, ); }
493
494
495
    std::string say_something(unsigned times) override {
        PYBIND11_OVERRIDE(std::string, B_Repeat, say_something, times);
    }
496
    double lucky_number() override { PYBIND11_OVERRIDE(double, B_Repeat, lucky_number, ); }
497
498
499
500
};
class PyC_Repeat : public C_Repeat {
public:
    using C_Repeat::C_Repeat;
501
    int unlucky_number() override { PYBIND11_OVERRIDE(int, C_Repeat, unlucky_number, ); }
502
503
504
    std::string say_something(unsigned times) override {
        PYBIND11_OVERRIDE(std::string, C_Repeat, say_something, times);
    }
505
    double lucky_number() override { PYBIND11_OVERRIDE(double, C_Repeat, lucky_number, ); }
506
507
508
509
};
class PyD_Repeat : public D_Repeat {
public:
    using D_Repeat::D_Repeat;
510
    int unlucky_number() override { PYBIND11_OVERRIDE(int, D_Repeat, unlucky_number, ); }
511
512
513
    std::string say_something(unsigned times) override {
        PYBIND11_OVERRIDE(std::string, D_Repeat, say_something, times);
    }
514
    double lucky_number() override { PYBIND11_OVERRIDE(double, D_Repeat, lucky_number, ); }
515
516
517
518
519
};

// Inheritance approach 2: templated trampoline classes.
//
// Advantages:
520
521
522
523
524
// - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one
//   for any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes
//   and 11 methods (repeat).
// - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and
//   can properly inherit constructors.
525
526
527
528
529
530
531
532
533
534
//
// Disadvantage:
// - the compiler must still generate and compile 14 different methods (more, even, than the 11
//   required for the repeat approach) instead of the 6 required for MI.  (If there was no pure
//   method (or no pure method override), the number would drop down to the same 11 as the repeat
//   approach).
template <class Base = A_Tpl>
class PyA_Tpl : public Base {
public:
    using Base::Base; // Inherit constructors
535
    int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, Base, unlucky_number, ); }
536
537
538
    std::string say_something(unsigned times) override {
        PYBIND11_OVERRIDE(std::string, Base, say_something, times);
    }
539
540
541
542
543
};
template <class Base = B_Tpl>
class PyB_Tpl : public PyA_Tpl<Base> {
public:
    using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors)
544
    // NOLINTNEXTLINE(bugprone-parent-virtual-call)
545
546
    int unlucky_number() override { PYBIND11_OVERRIDE(int, Base, unlucky_number, ); }
    double lucky_number() override { PYBIND11_OVERRIDE(double, Base, lucky_number, ); }
547
};
548
549
// Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these
// (we can use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead):
550
551
552
553
554
555
556
557
558
559
560
/*
template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
public:
    using PyB_Tpl<Base>::PyB_Tpl;
};
template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
public:
    using PyC_Tpl<Base>::PyC_Tpl;
};
*/

561
void initialize_inherited_virtuals(py::module_ &m) {
562
563
    // test_inherited_virtuals

564
    // Method 1: repeat
565
    py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat")
566
567
        .def(py::init<>())
        .def("unlucky_number", &A_Repeat::unlucky_number)
568
569
        .def("say_something", &A_Repeat::say_something)
        .def("say_everything", &A_Repeat::say_everything);
570
    py::class_<B_Repeat, A_Repeat, PyB_Repeat>(m, "B_Repeat")
571
572
        .def(py::init<>())
        .def("lucky_number", &B_Repeat::lucky_number);
573
574
    py::class_<C_Repeat, B_Repeat, PyC_Repeat>(m, "C_Repeat").def(py::init<>());
    py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat").def(py::init<>());
575

576
    // test_
577
    // Method 2: Templated trampolines
578
    py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl")
579
580
        .def(py::init<>())
        .def("unlucky_number", &A_Tpl::unlucky_number)
581
582
        .def("say_something", &A_Tpl::say_something)
        .def("say_everything", &A_Tpl::say_everything);
583
    py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(m, "B_Tpl")
584
585
        .def(py::init<>())
        .def("lucky_number", &B_Tpl::lucky_number);
586
587
    py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(m, "C_Tpl").def(py::init<>());
    py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(m, "D_Tpl").def(py::init<>());
588
589
590
591

    // Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7)
    m.def("test_gil", &test_gil);
    m.def("test_gil_from_thread", &test_gil_from_thread);
592
};