pybind11_tests.cpp 4.41 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/pybind11_tests.cpp -- pybind example plugin
Wenzel Jakob's avatar
Wenzel Jakob committed
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
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.
*/

Dean Moldovan's avatar
Dean Moldovan committed
10
#include "pybind11_tests.h"
11

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

14
15
16
#include <functional>
#include <list>

17
18
19
20
21
22
23
24
25
26
27
28
29
/*
For testing purposes, we define a static global variable here in a function that each individual
test .cpp calls with its initialization lambda.  It's convenient here because we can just not
compile some test files to disable/ignore some of the test code.

It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
be essentially random, which is okay for our test scripts (there are no dependencies between the
individual pybind11 test .cpp files), but most likely not what you want when using pybind11
productively.

Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
section of the documentation for good practice on splitting binding code over multiple files.
*/
30
31
std::list<std::function<void(py::module_ &)>> &initializers() {
    static std::list<std::function<void(py::module_ &)>> inits;
32
33
    return inits;
}
Wenzel Jakob's avatar
Wenzel Jakob committed
34

35
test_initializer::test_initializer(Initializer init) { initializers().emplace_back(init); }
36
37

test_initializer::test_initializer(const char *submodule_name, Initializer init) {
38
    initializers().emplace_back([=](py::module_ &parent) {
39
40
41
        auto m = parent.def_submodule(submodule_name);
        init(m);
    });
42
}
43

44
void bind_ConstructorStats(py::module_ &m) {
45
46
47
48
49
50
51
52
    py::class_<ConstructorStats>(m, "ConstructorStats")
        .def("alive", &ConstructorStats::alive)
        .def("values", &ConstructorStats::values)
        .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
        .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
        .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
        .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
        .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
53
54
55
        .def_static("get",
                    (ConstructorStats & (*) (py::object)) & ConstructorStats::get,
                    py::return_value_policy::reference_internal)
56

57
58
        // Not exactly ConstructorStats, but related: expose the internal pybind number of
        // registered instances to allow instance cleanup checks (invokes a GC first)
59
60
61
        .def_static("detail_reg_inst", []() {
            ConstructorStats::gc();
            return py::detail::get_internals().registered_instances.size();
62
        });
63
64
}

65
66
67
68
69
70
71
72
73
74
75
76
77
const char *cpp_std() {
    return
#if defined(PYBIND11_CPP20)
        "C++20";
#elif defined(PYBIND11_CPP17)
        "C++17";
#elif defined(PYBIND11_CPP14)
        "C++14";
#else
        "C++11";
#endif
}

78
79
PYBIND11_MODULE(pybind11_tests, m) {
    m.doc() = "pybind11 test module";
Wenzel Jakob's avatar
Wenzel Jakob committed
80

81
82
83
84
85
86
87
88
89
90
91
    // Intentionally kept minimal to not create a maintenance chore
    // ("just enough" to be conclusive).
#if defined(_MSC_FULL_VER)
    m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER);
#elif defined(__VERSION__)
    m.attr("compiler_info") = __VERSION__;
#else
    m.attr("compiler_info") = py::none();
#endif
    m.attr("cpp_std") = cpp_std();
    m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
92
93
94
95
96
97
    m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
        true;
#else
        false;
#endif
98

99
100
    bind_ConstructorStats(m);

101
102
#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
    m.attr("detailed_error_messages_enabled") = true;
103
#else
104
    m.attr("detailed_error_messages_enabled") = false;
105
106
107
108
109
110
#endif

    py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
        .def(py::init<>())
        .def(py::init<int>())
        .def("get_value", &UserType::value, "Get value using a method")
111
112
        .def("set_value", &UserType::set, "Set value using a method")
        .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
113
        .def("__repr__", [](const UserType &u) { return "UserType({})"_s.format(u.value()); });
114
115
116
117

    py::class_<IncType, UserType>(m, "IncType")
        .def(py::init<>())
        .def(py::init<int>())
118
        .def("__repr__", [](const IncType &u) { return "IncType({})"_s.format(u.value()); });
119

120
    for (const auto &initializer : initializers()) {
121
        initializer(m);
122
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
123
}