pybind11_tests.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. tests/pybind11_tests.cpp -- pybind example plugin
  3. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. #include <functional>
  10. #include <list>
  11. /*
  12. For testing purposes, we define a static global variable here in a function that each individual
  13. test .cpp calls with its initialization lambda. It's convenient here because we can just not
  14. compile some test files to disable/ignore some of the test code.
  15. It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
  16. be essentially random, which is okay for our test scripts (there are no dependencies between the
  17. individual pybind11 test .cpp files), but most likely not what you want when using pybind11
  18. productively.
  19. Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
  20. section of the documentation for good practice on splitting binding code over multiple files.
  21. */
  22. std::list<std::function<void(py::module &)>> &initializers() {
  23. static std::list<std::function<void(py::module &)>> inits;
  24. return inits;
  25. }
  26. test_initializer::test_initializer(Initializer init) {
  27. initializers().push_back(init);
  28. }
  29. test_initializer::test_initializer(const char *submodule_name, Initializer init) {
  30. initializers().push_back([=](py::module &parent) {
  31. auto m = parent.def_submodule(submodule_name);
  32. init(m);
  33. });
  34. }
  35. void bind_ConstructorStats(py::module &m) {
  36. py::class_<ConstructorStats>(m, "ConstructorStats")
  37. .def("alive", &ConstructorStats::alive)
  38. .def("values", &ConstructorStats::values)
  39. .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
  40. .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
  41. .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
  42. .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
  43. .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
  44. .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal)
  45. // Not exactly ConstructorStats, but related: expose the internal pybind number of registered instances
  46. // to allow instance cleanup checks (invokes a GC first)
  47. .def_static("detail_reg_inst", []() {
  48. ConstructorStats::gc();
  49. return py::detail::get_internals().registered_instances.size();
  50. })
  51. ;
  52. }
  53. PYBIND11_MODULE(pybind11_tests, m) {
  54. m.doc() = "pybind11 test module";
  55. bind_ConstructorStats(m);
  56. #if !defined(NDEBUG)
  57. m.attr("debug_enabled") = true;
  58. #else
  59. m.attr("debug_enabled") = false;
  60. #endif
  61. py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
  62. .def(py::init<>())
  63. .def(py::init<int>())
  64. .def("get_value", &UserType::value, "Get value using a method")
  65. .def("set_value", &UserType::set, "Set value using a method")
  66. .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
  67. .def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
  68. py::class_<IncType, UserType>(m, "IncType")
  69. .def(py::init<>())
  70. .def(py::init<int>())
  71. .def("__repr__", [](const IncType& u) { return "IncType({})"_s.format(u.value()); });
  72. for (const auto &initializer : initializers())
  73. initializer(m);
  74. if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
  75. }