Skip to content

Commit ba21366

Browse files
author
Ralf W. Grosse-Kunstleve
committed
merging current boost/python and libs/python from trunk into release branch
[SVN r67483]
1 parent 736ba48 commit ba21366

2 files changed

Lines changed: 74 additions & 41 deletions

File tree

include/boost/python/module_init.hpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,54 @@
1313

1414
namespace boost { namespace python { namespace detail {
1515

16+
# if PY_VERSION_HEX >= 0x03000000
17+
18+
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef&, void(*)());
19+
20+
#else
21+
1622
BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
1723

24+
#endif
25+
1826
}}}
1927

2028
# if PY_VERSION_HEX >= 0x03000000
2129

22-
# define _BOOST_PYTHON_MODULE_INIT(name) \
23-
PyObject* BOOST_PP_CAT (PyInit_,name)() \
24-
{ \
25-
return boost::python::detail::init_module( \
26-
BOOST_PP_STRINGIZE(name),&BOOST_PP_CAT(init_module_,name)); \
27-
} \
28-
void BOOST_PP_CAT(init_module_,name)()
30+
# define _BOOST_PYTHON_MODULE_INIT(name) \
31+
PyObject* BOOST_PP_CAT(PyInit_, name)() \
32+
{ \
33+
static PyModuleDef_Base initial_m_base = { \
34+
PyObject_HEAD_INIT(NULL) \
35+
0, /* m_init */ \
36+
0, /* m_index */ \
37+
0 /* m_copy */ }; \
38+
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
39+
\
40+
static struct PyModuleDef moduledef = { \
41+
initial_m_base, \
42+
BOOST_PP_STRINGIZE(name), \
43+
0, /* m_doc */ \
44+
-1, /* m_size */ \
45+
initial_methods, \
46+
0, /* m_reload */ \
47+
0, /* m_traverse */ \
48+
0, /* m_clear */ \
49+
0, /* m_free */ \
50+
}; \
51+
\
52+
return boost::python::detail::init_module( \
53+
moduledef, BOOST_PP_CAT(init_module_, name) ); \
54+
} \
55+
void BOOST_PP_CAT(init_module_, name)()
2956

3057
# else
3158

3259
# define _BOOST_PYTHON_MODULE_INIT(name) \
33-
void BOOST_PP_CAT(init,name)() \
60+
void BOOST_PP_CAT(init,name)() \
3461
{ \
3562
boost::python::detail::init_module( \
36-
BOOST_PP_STRINGIZE(name),&BOOST_PP_CAT(init_module_,name)); \
63+
BOOST_PP_STRINGIZE(name),&BOOST_PP_CAT(init_module_,name)); \
3764
} \
3865
void BOOST_PP_CAT(init_module_,name)()
3966

@@ -42,23 +69,23 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
4269
# if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(BOOST_PYTHON_STATIC_MODULE)
4370

4471
# define BOOST_PYTHON_MODULE_INIT(name) \
45-
void BOOST_PP_CAT(init_module_,name)(); \
72+
void BOOST_PP_CAT(init_module_,name)(); \
4673
extern "C" __declspec(dllexport) _BOOST_PYTHON_MODULE_INIT(name)
4774

4875
# elif BOOST_PYTHON_USE_GCC_SYMBOL_VISIBILITY
4976

5077
# define BOOST_PYTHON_MODULE_INIT(name) \
51-
void BOOST_PP_CAT(init_module_,name)(); \
78+
void BOOST_PP_CAT(init_module_,name)(); \
5279
extern "C" __attribute__ ((visibility("default"))) _BOOST_PYTHON_MODULE_INIT(name)
5380

5481
# else
5582

5683
# define BOOST_PYTHON_MODULE_INIT(name) \
57-
void BOOST_PP_CAT(init_module_,name)(); \
84+
void BOOST_PP_CAT(init_module_,name)(); \
5885
extern "C" _BOOST_PYTHON_MODULE_INIT(name)
5986

6087
# endif
6188

62-
# endif
89+
# endif
6390

6491
#endif // MODULE_INIT_DWA20020722_HPP

src/module.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111

1212
namespace boost { namespace python { namespace detail {
1313

14+
namespace
15+
{
16+
PyObject* init_module_in_scope(PyObject* m, void(*init_function)())
17+
{
18+
if (m != 0)
19+
{
20+
// Create the current module scope
21+
object m_obj(((borrowed_reference_t*)m));
22+
scope current_module(m_obj);
23+
24+
handle_exception(init_function);
25+
}
26+
27+
return m;
28+
}
29+
}
30+
1431
BOOST_PYTHON_DECL void scope_setattr_doc(char const* name, object const& x, char const* doc)
1532
{
1633
// Use function::add_to_namespace to achieve overloading if
@@ -19,42 +36,31 @@ BOOST_PYTHON_DECL void scope_setattr_doc(char const* name, object const& x, char
1936
objects::add_to_namespace(current, name, x, doc);
2037
}
2138

39+
#if PY_VERSION_HEX >= 0x03000000
40+
41+
PyObject* init_module(PyModuleDef& moduledef, void(*init_function)())
42+
{
43+
return init_module_in_scope(
44+
PyModule_Create(&moduledef),
45+
init_function);
46+
}
47+
48+
#else
49+
2250
namespace
2351
{
24-
PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } };
52+
PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } };
2553
}
2654

2755
BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*init_function)())
2856
{
29-
#if PY_VERSION_HEX >= 0x03000000
30-
static struct PyModuleDef moduledef = {
31-
PyModuleDef_HEAD_INIT,
32-
name,
33-
0, /* m_doc */
34-
-1, /* m_size */
35-
initial_methods,
36-
0, /* m_reload */
37-
0, /* m_traverse */
38-
0, /* m_clear */
39-
0, /* m_free */
40-
};
41-
PyObject* m = PyModule_Create(&moduledef);
42-
#else
43-
PyObject* m
44-
= Py_InitModule(const_cast<char*>(name), initial_methods);
45-
#endif
46-
47-
if (m != 0)
48-
{
49-
// Create the current module scope
50-
object m_obj(((borrowed_reference_t*)m));
51-
scope current_module(m_obj);
52-
53-
handle_exception(init_function);
54-
}
55-
return m;
57+
return init_module_in_scope(
58+
Py_InitModule(const_cast<char*>(name), initial_methods),
59+
init_function);
5660
}
5761

62+
#endif
63+
5864
}}} // namespace boost::python::detail
5965

6066
namespace boost { namespace python {

0 commit comments

Comments
 (0)