You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.5 KiB

3 months ago
  1. /*
  2. tests/pybind11_tests.cpp -- pybind example plugin
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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_property_readonly("value", &UserType::value, "Get value using a property")
  66. .def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
  67. py::class_<IncType, UserType>(m, "IncType")
  68. .def(py::init<>())
  69. .def(py::init<int>())
  70. .def("__repr__", [](const IncType& u) { return "IncType({})"_s.format(u.value()); });
  71. for (const auto &initializer : initializers())
  72. initializer(m);
  73. if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
  74. }