Skip to content

Commit f458dbd

Browse files
committed
Added scope
[SVN r14593]
1 parent b7421fd commit f458dbd

File tree

6 files changed

+139
-30
lines changed

6 files changed

+139
-30
lines changed

include/boost/python/detail/aix_init_module.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C"
1616
typedef PyObject* (*so_load_function)(char*,char*,FILE*);
1717
}
1818

19-
void aix_init_module(so_load_function, void (*init_module)());
19+
void aix_init_module(so_load_function, char const* name, void (*init_module)());
2020

2121
}}} // namespace boost::python::detail
2222
# endif

include/boost/python/module.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# include <boost/python/make_function.hpp>
1212
# include <boost/python/class_fwd.hpp>
1313
# include <boost/python/detail/module_base.hpp>
14-
# include <boost/python/detail/module_init.hpp>
14+
# include <boost/python/module_init.hpp>
1515

1616
namespace boost { namespace python {
1717

include/boost/python/detail/module_init.hpp renamed to include/boost/python/module_init.hpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,59 @@
33
// copyright notice appears in all copies. This software is provided
44
// "as is" without express or implied warranty, and with no claim as
55
// to its suitability for any purpose.
6-
#ifndef MODULE_INIT_DWA2002529_HPP
7-
# define MODULE_INIT_DWA2002529_HPP
6+
#ifndef MODULE_INIT_DWA20020722_HPP
7+
# define MODULE_INIT_DWA20020722_HPP
8+
9+
# include <boost/python/detail/wrap_python.hpp>
10+
# include <boost/python/detail/config.hpp>
811

912
# ifndef BOOST_PYTHON_MODULE_INIT
1013

14+
namespace boost { namespace python { namespace detail {
15+
16+
BOOST_PYTHON_DECL void init_module(char const* name, void(*)());
17+
18+
}}}
19+
1120
# if defined(_WIN32) || defined(__CYGWIN__)
1221

13-
# define BOOST_PYTHON_MODULE_INIT(name) \
14-
void init_module_##name(); \
15-
extern "C" __declspec(dllexport) void init##name() \
16-
{ \
17-
boost::python::handle_exception(&init_module_##name); \
18-
} \
22+
# define BOOST_PYTHON_MODULE_INIT(name) \
23+
void init_module_##name(); \
24+
extern "C" __declspec(dllexport) void init##name() \
25+
{ \
26+
boost::python::detail::init_module( \
27+
#name,&init_module_##name); \
28+
} \
1929
void init_module_##name()
2030

2131
# elif defined(_AIX)
2232

2333
# include <boost/python/detail/aix_init_module.hpp>
24-
# define BOOST_PYTHON_MODULE_INIT(name) \
25-
void init_module_##name(); \
26-
extern "C" \
27-
{ \
28-
extern PyObject* _PyImport_LoadDynamicModule(char*, char*, FILE *); \
29-
void init##name() \
30-
{ \
31-
boost::python::detail::aix_init_module(_PyImport_LoadDynamicModule, &init_module_##name); \
32-
} \
33-
} \
34+
# define BOOST_PYTHON_MODULE_INIT(name) \
35+
void init_module_##name(); \
36+
extern "C" \
37+
{ \
38+
extern PyObject* _PyImport_LoadDynamicModule(char*, char*, FILE *); \
39+
void init##name() \
40+
{ \
41+
boost::python::detail::aix_init_module( \
42+
_PyImport_LoadDynamicModule, #name, &init_module_##name); \
43+
} \
44+
} \
3445
void init_module_##name()
3546

3647
# else
3748

38-
# define BOOST_PYTHON_MODULE_INIT(name) \
39-
void init_module_##name(); \
40-
extern "C" void init##name() \
41-
{ \
42-
boost::python::handle_exception(&init_module_##name); \
43-
} \
49+
# define BOOST_PYTHON_MODULE_INIT(name) \
50+
void init_module_##name(); \
51+
extern "C" void init##name() \
52+
{ \
53+
boost::python::detail::init_module(#name, &init_module_##name); \
54+
} \
4455
void init_module_##name()
4556

4657
# endif
4758

4859
# endif
4960

50-
#endif // MODULE_INIT_DWA2002529_HPP
61+
#endif // MODULE_INIT_DWA20020722_HPP

include/boost/python/scope.hpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef SCOPE_DWA2002724_HPP
7+
# define SCOPE_DWA2002724_HPP
8+
9+
# include <boost/python/object_core.hpp>
10+
# include <boost/python/refcount.hpp>
11+
# include <boost/utility.hpp>
12+
13+
namespace boost { namespace python {
14+
15+
class BOOST_PYTHON_DECL scope : public object, noncopyable
16+
{
17+
public:
18+
inline scope(object const&);
19+
inline ~scope();
20+
static inline object get();
21+
22+
private: // data members
23+
PyObject* m_previous_scope;
24+
25+
private: // static members
26+
27+
// Use a PyObject* to avoid problems with static destruction after Py_Finalize
28+
static PyObject* current_scope;
29+
};
30+
31+
inline scope::scope(object const& new_scope)
32+
: object(new_scope)
33+
, m_previous_scope(current_scope)
34+
{
35+
current_scope = python::incref(new_scope.ptr());
36+
}
37+
38+
inline scope::~scope()
39+
{
40+
python::decref(current_scope);
41+
current_scope = m_previous_scope;
42+
}
43+
44+
inline object scope::get()
45+
{
46+
return object(detail::borrowed_reference(current_scope));
47+
}
48+
49+
namespace converter
50+
{
51+
template <>
52+
struct object_manager_traits<scope>
53+
: object_manager_traits<object>
54+
{
55+
};
56+
}
57+
58+
}} // namespace boost::python
59+
60+
#endif // SCOPE_DWA2002724_HPP

src/aix_init_module.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ namespace
9898

9999
void aix_init_module(
100100
so_load_function load_dynamic_module
101+
, char const* module_name
101102
, void (*init_module)())
102103
{
103104
static bool initialized;
@@ -133,7 +134,7 @@ void aix_init_module(
133134

134135
initialized = true;
135136
}
136-
python::handle_exception(init_module);
137+
python::detail::init_module(module_name, init_module);
137138
}
138139

139140
}}} // namespace boost::python

src/module.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
#include <boost/python/detail/module_base.hpp>
1010
#include <boost/python/object/function.hpp>
1111
#include <boost/python/cast.hpp>
12+
#include <boost/python/scope.hpp>
13+
#include <boost/python/borrowed.hpp>
14+
#include <boost/python/object.hpp>
15+
#include <boost/python/detail/raw_pyobject.hpp>
1216

1317
namespace boost { namespace python { namespace detail {
1418

1519
module_base::module_base(const char* name)
1620
: m_module(
17-
python::borrowed(Py_InitModule(const_cast<char*>(name), initial_methods))
18-
)
21+
allow_null(python::borrowed(
22+
scope::get().ptr()
23+
)))
1924
{
2025
}
2126

@@ -59,4 +64,36 @@ void module_base::add_class(type_handle const& class_obj)
5964

6065
PyMethodDef module_base::initial_methods[] = { { 0, 0, 0, 0 } };
6166

67+
namespace
68+
{
69+
PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } };
70+
}
71+
72+
BOOST_PYTHON_DECL void init_module(char const* name, void(*init_function)())
73+
{
74+
75+
PyObject* m
76+
= Py_InitModule(const_cast<char*>(name), initial_methods);
77+
78+
if (m != 0)
79+
{
80+
;
81+
82+
// Create the current module scope
83+
scope current_module(
84+
(object(
85+
((borrowed_reference_t*)m)
86+
))
87+
);
88+
89+
handle_exception(init_function);
90+
}
91+
}
92+
6293
}}} // namespace boost::python::detail
94+
95+
namespace boost { namespace python {
96+
97+
BOOST_PYTHON_DECL PyObject* scope::current_scope;
98+
99+
}}

0 commit comments

Comments
 (0)