2015-07-05 18:05:44 +00:00
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
tests/pybind11_tests.cpp -- pybind example plugin
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include "constructor_stats.h"
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
std::list<std::function<void(py::module &)>> &initializers() {
|
|
|
|
static std::list<std::function<void(py::module &)>> inits;
|
|
|
|
return inits;
|
|
|
|
}
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
test_initializer::test_initializer(std::function<void(py::module &)> initializer) {
|
|
|
|
initializers().push_back(std::move(initializer));
|
|
|
|
}
|
2016-05-05 18:33:54 +00:00
|
|
|
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
void bind_ConstructorStats(py::module &m) {
|
|
|
|
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)
|
2016-08-24 23:43:33 +00:00
|
|
|
.def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal);
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
PYBIND11_PLUGIN(pybind11_tests) {
|
|
|
|
py::module m("pybind11_tests", "pybind example plugin");
|
2015-07-05 18:05:44 +00:00
|
|
|
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
bind_ConstructorStats(m);
|
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
for (const auto &initializer : initializers())
|
|
|
|
initializer(m);
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-12-12 22:42:52 +00:00
|
|
|
if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
|
2016-05-05 18:33:54 +00:00
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
return m.ptr();
|
|
|
|
}
|