|
Packit |
534379 |
/*
|
|
Packit |
534379 |
tests/test_modules.cpp -- nested modules, importing modules, and
|
|
Packit |
534379 |
internal references
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
All rights reserved. Use of this source code is governed by a
|
|
Packit |
534379 |
BSD-style license that can be found in the LICENSE file.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "pybind11_tests.h"
|
|
Packit |
534379 |
#include "constructor_stats.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
TEST_SUBMODULE(modules, m) {
|
|
Packit |
534379 |
// test_nested_modules
|
|
Packit |
534379 |
py::module m_sub = m.def_submodule("subsubmodule");
|
|
Packit |
534379 |
m_sub.def("submodule_func", []() { return "submodule_func()"; });
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_reference_internal
|
|
Packit |
534379 |
class A {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
A(int v) : v(v) { print_created(this, v); }
|
|
Packit |
534379 |
~A() { print_destroyed(this); }
|
|
Packit |
534379 |
A(const A&) { print_copy_created(this); }
|
|
Packit |
534379 |
A& operator=(const A ©) { print_copy_assigned(this); v = copy.v; return *this; }
|
|
Packit |
534379 |
std::string toString() { return "A[" + std::to_string(v) + "]"; }
|
|
Packit |
534379 |
private:
|
|
Packit |
534379 |
int v;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
py::class_(m_sub, "A")
|
|
Packit |
534379 |
.def(py::init<int>())
|
|
Packit |
534379 |
.def("__repr__", &A::toString);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class B {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
B() { print_default_created(this); }
|
|
Packit |
534379 |
~B() { print_destroyed(this); }
|
|
Packit |
534379 |
B(const B&) { print_copy_created(this); }
|
|
Packit |
534379 |
B& operator=(const B ©) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
|
|
Packit |
534379 |
A &get_a1() { return a1; }
|
|
Packit |
534379 |
A &get_a2() { return a2; }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
A a1{1};
|
|
Packit |
534379 |
A a2{2};
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
py::class_(m_sub, "B")
|
|
Packit |
534379 |
.def(py::init<>())
|
|
Packit |
534379 |
.def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
|
|
Packit |
534379 |
.def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
|
|
Packit |
534379 |
.def_readwrite("a1", &B::a1) // def_readonly uses an internal reference return policy by default
|
|
Packit |
534379 |
.def_readwrite("a2", &B::a2);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
m.attr("OD") = py::module::import("collections").attr("OrderedDict");
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_duplicate_registration
|
|
Packit |
534379 |
// Registering two things with the same name
|
|
Packit |
534379 |
m.def("duplicate_registration", []() {
|
|
Packit |
534379 |
class Dupe1 { };
|
|
Packit |
534379 |
class Dupe2 { };
|
|
Packit |
534379 |
class Dupe3 { };
|
|
Packit |
534379 |
class DupeException { };
|
|
Packit |
534379 |
|
|
Packit |
534379 |
auto dm = py::module("dummy");
|
|
Packit |
534379 |
auto failures = py::list();
|
|
Packit |
534379 |
|
|
Packit |
534379 |
py::class_<Dupe1>(dm, "Dupe1");
|
|
Packit |
534379 |
py::class_<Dupe2>(dm, "Dupe2");
|
|
Packit |
534379 |
dm.def("dupe1_factory", []() { return Dupe1(); });
|
|
Packit |
534379 |
py::exception<DupeException>(dm, "DupeException");
|
|
Packit |
534379 |
|
|
Packit |
534379 |
try {
|
|
Packit |
534379 |
py::class_<Dupe1>(dm, "Dupe1");
|
|
Packit |
534379 |
failures.append("Dupe1 class");
|
|
Packit |
534379 |
} catch (std::runtime_error &) {}
|
|
Packit |
534379 |
try {
|
|
Packit |
534379 |
dm.def("Dupe1", []() { return Dupe1(); });
|
|
Packit |
534379 |
failures.append("Dupe1 function");
|
|
Packit |
534379 |
} catch (std::runtime_error &) {}
|
|
Packit |
534379 |
try {
|
|
Packit |
534379 |
py::class_<Dupe3>(dm, "dupe1_factory");
|
|
Packit |
534379 |
failures.append("dupe1_factory");
|
|
Packit |
534379 |
} catch (std::runtime_error &) {}
|
|
Packit |
534379 |
try {
|
|
Packit |
534379 |
py::exception<Dupe3>(dm, "Dupe2");
|
|
Packit |
534379 |
failures.append("Dupe2");
|
|
Packit |
534379 |
} catch (std::runtime_error &) {}
|
|
Packit |
534379 |
try {
|
|
Packit |
534379 |
dm.def("DupeException", []() { return 30; });
|
|
Packit |
534379 |
failures.append("DupeException1");
|
|
Packit |
534379 |
} catch (std::runtime_error &) {}
|
|
Packit |
534379 |
try {
|
|
Packit |
534379 |
py::class_<DupeException>(dm, "DupeException");
|
|
Packit |
534379 |
failures.append("DupeException2");
|
|
Packit |
534379 |
} catch (std::runtime_error &) {}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return failures;
|
|
Packit |
534379 |
});
|
|
Packit |
534379 |
}
|