mirror of
https://github.com/boostorg/python.git
synced 2026-01-20 04:42:28 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80731af9ee |
@@ -11,11 +11,41 @@
|
|||||||
|
|
||||||
# ifndef BOOST_PYTHON_MODULE_INIT
|
# ifndef BOOST_PYTHON_MODULE_INIT
|
||||||
|
|
||||||
namespace boost { namespace python { namespace detail {
|
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
|
# if PY_VERSION_HEX >= 0x03000000
|
||||||
|
|
||||||
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef&, void(*)());
|
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef&, void(*)(), bool gil_not_used = false);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
@@ -27,7 +57,37 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
|
|||||||
|
|
||||||
# if PY_VERSION_HEX >= 0x03000000
|
# if PY_VERSION_HEX >= 0x03000000
|
||||||
|
|
||||||
# define _BOOST_PYTHON_MODULE_INIT(name) \
|
# 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)() \
|
PyObject* BOOST_PP_CAT(PyInit_, name)() \
|
||||||
{ \
|
{ \
|
||||||
static PyModuleDef_Base initial_m_base = { \
|
static PyModuleDef_Base initial_m_base = { \
|
||||||
@@ -53,6 +113,7 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
|
|||||||
moduledef, BOOST_PP_CAT(init_module_, name) ); \
|
moduledef, BOOST_PP_CAT(init_module_, name) ); \
|
||||||
} \
|
} \
|
||||||
void BOOST_PP_CAT(init_module_, name)()
|
void BOOST_PP_CAT(init_module_, name)()
|
||||||
|
# endif // HAS_CXX11
|
||||||
|
|
||||||
# else
|
# else
|
||||||
|
|
||||||
@@ -66,9 +127,15 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
|
|||||||
|
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
# define BOOST_PYTHON_MODULE_INIT(name) \
|
# ifdef HAS_CXX11
|
||||||
|
# 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)(); \
|
void BOOST_PP_CAT(init_module_,name)(); \
|
||||||
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_INIT(name)
|
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_INIT(name)
|
||||||
|
# endif // HAS_CXX11
|
||||||
|
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
|||||||
@@ -38,11 +38,12 @@ BOOST_PYTHON_DECL void scope_setattr_doc(char const* name, object const& x, char
|
|||||||
|
|
||||||
#if PY_VERSION_HEX >= 0x03000000
|
#if PY_VERSION_HEX >= 0x03000000
|
||||||
|
|
||||||
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef& moduledef, void(*init_function)())
|
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef& moduledef,
|
||||||
|
void(*init_function)(), bool gil_not_used)
|
||||||
{
|
{
|
||||||
PyObject *mod = PyModule_Create(&moduledef);
|
PyObject *mod = PyModule_Create(&moduledef);
|
||||||
#ifdef Py_GIL_DISABLED
|
#ifdef Py_GIL_DISABLED
|
||||||
if (mod != NULL) {
|
if (mod != NULL && gil_not_used) {
|
||||||
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
|
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ for t in [('injected',),
|
|||||||
('raw_ctor',),
|
('raw_ctor',),
|
||||||
('exception_translator',),
|
('exception_translator',),
|
||||||
('module_init_exception',),
|
('module_init_exception',),
|
||||||
|
('module_nogil',),
|
||||||
('test_enum', ['enum_ext']),
|
('test_enum', ['enum_ext']),
|
||||||
('test_cltree', ['cltree']),
|
('test_cltree', ['cltree']),
|
||||||
('newtest', ['m1', 'm2']),
|
('newtest', ['m1', 'm2']),
|
||||||
|
|||||||
25
test/module_nogil.cpp
Normal file
25
test/module_nogil.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// Test for BOOST_PYTHON_MODULE with optional mod_gil_not_used argument
|
||||||
|
|
||||||
|
#include <boost/python/module.hpp>
|
||||||
|
#include <boost/python/def.hpp>
|
||||||
|
|
||||||
|
// Simple function to export
|
||||||
|
int get_value() {
|
||||||
|
return 1234;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef HAS_CXX11
|
||||||
|
// C++11 build: test with mod_gil_not_used option
|
||||||
|
BOOST_PYTHON_MODULE(module_nogil_ext, boost::python::mod_gil_not_used())
|
||||||
|
{
|
||||||
|
using namespace boost::python;
|
||||||
|
def("get_value", get_value);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// C++98 build: test without optional arguments
|
||||||
|
BOOST_PYTHON_MODULE(module_nogil_ext)
|
||||||
|
{
|
||||||
|
using namespace boost::python;
|
||||||
|
def("get_value", get_value);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
29
test/module_nogil.py
Normal file
29
test/module_nogil.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""
|
||||||
|
>>> from module_nogil_ext import *
|
||||||
|
>>> get_value()
|
||||||
|
1234
|
||||||
|
>>> import sys, sysconfig
|
||||||
|
>>> Py_GIL_DISABLED = bool(sysconfig.get_config_var('Py_GIL_DISABLED'))
|
||||||
|
>>> if Py_GIL_DISABLED and sys._is_gil_enabled():
|
||||||
|
... print('GIL is enabled and should not be')
|
||||||
|
... else:
|
||||||
|
... print('okay')
|
||||||
|
okay
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
def run(args = None):
|
||||||
|
import sys
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
if args is not None:
|
||||||
|
sys.argv = args
|
||||||
|
return doctest.testmod(sys.modules.get(__name__))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("running...")
|
||||||
|
import sys
|
||||||
|
status = run()[0]
|
||||||
|
if (status == 0): print("Done.")
|
||||||
|
sys.exit(status)
|
||||||
Reference in New Issue
Block a user