| |
| |
| |
| |
| |
| |
| |
| |
| #include <pybind11/pybind11.h> |
| #include <cstdint> |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| namespace { |
|
|
| namespace py = pybind11; |
| void gil_acquire() { py::gil_scoped_acquire gil; } |
|
|
| constexpr char kModuleName[] = "cross_module_gil_utils"; |
|
|
| #if PY_MAJOR_VERSION >= 3 |
| struct PyModuleDef moduledef = { |
| PyModuleDef_HEAD_INIT, |
| kModuleName, |
| NULL, |
| 0, |
| NULL, |
| NULL, |
| NULL, |
| NULL, |
| NULL |
| }; |
| #else |
| PyMethodDef module_methods[] = { |
| {NULL, NULL, 0, NULL} |
| }; |
| #endif |
|
|
| } |
|
|
| extern "C" PYBIND11_EXPORT |
| #if PY_MAJOR_VERSION >= 3 |
| PyObject* PyInit_cross_module_gil_utils() |
| #else |
| void initcross_module_gil_utils() |
| #endif |
| { |
|
|
| PyObject* m = |
| #if PY_MAJOR_VERSION >= 3 |
| PyModule_Create(&moduledef); |
| #else |
| Py_InitModule(kModuleName, module_methods); |
| #endif |
|
|
| if (m != NULL) { |
| static_assert( |
| sizeof(&gil_acquire) == sizeof(void*), |
| "Function pointer must have the same size as void*"); |
| PyModule_AddObject(m, "gil_acquire_funcaddr", |
| PyLong_FromVoidPtr(reinterpret_cast<void*>(&gil_acquire))); |
| } |
|
|
| #if PY_MAJOR_VERSION >= 3 |
| return m; |
| #endif |
| } |
|
|