test_modules.cpp 4.02 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/test_modules.cpp -- nested modules, importing modules, and
Wenzel Jakob's avatar
Wenzel Jakob committed
3
                            internal references
Wenzel Jakob's avatar
Wenzel Jakob committed
4

5
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
6
7
8
9
10

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

Dean Moldovan's avatar
Dean Moldovan committed
11
#include "constructor_stats.h"
12
#include "pybind11_tests.h"
Wenzel Jakob's avatar
Wenzel Jakob committed
13

14
15
TEST_SUBMODULE(modules, m) {
    // test_nested_modules
16
    // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
17
18
19
20
21
22
    py::module m_sub = m.def_submodule("subsubmodule");
    m_sub.def("submodule_func", []() { return "submodule_func()"; });

    // test_reference_internal
    class A {
    public:
23
        explicit A(int v) : v(v) { print_created(this, v); }
24
        ~A() { print_destroyed(this); }
25
26
27
28
29
30
        A(const A &) { print_copy_created(this); }
        A &operator=(const A &copy) {
            print_copy_assigned(this);
            v = copy.v;
            return *this;
        }
31
32
        std::string toString() const { return "A[" + std::to_string(v) + "]"; }

33
34
35
    private:
        int v;
    };
36
    py::class_<A>(m_sub, "A").def(py::init<int>()).def("__repr__", &A::toString);
Wenzel Jakob's avatar
Wenzel Jakob committed
37

38
39
40
41
    class B {
    public:
        B() { print_default_created(this); }
        ~B() { print_destroyed(this); }
42
43
44
45
46
47
48
        B(const B &) { print_copy_created(this); }
        B &operator=(const B &copy) {
            print_copy_assigned(this);
            a1 = copy.a1;
            a2 = copy.a2;
            return *this;
        }
49
50
51
52
53
54
        A &get_a1() { return a1; }
        A &get_a2() { return a2; }

        A a1{1};
        A a2{2};
    };
Wenzel Jakob's avatar
Wenzel Jakob committed
55
56
    py::class_<B>(m_sub, "B")
        .def(py::init<>())
57
58
59
60
61
62
63
64
65
66
        .def("get_a1",
             &B::get_a1,
             "Return the internal A 1",
             py::return_value_policy::reference_internal)
        .def("get_a2",
             &B::get_a2,
             "Return the internal A 2",
             py::return_value_policy::reference_internal)
        .def_readwrite("a1", &B::a1) // def_readonly uses an internal
                                     // reference return policy by default
Wenzel Jakob's avatar
Wenzel Jakob committed
67
        .def_readwrite("a2", &B::a2);
Wenzel Jakob's avatar
Wenzel Jakob committed
68

69
    // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
Wenzel Jakob's avatar
Wenzel Jakob committed
70
    m.attr("OD") = py::module::import("collections").attr("OrderedDict");
71

72
    // test_duplicate_registration
73
74
    // Registering two things with the same name
    m.def("duplicate_registration", []() {
75
76
77
78
        class Dupe1 {};
        class Dupe2 {};
        class Dupe3 {};
        class DupeException {};
79

80
        // Go ahead and leak, until we have a non-leaking py::module_ constructor
81
82
        auto dm
            = py::module_::create_extension_module("dummy", nullptr, new py::module_::module_def);
83
84
85
86
87
88
89
90
91
92
        auto failures = py::list();

        py::class_<Dupe1>(dm, "Dupe1");
        py::class_<Dupe2>(dm, "Dupe2");
        dm.def("dupe1_factory", []() { return Dupe1(); });
        py::exception<DupeException>(dm, "DupeException");

        try {
            py::class_<Dupe1>(dm, "Dupe1");
            failures.append("Dupe1 class");
93
94
        } catch (std::runtime_error &) {
        }
95
96
97
        try {
            dm.def("Dupe1", []() { return Dupe1(); });
            failures.append("Dupe1 function");
98
99
        } catch (std::runtime_error &) {
        }
100
101
102
        try {
            py::class_<Dupe3>(dm, "dupe1_factory");
            failures.append("dupe1_factory");
103
104
        } catch (std::runtime_error &) {
        }
105
106
107
        try {
            py::exception<Dupe3>(dm, "Dupe2");
            failures.append("Dupe2");
108
109
        } catch (std::runtime_error &) {
        }
110
111
112
        try {
            dm.def("DupeException", []() { return 30; });
            failures.append("DupeException1");
113
114
        } catch (std::runtime_error &) {
        }
115
116
117
        try {
            py::class_<DupeException>(dm, "DupeException");
            failures.append("DupeException2");
118
119
        } catch (std::runtime_error &) {
        }
120
121
122

        return failures;
    });
123
124

    m.def("def_submodule", [](py::module_ m, const char *name) { return m.def_submodule(name); });
125
}