2015-07-05 18:05:44 +00:00
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
tests/test_modules.cpp -- nested modules, importing modules, and
|
2015-10-13 21:44:25 +00:00
|
|
|
internal references
|
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
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
TEST_SUBMODULE(modules, m) {
|
|
|
|
// test_nested_modules
|
2020-10-03 17:38:03 +00:00
|
|
|
// This is intentionally "py::module" to verify it still can be used in place of "py::module_"
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
py::module m_sub = m.def_submodule("subsubmodule");
|
|
|
|
m_sub.def("submodule_func", []() { return "submodule_func()"; });
|
|
|
|
|
|
|
|
// test_reference_internal
|
|
|
|
class A {
|
|
|
|
public:
|
|
|
|
A(int v) : v(v) { print_created(this, v); }
|
|
|
|
~A() { print_destroyed(this); }
|
|
|
|
A(const A&) { print_copy_created(this); }
|
|
|
|
A& operator=(const A ©) { print_copy_assigned(this); v = copy.v; return *this; }
|
|
|
|
std::string toString() { return "A[" + std::to_string(v) + "]"; }
|
|
|
|
private:
|
|
|
|
int v;
|
|
|
|
};
|
2015-07-05 18:05:44 +00:00
|
|
|
py::class_<A>(m_sub, "A")
|
|
|
|
.def(py::init<int>())
|
|
|
|
.def("__repr__", &A::toString);
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
class B {
|
|
|
|
public:
|
|
|
|
B() { print_default_created(this); }
|
|
|
|
~B() { print_destroyed(this); }
|
|
|
|
B(const B&) { print_copy_created(this); }
|
|
|
|
B& operator=(const B ©) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
|
|
|
|
A &get_a1() { return a1; }
|
|
|
|
A &get_a2() { return a2; }
|
|
|
|
|
|
|
|
A a1{1};
|
|
|
|
A a2{2};
|
|
|
|
};
|
2015-07-05 18:05:44 +00:00
|
|
|
py::class_<B>(m_sub, "B")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
|
|
|
|
.def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
|
|
|
|
.def_readwrite("a1", &B::a1) // def_readonly uses an internal reference return policy by default
|
|
|
|
.def_readwrite("a2", &B::a2);
|
2015-10-13 21:44:25 +00:00
|
|
|
|
2020-10-03 17:38:03 +00:00
|
|
|
// This is intentionally "py::module" to verify it still can be used in place of "py::module_"
|
2015-10-13 21:44:25 +00:00
|
|
|
m.attr("OD") = py::module::import("collections").attr("OrderedDict");
|
2017-06-07 14:52:50 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_duplicate_registration
|
2017-06-07 14:52:50 +00:00
|
|
|
// Registering two things with the same name
|
|
|
|
m.def("duplicate_registration", []() {
|
|
|
|
class Dupe1 { };
|
|
|
|
class Dupe2 { };
|
|
|
|
class Dupe3 { };
|
|
|
|
class DupeException { };
|
|
|
|
|
2020-10-09 14:46:11 +00:00
|
|
|
// Go ahead and leak, until we have a non-leaking py::module_ constructor
|
|
|
|
auto dm = py::module_::create_extension_module("dummy", nullptr, new py::module_::module_def);
|
2017-06-07 14:52:50 +00:00
|
|
|
auto failures = py::list();
|
|
|
|
|
|
|
|
py::class_<Dupe1>(dm, "Dupe1");
|
|
|
|
py::class_<Dupe2>(dm, "Dupe2");
|
|
|
|
dm.def("dupe1_factory", []() { return Dupe1(); });
|
|
|
|
py::exception<DupeException>(dm, "DupeException");
|
|
|
|
|
|
|
|
try {
|
|
|
|
py::class_<Dupe1>(dm, "Dupe1");
|
|
|
|
failures.append("Dupe1 class");
|
|
|
|
} catch (std::runtime_error &) {}
|
|
|
|
try {
|
|
|
|
dm.def("Dupe1", []() { return Dupe1(); });
|
|
|
|
failures.append("Dupe1 function");
|
|
|
|
} catch (std::runtime_error &) {}
|
|
|
|
try {
|
|
|
|
py::class_<Dupe3>(dm, "dupe1_factory");
|
|
|
|
failures.append("dupe1_factory");
|
|
|
|
} catch (std::runtime_error &) {}
|
|
|
|
try {
|
|
|
|
py::exception<Dupe3>(dm, "Dupe2");
|
|
|
|
failures.append("Dupe2");
|
|
|
|
} catch (std::runtime_error &) {}
|
|
|
|
try {
|
|
|
|
dm.def("DupeException", []() { return 30; });
|
|
|
|
failures.append("DupeException1");
|
|
|
|
} catch (std::runtime_error &) {}
|
|
|
|
try {
|
|
|
|
py::class_<DupeException>(dm, "DupeException");
|
|
|
|
failures.append("DupeException2");
|
|
|
|
} catch (std::runtime_error &) {}
|
|
|
|
|
|
|
|
return failures;
|
|
|
|
});
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
}
|