2015-07-05 18:05:44 +00:00
|
|
|
/*
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
example/example-python-types.cpp2 -- singleton design pattern, static functions and
|
2015-07-29 15:51:54 +00:00
|
|
|
variables, passing and interacting with Python types
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "example.h"
|
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
|
|
|
#include "constructor-stats.h"
|
2015-10-15 16:13:33 +00:00
|
|
|
#include <pybind11/stl.h>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-03-31 12:22:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <io.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
class ExamplePythonTypes {
|
2015-07-05 18:05:44 +00:00
|
|
|
public:
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
static ExamplePythonTypes *new_instance() {
|
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
|
|
|
auto *ptr = new ExamplePythonTypes();
|
|
|
|
print_created(ptr, "via new_instance");
|
|
|
|
return ptr;
|
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
|
|
|
~ExamplePythonTypes() { print_destroyed(this); }
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
/* Create and return a Python dictionary */
|
|
|
|
py::dict get_dict() {
|
|
|
|
py::dict dict;
|
|
|
|
dict[py::str("key")] = py::str("value");
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:04:49 +00:00
|
|
|
/* Create and return a Python set */
|
|
|
|
py::set get_set() {
|
|
|
|
py::set set;
|
2015-11-15 12:03:07 +00:00
|
|
|
set.add(py::str("key1"));
|
|
|
|
set.add(py::str("key2"));
|
2015-11-14 18:04:49 +00:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Create and return a C++ dictionary */
|
|
|
|
std::map<std::string, std::string> get_dict_2() {
|
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
result["key"] = "value";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:04:49 +00:00
|
|
|
/* Create and return a C++ set */
|
|
|
|
std::set<std::string> get_set_2() {
|
|
|
|
std::set<std::string> result;
|
|
|
|
result.insert("key1");
|
|
|
|
result.insert("key2");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Create, manipulate, and return a Python list */
|
|
|
|
py::list get_list() {
|
|
|
|
py::list list;
|
|
|
|
list.append(py::str("value"));
|
|
|
|
cout << "Entry at positon 0: " << py::object(list[0]) << endl;
|
|
|
|
list[0] = py::str("overwritten");
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* C++ STL data types are automatically casted */
|
2016-03-08 18:40:32 +00:00
|
|
|
std::vector<std::wstring> get_list_2() {
|
|
|
|
std::vector<std::wstring> list;
|
|
|
|
list.push_back(L"value");
|
2015-07-05 18:05:44 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2016-02-07 15:36:26 +00:00
|
|
|
/* C++ STL data types are automatically casted */
|
|
|
|
std::array<std::string, 2> get_array() {
|
|
|
|
return std::array<std::string, 2> {{ "array entry 1" , "array entry 2"}};
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Easily iterate over a dictionary using a C++11 range-based for loop */
|
|
|
|
void print_dict(py::dict dict) {
|
|
|
|
for (auto item : dict)
|
|
|
|
std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:36:37 +00:00
|
|
|
/* Easily iterate over a set using a C++11 range-based for loop */
|
2015-11-14 18:04:49 +00:00
|
|
|
void print_set(py::set set) {
|
|
|
|
for (auto item : set)
|
|
|
|
std::cout << "key: " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Easily iterate over a list using a C++11 range-based for loop */
|
|
|
|
void print_list(py::list list) {
|
|
|
|
int index = 0;
|
|
|
|
for (auto item : list)
|
|
|
|
std::cout << "list item " << index++ << ": " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:36:37 +00:00
|
|
|
/* STL data types (such as maps) are automatically casted from Python */
|
|
|
|
void print_dict_2(const std::map<std::string, std::string> &dict) {
|
|
|
|
for (auto item : dict)
|
|
|
|
std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STL data types (such as sets) are automatically casted from Python */
|
|
|
|
void print_set_2(const std::set<std::string> &set) {
|
|
|
|
for (auto item : set)
|
|
|
|
std::cout << "key: " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STL data types (such as vectors) are automatically casted from Python */
|
2016-03-08 18:40:32 +00:00
|
|
|
void print_list_2(std::vector<std::wstring> &list) {
|
2016-03-31 12:22:37 +00:00
|
|
|
#ifdef _WIN32 /* Can't easily mix cout and wcout on Windows */
|
|
|
|
_setmode(_fileno(stdout), _O_TEXT);
|
|
|
|
#endif
|
2015-07-05 18:05:44 +00:00
|
|
|
int index = 0;
|
|
|
|
for (auto item : list)
|
2016-03-08 18:40:32 +00:00
|
|
|
std::wcout << L"list item " << index++ << L": " << item << std::endl;
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pybind automatically translates between C++11 and Python tuples */
|
|
|
|
std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) {
|
|
|
|
return std::make_pair(input.second, input.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pybind automatically translates between C++11 and Python tuples */
|
|
|
|
std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) {
|
|
|
|
return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
|
|
|
|
}
|
|
|
|
|
2016-02-07 15:36:26 +00:00
|
|
|
/* STL data types (such as arrays) are automatically casted from Python */
|
|
|
|
void print_array(std::array<std::string, 2> &array) {
|
|
|
|
int index = 0;
|
|
|
|
for (auto item : array)
|
|
|
|
std::cout << "array item " << index++ << ": " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
void throw_exception() {
|
|
|
|
throw std::runtime_error("This exception was intentionally thrown.");
|
|
|
|
}
|
|
|
|
|
2016-08-13 11:38:11 +00:00
|
|
|
py::bytes get_bytes_from_string() {
|
2016-08-13 17:52:39 +00:00
|
|
|
return (py::bytes) std::string("foo");
|
2016-08-13 11:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
py::bytes get_bytes_from_str() {
|
2016-08-13 17:52:39 +00:00
|
|
|
return (py::bytes) py::str(std::string("bar"));
|
2016-08-13 11:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
py::str get_str_from_string() {
|
2016-08-13 17:52:39 +00:00
|
|
|
return (py::str) std::string("baz");
|
2016-08-13 11:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
py::str get_str_from_bytes() {
|
2016-08-13 17:52:39 +00:00
|
|
|
return (py::str) py::bytes(std::string("boo"));
|
2016-08-13 11:38:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
static int value;
|
|
|
|
static const int value2;
|
|
|
|
};
|
|
|
|
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
int ExamplePythonTypes::value = 0;
|
|
|
|
const int ExamplePythonTypes::value2 = 5;
|
2015-07-05 18:05:44 +00:00
|
|
|
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
void init_ex_python_types(py::module &m) {
|
2015-07-05 18:05:44 +00:00
|
|
|
/* No constructor is explicitly defined below. An exception is raised when
|
|
|
|
trying to construct it directly from Python */
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
py::class_<ExamplePythonTypes>(m, "ExamplePythonTypes", "Example 2 documentation")
|
|
|
|
.def("get_dict", &ExamplePythonTypes::get_dict, "Return a Python dictionary")
|
|
|
|
.def("get_dict_2", &ExamplePythonTypes::get_dict_2, "Return a C++ dictionary")
|
|
|
|
.def("get_list", &ExamplePythonTypes::get_list, "Return a Python list")
|
|
|
|
.def("get_list_2", &ExamplePythonTypes::get_list_2, "Return a C++ list")
|
|
|
|
.def("get_set", &ExamplePythonTypes::get_set, "Return a Python set")
|
|
|
|
.def("get_set2", &ExamplePythonTypes::get_set_2, "Return a C++ set")
|
|
|
|
.def("get_array", &ExamplePythonTypes::get_array, "Return a C++ array")
|
|
|
|
.def("print_dict", &ExamplePythonTypes::print_dict, "Print entries of a Python dictionary")
|
|
|
|
.def("print_dict_2", &ExamplePythonTypes::print_dict_2, "Print entries of a C++ dictionary")
|
|
|
|
.def("print_set", &ExamplePythonTypes::print_set, "Print entries of a Python set")
|
|
|
|
.def("print_set_2", &ExamplePythonTypes::print_set_2, "Print entries of a C++ set")
|
|
|
|
.def("print_list", &ExamplePythonTypes::print_list, "Print entries of a Python list")
|
|
|
|
.def("print_list_2", &ExamplePythonTypes::print_list_2, "Print entries of a C++ list")
|
|
|
|
.def("print_array", &ExamplePythonTypes::print_array, "Print entries of a C++ array")
|
|
|
|
.def("pair_passthrough", &ExamplePythonTypes::pair_passthrough, "Return a pair in reversed order")
|
|
|
|
.def("tuple_passthrough", &ExamplePythonTypes::tuple_passthrough, "Return a triple in reversed order")
|
|
|
|
.def("throw_exception", &ExamplePythonTypes::throw_exception, "Throw an exception")
|
2016-08-13 11:38:11 +00:00
|
|
|
.def("get_bytes_from_string", &ExamplePythonTypes::get_bytes_from_string, "py::bytes from std::string")
|
|
|
|
.def("get_bytes_from_str", &ExamplePythonTypes::get_bytes_from_str, "py::bytes from py::str")
|
|
|
|
.def("get_str_from_string", &ExamplePythonTypes::get_str_from_string, "py::str from std::string")
|
|
|
|
.def("get_str_from_bytes", &ExamplePythonTypes::get_str_from_bytes, "py::str from py::bytes")
|
Rename examples files, as per #288
This renames example files from `exampleN` to `example-description`.
Specifically, the following renaming is applied:
example1 -> example-methods-and-attributes
example2 -> example-python-types
example3 -> example-operator-overloading
example4 -> example-constants-and-functions
example5 -> example-callbacks (*)
example6 -> example-sequence-and-iterators
example7 -> example-buffers
example8 -> example-custom-ref-counting
example9 -> example-modules
example10 -> example-numpy-vectorize
example11 -> example-arg-keywords-and-defaults
example12 -> example-virtual-functions
example13 -> example-keep-alive
example14 -> example-opaque-types
example15 -> example-pickling
example16 -> example-inheritance
example17 -> example-stl-binders
example18 -> example-eval
example19 -> example-custom-exceptions
* the inheritance parts of example5 are moved into example-inheritance
(previously example16), and the remainder is left as example-callbacks.
This commit also renames the internal variables ("Example1",
"Example2", "Example4", etc.) into non-numeric names ("ExampleMandA",
"ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the
file renaming.
The order of tests is preserved, but this can easily be changed if
there is some more natural ordering by updating the list in
examples/CMakeLists.txt.
2016-07-18 20:43:18 +00:00
|
|
|
.def_static("new_instance", &ExamplePythonTypes::new_instance, "Return an instance")
|
|
|
|
.def_readwrite_static("value", &ExamplePythonTypes::value, "Static value member")
|
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
|
|
|
.def_readonly_static("value2", &ExamplePythonTypes::value2, "Static value member (readonly)")
|
|
|
|
;
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|