From f458dbdbcbaf26f935fabb9eedb58a843a2425f5 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Thu, 25 Jul 2002 02:23:01 +0000 Subject: [PATCH] Added scope [SVN r14593] --- .../boost/python/detail/aix_init_module.hpp | 2 +- include/boost/python/module.hpp | 2 +- .../boost/python/{detail => }/module_init.hpp | 61 +++++++++++-------- include/boost/python/scope.hpp | 60 ++++++++++++++++++ src/aix_init_module.cpp | 3 +- src/module.cpp | 41 ++++++++++++- 6 files changed, 139 insertions(+), 30 deletions(-) rename include/boost/python/{detail => }/module_init.hpp (57%) create mode 100644 include/boost/python/scope.hpp diff --git a/include/boost/python/detail/aix_init_module.hpp b/include/boost/python/detail/aix_init_module.hpp index e73210d5..6eda62ea 100644 --- a/include/boost/python/detail/aix_init_module.hpp +++ b/include/boost/python/detail/aix_init_module.hpp @@ -16,7 +16,7 @@ extern "C" typedef PyObject* (*so_load_function)(char*,char*,FILE*); } -void aix_init_module(so_load_function, void (*init_module)()); +void aix_init_module(so_load_function, char const* name, void (*init_module)()); }}} // namespace boost::python::detail # endif diff --git a/include/boost/python/module.hpp b/include/boost/python/module.hpp index 5537171d..7a774496 100644 --- a/include/boost/python/module.hpp +++ b/include/boost/python/module.hpp @@ -11,7 +11,7 @@ # include # include # include -# include +# include namespace boost { namespace python { diff --git a/include/boost/python/detail/module_init.hpp b/include/boost/python/module_init.hpp similarity index 57% rename from include/boost/python/detail/module_init.hpp rename to include/boost/python/module_init.hpp index 5b5ce9f5..1d65308a 100644 --- a/include/boost/python/detail/module_init.hpp +++ b/include/boost/python/module_init.hpp @@ -3,48 +3,59 @@ // copyright notice appears in all copies. This software is provided // "as is" without express or implied warranty, and with no claim as // to its suitability for any purpose. -#ifndef MODULE_INIT_DWA2002529_HPP -# define MODULE_INIT_DWA2002529_HPP +#ifndef MODULE_INIT_DWA20020722_HPP +# define MODULE_INIT_DWA20020722_HPP + +# include +# include # ifndef BOOST_PYTHON_MODULE_INIT +namespace boost { namespace python { namespace detail { + +BOOST_PYTHON_DECL void init_module(char const* name, void(*)()); + +}}} + # if defined(_WIN32) || defined(__CYGWIN__) -# define BOOST_PYTHON_MODULE_INIT(name) \ -void init_module_##name(); \ -extern "C" __declspec(dllexport) void init##name() \ -{ \ - boost::python::handle_exception(&init_module_##name); \ -} \ +# define BOOST_PYTHON_MODULE_INIT(name) \ +void init_module_##name(); \ +extern "C" __declspec(dllexport) void init##name() \ +{ \ + boost::python::detail::init_module( \ + #name,&init_module_##name); \ +} \ void init_module_##name() # elif defined(_AIX) # include -# define BOOST_PYTHON_MODULE_INIT(name) \ -void init_module_##name(); \ -extern "C" \ -{ \ - extern PyObject* _PyImport_LoadDynamicModule(char*, char*, FILE *); \ - void init##name() \ - { \ - boost::python::detail::aix_init_module(_PyImport_LoadDynamicModule, &init_module_##name); \ - } \ -} \ +# define BOOST_PYTHON_MODULE_INIT(name) \ +void init_module_##name(); \ +extern "C" \ +{ \ + extern PyObject* _PyImport_LoadDynamicModule(char*, char*, FILE *); \ + void init##name() \ + { \ + boost::python::detail::aix_init_module( \ + _PyImport_LoadDynamicModule, #name, &init_module_##name); \ + } \ +} \ void init_module_##name() # else -# define BOOST_PYTHON_MODULE_INIT(name) \ -void init_module_##name(); \ -extern "C" void init##name() \ -{ \ - boost::python::handle_exception(&init_module_##name); \ -} \ +# define BOOST_PYTHON_MODULE_INIT(name) \ +void init_module_##name(); \ +extern "C" void init##name() \ +{ \ + boost::python::detail::init_module(#name, &init_module_##name); \ +} \ void init_module_##name() # endif # endif -#endif // MODULE_INIT_DWA2002529_HPP +#endif // MODULE_INIT_DWA20020722_HPP diff --git a/include/boost/python/scope.hpp b/include/boost/python/scope.hpp new file mode 100644 index 00000000..edcfe02a --- /dev/null +++ b/include/boost/python/scope.hpp @@ -0,0 +1,60 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +#ifndef SCOPE_DWA2002724_HPP +# define SCOPE_DWA2002724_HPP + +# include +# include +# include + +namespace boost { namespace python { + +class BOOST_PYTHON_DECL scope : public object, noncopyable +{ + public: + inline scope(object const&); + inline ~scope(); + static inline object get(); + + private: // data members + PyObject* m_previous_scope; + + private: // static members + + // Use a PyObject* to avoid problems with static destruction after Py_Finalize + static PyObject* current_scope; +}; + +inline scope::scope(object const& new_scope) + : object(new_scope) + , m_previous_scope(current_scope) +{ + current_scope = python::incref(new_scope.ptr()); +} + +inline scope::~scope() +{ + python::decref(current_scope); + current_scope = m_previous_scope; +} + +inline object scope::get() +{ + return object(detail::borrowed_reference(current_scope)); +} + +namespace converter +{ + template <> + struct object_manager_traits + : object_manager_traits + { + }; +} + +}} // namespace boost::python + +#endif // SCOPE_DWA2002724_HPP diff --git a/src/aix_init_module.cpp b/src/aix_init_module.cpp index 61422e6b..fb623579 100644 --- a/src/aix_init_module.cpp +++ b/src/aix_init_module.cpp @@ -98,6 +98,7 @@ namespace void aix_init_module( so_load_function load_dynamic_module + , char const* module_name , void (*init_module)()) { static bool initialized; @@ -133,7 +134,7 @@ void aix_init_module( initialized = true; } - python::handle_exception(init_module); + python::detail::init_module(module_name, init_module); } }}} // namespace boost::python diff --git a/src/module.cpp b/src/module.cpp index f7edf39e..dcb64397 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -9,13 +9,18 @@ #include #include #include +#include +#include +#include +#include namespace boost { namespace python { namespace detail { module_base::module_base(const char* name) : m_module( - python::borrowed(Py_InitModule(const_cast(name), initial_methods)) - ) + allow_null(python::borrowed( + scope::get().ptr() + ))) { } @@ -59,4 +64,36 @@ void module_base::add_class(type_handle const& class_obj) PyMethodDef module_base::initial_methods[] = { { 0, 0, 0, 0 } }; +namespace +{ + PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; +} + +BOOST_PYTHON_DECL void init_module(char const* name, void(*init_function)()) +{ + + PyObject* m + = Py_InitModule(const_cast(name), initial_methods); + + if (m != 0) + { + ; + + // Create the current module scope + scope current_module( + (object( + ((borrowed_reference_t*)m) + )) + ); + + handle_exception(init_function); + } +} + }}} // namespace boost::python::detail + +namespace boost { namespace python { + +BOOST_PYTHON_DECL PyObject* scope::current_scope; + +}}