blob: 897cf89c468ba04af0051002fa7ac3a7409f3f50 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_modules.cpp -- nested modules, importing modules, and
Wenzel Jakobdb028d62015-10-13 23:44:25 +02003 internal references
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020011#include "pybind11_tests.h"
12#include "constructor_stats.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020013
Jason Rhinelander391c7542017-07-25 16:47:36 -040014TEST_SUBMODULE(modules, m) {
15 // test_nested_modules
Henry Schreiner6bcd2202020-10-03 13:38:03 -040016 // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
Jason Rhinelander391c7542017-07-25 16:47:36 -040017 py::module m_sub = m.def_submodule("subsubmodule");
18 m_sub.def("submodule_func", []() { return "submodule_func()"; });
Wenzel Jakob38bd7112015-07-05 20:05:44 +020019
Jason Rhinelander391c7542017-07-25 16:47:36 -040020 // test_reference_internal
21 class A {
22 public:
23 A(int v) : v(v) { print_created(this, v); }
24 ~A() { print_destroyed(this); }
25 A(const A&) { print_copy_created(this); }
26 A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
27 std::string toString() { return "A[" + std::to_string(v) + "]"; }
28 private:
29 int v;
30 };
Wenzel Jakob38bd7112015-07-05 20:05:44 +020031 py::class_<A>(m_sub, "A")
32 .def(py::init<int>())
33 .def("__repr__", &A::toString);
34
Jason Rhinelander391c7542017-07-25 16:47:36 -040035 class B {
36 public:
37 B() { print_default_created(this); }
38 ~B() { print_destroyed(this); }
39 B(const B&) { print_copy_created(this); }
40 B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
41 A &get_a1() { return a1; }
42 A &get_a2() { return a2; }
43
44 A a1{1};
45 A a2{2};
46 };
Wenzel Jakob38bd7112015-07-05 20:05:44 +020047 py::class_<B>(m_sub, "B")
48 .def(py::init<>())
49 .def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
50 .def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
51 .def_readwrite("a1", &B::a1) // def_readonly uses an internal reference return policy by default
52 .def_readwrite("a2", &B::a2);
Wenzel Jakobdb028d62015-10-13 23:44:25 +020053
Henry Schreiner6bcd2202020-10-03 13:38:03 -040054 // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
Wenzel Jakobdb028d62015-10-13 23:44:25 +020055 m.attr("OD") = py::module::import("collections").attr("OrderedDict");
Dean Moldovanbdfb50f2017-06-07 16:52:50 +020056
Jason Rhinelander391c7542017-07-25 16:47:36 -040057 // test_duplicate_registration
Dean Moldovanbdfb50f2017-06-07 16:52:50 +020058 // Registering two things with the same name
59 m.def("duplicate_registration", []() {
60 class Dupe1 { };
61 class Dupe2 { };
62 class Dupe3 { };
63 class DupeException { };
64
Henry Schreiner6bcd2202020-10-03 13:38:03 -040065 auto dm = py::module_("dummy");
Dean Moldovanbdfb50f2017-06-07 16:52:50 +020066 auto failures = py::list();
67
68 py::class_<Dupe1>(dm, "Dupe1");
69 py::class_<Dupe2>(dm, "Dupe2");
70 dm.def("dupe1_factory", []() { return Dupe1(); });
71 py::exception<DupeException>(dm, "DupeException");
72
73 try {
74 py::class_<Dupe1>(dm, "Dupe1");
75 failures.append("Dupe1 class");
76 } catch (std::runtime_error &) {}
77 try {
78 dm.def("Dupe1", []() { return Dupe1(); });
79 failures.append("Dupe1 function");
80 } catch (std::runtime_error &) {}
81 try {
82 py::class_<Dupe3>(dm, "dupe1_factory");
83 failures.append("dupe1_factory");
84 } catch (std::runtime_error &) {}
85 try {
86 py::exception<Dupe3>(dm, "Dupe2");
87 failures.append("Dupe2");
88 } catch (std::runtime_error &) {}
89 try {
90 dm.def("DupeException", []() { return 30; });
91 failures.append("DupeException1");
92 } catch (std::runtime_error &) {}
93 try {
94 py::class_<DupeException>(dm, "DupeException");
95 failures.append("DupeException2");
96 } catch (std::runtime_error &) {}
97
98 return failures;
99 });
Jason Rhinelander391c7542017-07-25 16:47:36 -0400100}