| 101 | } // namespace pybind11_tests |
| 102 | |
| 103 | PYBIND11_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 | } |