Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/pybind11_tests.cpp -- pybind example plugin |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 3 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 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 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | #include "pybind11_tests.h" |
| 11 | #include "constructor_stats.h" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 12 | |
Jason Rhinelander | 0558a9a | 2017-02-01 04:36:29 -0500 | [diff] [blame] | 13 | /* |
| 14 | For testing purposes, we define a static global variable here in a function that each individual |
| 15 | test .cpp calls with its initialization lambda. It's convenient here because we can just not |
| 16 | compile some test files to disable/ignore some of the test code. |
| 17 | |
| 18 | It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will |
| 19 | be essentially random, which is okay for our test scripts (there are no dependencies between the |
| 20 | individual pybind11 test .cpp files), but most likely not what you want when using pybind11 |
| 21 | productively. |
| 22 | |
| 23 | Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions" |
| 24 | section of the documentation for good practice on splitting binding code over multiple files. |
| 25 | */ |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 26 | std::list<std::function<void(py::module &)>> &initializers() { |
| 27 | static std::list<std::function<void(py::module &)>> inits; |
| 28 | return inits; |
| 29 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 30 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 31 | test_initializer::test_initializer(std::function<void(py::module &)> initializer) { |
| 32 | initializers().push_back(std::move(initializer)); |
| 33 | } |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 34 | |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 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) |
Jason Rhinelander | 14e7065 | 2017-04-21 17:14:22 -0400 | [diff] [blame] | 44 | .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal) |
| 45 | |
| 46 | // Not exactly ConstructorStats, but related: expose the internal pybind number of registered instances |
| 47 | // to allow instance cleanup checks (invokes a GC first) |
| 48 | .def_static("detail_reg_inst", []() { |
| 49 | ConstructorStats::gc(); |
| 50 | return py::detail::get_internals().registered_instances.size(); |
| 51 | }) |
| 52 | ; |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 55 | PYBIND11_MODULE(pybind11_tests, m) { |
| 56 | m.doc() = "pybind11 test module"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 57 | |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 58 | bind_ConstructorStats(m); |
| 59 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 60 | for (const auto &initializer : initializers()) |
| 61 | initializer(m); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 62 | |
Jason Rhinelander | 3f1ff3f | 2016-12-12 17:42:52 -0500 | [diff] [blame] | 63 | if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 64 | } |