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

Function PYBIND11_MODULE

tests/mod_per_interpreter_gil_with_singleton.cpp:103–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

101} // namespace pybind11_tests
102
103PYBIND11_MODULE(mod_per_interpreter_gil_with_singleton,
104 m,
105 py::mod_gil_not_used(),
106 py::multiple_interpreters::per_interpreter_gil()) {
107 using namespace pybind11_tests::mod_per_interpreter_gil_with_singleton;
108
109#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
110 m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true;
111#else
112 m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false;
113#endif
114
115 MySingleton::init();
116
117 // Ensure py::multiple_interpreters::per_interpreter_gil() works with singletons using
118 // py::gil_safe_call_once_and_store
119 m.def(
120 "get_objects_in_singleton",
121 []() -> std::vector<py::handle> { return MySingleton::get_instance().get_objects(); },
122 "Get the list of objects stored in the singleton");
123
124 // Ensure py::multiple_interpreters::per_interpreter_gil() works with class bindings
125 py::class_<MyClass>(m, "MyClass")
126 .def(py::init<py::ssize_t>())
127 .def("get_value", &MyClass::get_value);
128
129 // Ensure py::multiple_interpreters::per_interpreter_gil() works with global exceptions
130 py::register_exception<MyGlobalError>(m, "MyGlobalError");
131 // Ensure py::multiple_interpreters::per_interpreter_gil() works with local exceptions
132 py::register_local_exception<MyLocalError>(m, "MyLocalError");
133
134#ifdef PYBIND11_HAS_NATIVE_ENUM
135 // Ensure py::multiple_interpreters::per_interpreter_gil() works with native_enum
136 py::native_enum<MyEnum>(m, "MyEnum", "enum.IntEnum")
137 .value("ONE", MyEnum::ONE)
138 .value("TWO", MyEnum::TWO)
139 .value("THREE", MyEnum::THREE)
140 .finalize();
141#else
142 py::enum_<MyEnum>(m, "MyEnum")
143 .value("ONE", MyEnum::ONE)
144 .value("TWO", MyEnum::TWO)
145 .value("THREE", MyEnum::THREE);
146#endif
147}

Callers

nothing calls this directly

Calls 4

initFunction · 0.85
attrMethod · 0.80
finalizeMethod · 0.80
valueMethod · 0.45

Tested by

no test coverage detected