Skip to content

Commit c86ea20

Browse files
o01egstefanseefeld
authored andcommitted
Fix and add tests for module state
1 parent afd27c6 commit c86ea20

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

include/boost/python/module_init.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
170170
# define _BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType, ...) \
171171
int BOOST_PP_CAT(exec_module_,name)(PyObject* mod) \
172172
{ \
173-
return boost::python::detail::exec_module( \
173+
return boost::python::detail::exec_module_with_state( \
174174
mod, reinterpret_cast<void(*)(void*)>(BOOST_PP_CAT(init_module_, name)) ); \
175175
} \
176176
extern "C" BOOST_SYMBOL_EXPORT PyObject* BOOST_PP_CAT(PyInit_, name)() \
@@ -244,7 +244,7 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
244244
# define _BOOST_PYTHON_MODULE_WITH_STATE_INIT(name, StateType) \
245245
int BOOST_PP_CAT(exec_module_,name)(PyObject* mod) \
246246
{ \
247-
return boost::python::detail::exec_module( \
247+
return boost::python::detail::exec_module_with_state( \
248248
mod, reinterpret_cast<void(*)(void*)>(BOOST_PP_CAT(init_module_, name)) ); \
249249
} \
250250
extern "C" BOOST_SYMBOL_EXPORT PyObject* BOOST_PP_CAT(PyInit_, name)() \

test/fabscript

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,7 @@ tests.append(extension_test("module_multi_phase",
180180
condition=python_version_major > 3 or (python_version_major == 3 and python_version_minor >= 5)))
181181
tests.append(extension_test("module_multi_phase_nogil",
182182
condition=python_version_major > 3 or (python_version_major == 3 and python_version_minor >= 5)))
183+
tests.append(extension_test("module_multi_phase_state",
184+
condition=python_version_major > 3 or (python_version_major == 3 and python_version_minor >= 5)))
183185

184186
default = report('report', tests, fail_on_failures=True)

test/module_multi_phase_state.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Distributed under the Boost Software License, Version 1.0. (See
2+
// accompanying file LICENSE_1_0.txt or copy at
3+
// http://www.boost.org/LICENSE_1_0.txt)
4+
5+
#include <boost/python/def.hpp>
6+
#include <boost/python/import.hpp>
7+
#include <boost/python/module.hpp>
8+
9+
using namespace boost::python;
10+
11+
struct TestState
12+
{
13+
int x;
14+
};
15+
16+
int get_state_x() {
17+
TestState* state = reinterpret_cast<TestState*>(PyModule_GetState(import("module_multi_phase_state_ext").ptr()));
18+
if (state != nullptr) {
19+
return state->x;
20+
}
21+
return -1;
22+
}
23+
24+
BOOST_PYTHON_MODULE_WITH_STATE(module_multi_phase_state_ext, TestState)
25+
{
26+
state->x = 42;
27+
def("x", get_state_x);
28+
}
29+
30+
#include "module_tail.cpp"

test/module_multi_phase_state.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Distributed under the Boost
2+
# Software License, Version 1.0. (See accompanying
3+
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4+
"""
5+
>>> import module_multi_phase_state_ext
6+
>>> module_multi_phase_state_ext.x()
7+
42
8+
"""
9+
10+
def run(args = None):
11+
import sys
12+
import doctest
13+
14+
if args is not None:
15+
sys.argv = args
16+
return doctest.testmod(sys.modules.get(__name__))
17+
18+
if __name__ == '__main__':
19+
print("running...")
20+
import sys
21+
status = run()[0]
22+
if (status == 0): print("Done.")
23+
sys.exit(status)

0 commit comments

Comments
 (0)