github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/tests/pybind11_tests.cpp (about) 1 /* 2 tests/pybind11_tests.cpp -- pybind example plugin 3 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 10 #include "pybind11_tests.h" 11 12 #include "constructor_stats.h" 13 14 #include <functional> 15 #include <list> 16 17 /* 18 For testing purposes, we define a static global variable here in a function that each individual 19 test .cpp calls with its initialization lambda. It's convenient here because we can just not 20 compile some test files to disable/ignore some of the test code. 21 22 It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will 23 be essentially random, which is okay for our test scripts (there are no dependencies between the 24 individual pybind11 test .cpp files), but most likely not what you want when using pybind11 25 productively. 26 27 Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions" 28 section of the documentation for good practice on splitting binding code over multiple files. 29 */ 30 std::list<std::function<void(py::module_ &)>> &initializers() { 31 static std::list<std::function<void(py::module_ &)>> inits; 32 return inits; 33 } 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 auto m = parent.def_submodule(submodule_name); 40 init(m); 41 }); 42 } 43 44 void bind_ConstructorStats(py::module_ &m) { 45 py::class_<ConstructorStats>(m, "ConstructorStats") 46 .def("alive", &ConstructorStats::alive) 47 .def("values", &ConstructorStats::values) 48 .def_readwrite("default_constructions", &ConstructorStats::default_constructions) 49 .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments) 50 .def_readwrite("move_assignments", &ConstructorStats::move_assignments) 51 .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions) 52 .def_readwrite("move_constructions", &ConstructorStats::move_constructions) 53 .def_static("get", 54 (ConstructorStats & (*) (py::object)) & ConstructorStats::get, 55 py::return_value_policy::reference_internal) 56 57 // Not exactly ConstructorStats, but related: expose the internal pybind number of 58 // registered instances to allow instance cleanup checks (invokes a GC first) 59 .def_static("detail_reg_inst", []() { 60 ConstructorStats::gc(); 61 return py::detail::get_internals().registered_instances.size(); 62 }); 63 } 64 65 const char *cpp_std() { 66 return 67 #if defined(PYBIND11_CPP20) 68 "C++20"; 69 #elif defined(PYBIND11_CPP17) 70 "C++17"; 71 #elif defined(PYBIND11_CPP14) 72 "C++14"; 73 #else 74 "C++11"; 75 #endif 76 } 77 78 PYBIND11_MODULE(pybind11_tests, m) { 79 m.doc() = "pybind11 test module"; 80 81 // Intentionally kept minimal to not create a maintenance chore 82 // ("just enough" to be conclusive). 83 #if defined(_MSC_FULL_VER) 84 m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER); 85 #elif defined(__VERSION__) 86 m.attr("compiler_info") = __VERSION__; 87 #else 88 m.attr("compiler_info") = py::none(); 89 #endif 90 m.attr("cpp_std") = cpp_std(); 91 m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID; 92 m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") = 93 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) 94 true; 95 #else 96 false; 97 #endif 98 99 bind_ConstructorStats(m); 100 101 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) 102 m.attr("detailed_error_messages_enabled") = true; 103 #else 104 m.attr("detailed_error_messages_enabled") = false; 105 #endif 106 107 py::class_<UserType>(m, "UserType", "A `py::class_` type for testing") 108 .def(py::init<>()) 109 .def(py::init<int>()) 110 .def("get_value", &UserType::value, "Get value using a method") 111 .def("set_value", &UserType::set, "Set value using a method") 112 .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 py::class_<IncType, UserType>(m, "IncType") 116 .def(py::init<>()) 117 .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 } 123 }