-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathmodule.cpp
More file actions
128 lines (103 loc) · 3.12 KB
/
Copy pathmodule.cpp
File metadata and controls
128 lines (103 loc) · 3.12 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
// (C) Copyright David Abrahams 2000.
// 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)
//
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
#include <boost/python/scope.hpp>
#include <boost/python/object/add_to_namespace.hpp>
namespace boost { namespace python { namespace detail {
namespace
{
PyObject* init_module_in_scope(PyObject* m, void(*init_function)())
{
if (m != 0)
{
// Create the current module scope
object m_obj(((borrowed_reference_t*)m));
scope current_module(m_obj);
if (handle_exception(init_function)) return NULL;
}
return m;
}
# if PY_VERSION_HEX >= 0x03050000
class init_function_with_state {
public:
init_function_with_state(void(*init_function)(void*), void* state)
: init_function_(init_function), state_(state) {}
void operator()() const { init_function_(state_); }
private:
void(*const init_function_)(void*);
void* const state_;
};
PyObject* init_module_in_scope_with_state(PyObject* m, void(*init_function)(void*))
{
if (m != 0)
{
// Create the current module scope
object m_obj(((borrowed_reference_t*)m));
scope current_module(m_obj);
void* state = PyModule_GetState(m);
if (handle_exception(init_function_with_state(init_function, state))) return NULL;
}
return m;
}
# endif
}
BOOST_PYTHON_DECL void scope_setattr_doc(char const* name, object const& x, char const* doc)
{
// Use function::add_to_namespace to achieve overloading if
// appropriate.
scope current;
objects::add_to_namespace(current, name, x, doc);
}
#if PY_VERSION_HEX >= 0x03000000
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef& moduledef,
void(*init_function)(), bool gil_not_used)
{
PyObject *mod = PyModule_Create(&moduledef);
#ifdef Py_GIL_DISABLED
if (mod != NULL && gil_not_used) {
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
}
#endif
return init_module_in_scope(
mod,
init_function);
}
# if PY_VERSION_HEX >= 0x03050000
BOOST_PYTHON_DECL int exec_module(PyObject* mod, void(*init_function)())
{
PyObject* retval = init_module_in_scope(
mod,
init_function);
return retval ? 0 : -1;
}
BOOST_PYTHON_DECL int exec_module_with_state(PyObject* mod, void(*init_function)(void*))
{
PyObject* retval = init_module_in_scope_with_state(
mod,
init_function);
return retval ? 0 : -1;
}
# endif
#else
namespace
{
PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } };
}
BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*init_function)())
{
return init_module_in_scope(
Py_InitModule(const_cast<char*>(name), initial_methods),
init_function);
}
#endif
}}} // namespace boost::python::detail
namespace boost { namespace python {
namespace detail
{
BOOST_PYTHON_DECL PyObject* current_scope = 0;
}
}}