forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_init.hpp
More file actions
327 lines (294 loc) · 10.9 KB
/
Copy pathmodule_init.hpp
File metadata and controls
327 lines (294 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef MODULE_INIT_DWA20020722_HPP
# define MODULE_INIT_DWA20020722_HPP
# include <boost/python/detail/prefix.hpp>
# include <boost/preprocessor/cat.hpp>
# include <boost/preprocessor/stringize.hpp>
# if PY_VERSION_HEX >= 0x03050000
# include <boost/static_assert.hpp>
# include <boost/type_traits/is_pod.hpp>
# endif
# ifndef BOOST_PYTHON_MODULE_INIT
namespace boost { namespace python {
# ifdef HAS_CXX11
// Use to activate the Py_MOD_GIL_NOT_USED flag.
class mod_gil_not_used {
public:
explicit mod_gil_not_used(bool flag = true) : flag_(flag) {}
bool flag() const { return flag_; }
private:
bool flag_;
};
namespace detail {
inline bool gil_not_used_option() { return false; }
template <typename F, typename... O>
bool gil_not_used_option(F &&, O &&...o);
template <typename... O>
inline bool gil_not_used_option(mod_gil_not_used f, O &&...o) {
return f.flag() || gil_not_used_option(o...);
}
template <typename F, typename... O>
inline bool gil_not_used_option(F &&, O &&...o) {
return gil_not_used_option(o...);
}
}
# endif // HAS_CXX11
namespace detail {
# if PY_VERSION_HEX >= 0x03000000
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef&, void(*)(), bool gil_not_used = false);
# if PY_VERSION_HEX >= 0x03050000
BOOST_PYTHON_DECL int exec_module(PyObject*, void(*)());
BOOST_PYTHON_DECL int exec_module_with_state(PyObject*, void(*)(void*));
# endif // PY_VERSION_HEX >= 0x03050000
# else // PY_VERSION_HEX >= 0x03000000
BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
# endif // PY_VERSION_HEX >= 0x03000000
}}}
# if PY_VERSION_HEX >= 0x03000000
# ifdef HAS_CXX11
# define _BOOST_PYTHON_MODULE_INIT(name, ...) \
PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
-1, /* m_size */ \
initial_methods, \
0, /* m_reload */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return boost::python::detail::init_module( \
moduledef, BOOST_PP_CAT(init_module_, name), \
boost::python::detail::gil_not_used_option(__VA_ARGS__) ); \
} \
void BOOST_PP_CAT(init_module_, name)()
# else // !HAS_CXX11
# define _BOOST_PYTHON_MODULE_INIT(name) \
PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
-1, /* m_size */ \
initial_methods, \
0, /* m_reload */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return boost::python::detail::init_module( \
moduledef, BOOST_PP_CAT(init_module_, name) ); \
} \
void BOOST_PP_CAT(init_module_, name)()
# endif // HAS_CXX11
# if PY_VERSION_HEX >= 0x03050000
# if defined(HAS_CXX11) && (PY_VERSION_HEX >= 0x030D0000)
# define _BOOST_PYTHON_MODULE_MULTI_PHASE_INIT(name, ...) \
int BOOST_PP_CAT(exec_module_,name)(PyObject* mod) \
{ \
return boost::python::detail::exec_module( \
mod, BOOST_PP_CAT(init_module_, name) ); \
} \
extern "C" BOOST_SYMBOL_EXPORT PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static PyModuleDef_Slot slots[] = { \
{Py_mod_exec, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BOOST_PP_CAT(exec_module_, name)))}, \
{Py_mod_gil, boost::python::detail::gil_not_used_option(__VA_ARGS__) ? Py_MOD_GIL_NOT_USED : Py_MOD_GIL_USED}, \
{0, NULL} \
}; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
0, /* m_size */ \
initial_methods, \
slots, /* m_slots */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return PyModuleDef_Init(&moduledef); \
} \
void BOOST_PP_CAT(init_module_, name)()
# define _BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType, ...) \
int BOOST_PP_CAT(exec_module_,name)(PyObject* mod) \
{ \
return boost::python::detail::exec_module_with_state( \
mod, reinterpret_cast<void(*)(void*)>(BOOST_PP_CAT(init_module_, name)) ); \
} \
extern "C" BOOST_SYMBOL_EXPORT PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
BOOST_STATIC_ASSERT_MSG(boost::is_pod<StateType>::value, \
"Module State MUST be a plain POD structure!"); \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static PyModuleDef_Slot slots[] = { \
{Py_mod_exec, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BOOST_PP_CAT(exec_module_, name)))}, \
{Py_mod_gil, boost::python::detail::gil_not_used_option(__VA_ARGS__) ? Py_MOD_GIL_NOT_USED : Py_MOD_GIL_USED}, \
{0, NULL} \
}; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
sizeof(StateType), /* m_size */ \
initial_methods, \
slots, /* m_slots */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return PyModuleDef_Init(&moduledef); \
} \
void BOOST_PP_CAT(init_module_, name)(StateType* state)
# else // ! HAS_CXX11 && Python 3.13+
# define _BOOST_PYTHON_MODULE_MULTI_PHASE_INIT(name) \
int BOOST_PP_CAT(exec_module_,name)(PyObject* mod) \
{ \
return boost::python::detail::exec_module( \
mod, BOOST_PP_CAT(init_module_, name) ); \
} \
extern "C" BOOST_SYMBOL_EXPORT PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static PyModuleDef_Slot slots[] = { \
{Py_mod_exec, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BOOST_PP_CAT(exec_module_, name)))}, \
{0, NULL} \
}; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
0, /* m_size */ \
initial_methods, \
slots, /* m_slots */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return PyModuleDef_Init(&moduledef); \
} \
void BOOST_PP_CAT(init_module_, name)()
# define _BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType) \
int BOOST_PP_CAT(exec_module_,name)(PyObject* mod) \
{ \
return boost::python::detail::exec_module_with_state( \
mod, reinterpret_cast<void(*)(void*)>(BOOST_PP_CAT(init_module_, name)) ); \
} \
extern "C" BOOST_SYMBOL_EXPORT PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
BOOST_STATIC_ASSERT_MSG(boost::is_pod<StateType>::value, \
"Module State MUST be a plain POD structure!"); \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static PyModuleDef_Slot slots[] = { \
{Py_mod_exec, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BOOST_PP_CAT(exec_module_, name)))}, \
{0, NULL} \
}; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
sizeof(StateType), /* m_size */ \
initial_methods, \
slots, /* m_slots */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return PyModuleDef_Init(&moduledef); \
} \
void BOOST_PP_CAT(init_module_, name)(StateType* state)
# endif // HAS_CXX11 && Python 3.13+
# endif // PY_VERSION_HEX >= 0x03050000
# else // ! PY_VERSION_HEX >= 0x03000000
# define _BOOST_PYTHON_MODULE_INIT(name) \
void BOOST_PP_CAT(init,name)() \
{ \
boost::python::detail::init_module( \
BOOST_PP_STRINGIZE(name),&BOOST_PP_CAT(init_module_,name)); \
} \
void BOOST_PP_CAT(init_module_,name)()
# endif // PY_VERSION_HEX >= 0x03000000
# if defined(HAS_CXX11) && (PY_VERSION_HEX >= 0x03000000)
# define BOOST_PYTHON_MODULE_INIT(name, ...) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_INIT(name, __VA_ARGS__)
# else
# define BOOST_PYTHON_MODULE_INIT(name) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_INIT(name)
# endif // HAS_CXX11 && Python 3
# if PY_VERSION_HEX >= 0x03050000
# if defined(HAS_CXX11) && (PY_VERSION_HEX >= 0x030D0000)
# define BOOST_PYTHON_MODULE_MULTI_PHASE_INIT(name, ...) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_MULTI_PHASE_INIT(name, __VA_ARGS__)
# define BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType, ...) \
void BOOST_PP_CAT(init_module_,name)(StateType* state); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType, __VA_ARGS__)
# else
# define BOOST_PYTHON_MODULE_MULTI_PHASE_INIT(name) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_MULTI_PHASE_INIT(name)
# define BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType) \
void BOOST_PP_CAT(init_module_,name)(StateType* state); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType)
# endif // HAS_CXX11 && Python 3.13+
# endif // PY_VERSION_HEX >= 0x03050000
# endif // BOOST_PYTHON_MODULE_INIT
#endif // MODULE_INIT_DWA20020722_HPP