2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

Added scope

[SVN r14593]
This commit is contained in:
Dave Abrahams
2002-07-25 02:23:01 +00:00
parent b7421fd5cd
commit f458dbdbcb
6 changed files with 139 additions and 30 deletions

View File

@@ -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

View File

@@ -11,7 +11,7 @@
# include <boost/python/make_function.hpp>
# include <boost/python/class_fwd.hpp>
# include <boost/python/detail/module_base.hpp>
# include <boost/python/detail/module_init.hpp>
# include <boost/python/module_init.hpp>
namespace boost { namespace python {

View File

@@ -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 <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# 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 <boost/python/detail/aix_init_module.hpp>
# 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

View File

@@ -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 <boost/python/object_core.hpp>
# include <boost/python/refcount.hpp>
# include <boost/utility.hpp>
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<scope>
: object_manager_traits<object>
{
};
}
}} // namespace boost::python
#endif // SCOPE_DWA2002724_HPP

View File

@@ -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

View File

@@ -9,13 +9,18 @@
#include <boost/python/detail/module_base.hpp>
#include <boost/python/object/function.hpp>
#include <boost/python/cast.hpp>
#include <boost/python/scope.hpp>
#include <boost/python/borrowed.hpp>
#include <boost/python/object.hpp>
#include <boost/python/detail/raw_pyobject.hpp>
namespace boost { namespace python { namespace detail {
module_base::module_base(const char* name)
: m_module(
python::borrowed(Py_InitModule(const_cast<char*>(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<char*>(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;
}}