MCPcopy Create free account
hub / github.com/pybind/pybind11 / TEST_SUBMODULE

Function TEST_SUBMODULE

tests/test_local_bindings.cpp:20–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18#include <utility>
19
20TEST_SUBMODULE(local_bindings, m) {
21 // test_load_external
22 m.def("load_external1", [](ExternalType1 &e) { return e.i; });
23 m.def("load_external2", [](ExternalType2 &e) { return e.i; });
24 m.def("load_external3", [](ExternalType3 &e) { return e.i; });
25
26 struct SharedKeepAlive {
27 std::shared_ptr<int> contents;
28 int value() const { return contents ? *contents : -20251012; }
29 long use_count() const { return contents.use_count(); }
30 };
31 py::class_<SharedKeepAlive>(m, "SharedKeepAlive")
32 .def_property_readonly("value", &SharedKeepAlive::value)
33 .def_property_readonly("use_count", &SharedKeepAlive::use_count);
34 m.def("load_external2_shared", [](const std::shared_ptr<ExternalType2> &p) {
35 return SharedKeepAlive{std::shared_ptr<int>(p, &p->i)};
36 });
37 m.def("load_external3_shared", [](const std::shared_ptr<ExternalType3> &p) {
38 return SharedKeepAlive{std::shared_ptr<int>(p, &p->i)};
39 });
40 m.def("load_external1_unique", [](std::unique_ptr<ExternalType1> p) { return p->i; });
41 m.def("load_external3_unique", [](std::unique_ptr<ExternalType3> p) { return p->i; });
42
43 // Aspects of set_foreign_holder that are not covered:
44 // - loading a foreign instance into a custom holder should fail
45 // - we're only covering the case where the local module doesn't know
46 // about the type; the paths where it does (e.g., if both global and
47 // foreign-module-local bindings exist for the same type) should work
48 // the same way (they use the same code so they very likely do)
49
50 // test_local_bindings
51 // Register a class with py::module_local:
52 bind_local<LocalType, -1>(m, "LocalType", py::module_local()).def("get3", [](LocalType &t) {
53 return t.i + 3;
54 });
55
56 m.def("local_value", [](LocalType &l) { return l.i; });
57
58 // test_nonlocal_failure
59 // The main pybind11 test module is loaded first, so this registration will succeed (the second
60 // one, in pybind11_cross_module_tests.cpp, is designed to fail):
61 bind_local<NonLocalType, 0>(m, "NonLocalType")
62 .def(py::init<int>())
63 .def("get", [](LocalType &i) { return i.i; });
64
65 // test_duplicate_local
66 // py::module_local declarations should be visible across compilation units that get linked
67 // together; this tries to register a duplicate local. It depends on a definition in
68 // test_class.cpp and should raise a runtime error from the duplicate definition attempt. If
69 // test_class isn't available it *also* throws a runtime error (with "test_class not enabled"
70 // as value).
71 m.def("register_local_external", [m]() {
72 auto main = py::module_::import("pybind11_tests");
73 if (py::hasattr(main, "class_")) {
74 bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
75 } else {
76 throw std::runtime_error("test_class not enabled");
77 }

Callers

nothing calls this directly

Calls 6

module_localClass · 0.85
importFunction · 0.85
hasattrFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
nameMethod · 0.45

Tested by

no test coverage detected