我正在使用boost-python将遗留的C ++库与Python集成。遗留库具有一些全局初始化,然后其中的类使用应用程序范围的数据。我需要确保在销毁所有包装对象后调用遗留库的关闭功能,并认为这可以通过使用atexit注册关闭函数来实现。但是,我发现在atexit调用shutdown函数后正在清理包装的对象,导致遗留库中出现多个段错误!
我可以通过在退出之前调用包装对象上的del来实现所需的行为,但是希望将删除留给Python。我查看了documentation of object.__del__中的红色警告框,我想知道我的理想世界是否无法到达。
在将旧代码包装到python模块中时,在清除所有对象后调用关于确保关闭方法的任何建议吗?
一些平台细节,以防它们很重要:
最小代码:
#include <iostream>
#include <boost/python.hpp>
using namespace std;
namespace legacy
{
void initialize() { cout << "legacy::initialize" << endl; }
void shutdown() { cout << "legacy::shutdown" << endl; }
class Test
{
public:
Test();
virtual ~Test();
};
Test::Test() { }
Test::~Test() { cout << "legacy::Test::~Test" << endl; }
}
BOOST_PYTHON_MODULE(legacy)
{
using namespace boost::python;
legacy::initialize();
class_<legacy::Test>("Test");
def("_finalize", &legacy::shutdown);
object atexit = object(handle<>(PyImport_ImportModule("atexit")));
object finalize = scope().attr("_finalize");
atexit.attr("register")(finalize);
}
编译完成后,可以使用python运行,并显示以下输入和输出:
&GT;&GT;&GT;进口遗产 传统::初始化
&GT;&GT;&GT; test = legacy.Test()
&GT;&GT;&GT; ^ Z
传统::关机
遗留::测试::〜测试
答案 0 :(得分:2)
简而言之,创建一个保护类型,它将在其构造函数和析构函数中初始化和关闭旧库,然后通过每个公开对象中的智能指针来管理保护。
有一些微妙的细节可以使销毁过程变得非常困难:
Py_Finalize()
中模块中对象和对象的销毁顺序是随机的。要实现此目的,Boost.Python对象需要协调何时初始化和关闭旧API。这些对象还需要拥有使用旧API的旧对象的所有权。使用single responsibility principle,可以将职责划分为几个类。
可以使用resource acquisition is initialization(RAII)惯用法来初始化和关闭旧版AP。例如,使用以下legacy_api_guard
,在构造legacy_api_guard
对象时,它将初始化旧API。当legacy_api_guard
对象被破坏时,它将关闭旧API。
/// @brief Guard that will initialize or shutdown the legacy API.
struct legacy_api_guard
{
legacy_api_guard() { legacy::initialize(); }
~legacy_api_guard() { legacy::shutdown(); }
};
由于多个对象需要在何时初始化和关闭旧API时共享管理,因此可以使用智能指针(例如std::shared_ptr
)来负责管理防护。以下示例延迟初始化并关闭旧API:
/// @brief Global shared guard for the legacy API.
std::weak_ptr<legacy_api_guard> legacy_api_guard_;
/// @brief Get (or create) guard for legacy API.
std::shared_ptr<legacy_api_guard> get_api_guard()
{
auto shared = legacy_api_guard_.lock();
if (!shared)
{
shared = std::make_shared<legacy_api_guard>();
legacy_api_guard_ = shared;
}
return shared;
}
最后,嵌入到Boost.Python对象中的实际类型需要在创建旧对象的实例之前获取旧API保护的句柄。此外,在销毁后,应在旧对象销毁后释放旧版API防护。实现此目的的一种非侵入性方法是在将遗留类型公开给Boost.Python时使用提供自定义HeldType。在公开类型时,需要抑制默认的Boost.Python生成的初始值设定项,因为将使用自定义工厂函数来提供对对象创建的控制:
/// @brief legacy_object_holder is a smart pointer that will hold
/// legacy types and help guarantee the legacy API is initialized
/// while these objects are alive. This smart pointer will remain
/// transparent to the legacy library and the user-facing Python.
template <typename T>
class legacy_object_holder
{
public:
typedef T element_type;
template <typename... Args>
legacy_object_holder(Args&&... args)
: legacy_guard_(::get_api_guard()),
ptr_(std::make_shared<T>(std::forward<Args>(args)...))
{}
legacy_object_holder(legacy_object_holder& rhs) = default;
element_type* get() const { return ptr_.get(); }
private:
// Order of declaration is critical here. The guard should be
// allocated first, then the element. This allows for the
// element to be destroyed first, followed by the guard.
std::shared_ptr<legacy_api_guard> legacy_guard_;
std::shared_ptr<element_type> ptr_;
};
/// @brief Helper function used to extract the pointed to object from
/// an object_holder. Boost.Python will use this through ADL.
template <typename T>
T* get_pointer(const legacy_object_holder<T>& holder)
{
return holder.get();
}
/// Auxiliary function to make exposing legacy objects easier.
template <typename T, typename ...Args>
legacy_object_holder<T>* make_legacy_object(Args&&... args)
{
return new legacy_object_holder<T>(std::forward<Args>(args)...);
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<
legacy::Test, legacy_object_holder<legacy::Test>,
boost::noncopyable>("Test", python::no_init)
.def("__init__", python::make_constructor(
&make_legacy_object<legacy::Test>))
;
}
以下是一个完整的示例demonstrating,使用自定义HeldType非侵入式地保护资源并使用共享管理:
#include <iostream> // std::cout, std::endl
#include <memory> // std::shared_ptr, std::weak_ptr
#include <boost/python.hpp>
/// @brief legacy namespace that cannot be changed.
namespace legacy {
void initialize() { std::cout << "legacy::initialize()" << std::endl; }
void shutdown() { std::cout << "legacy::shutdown()" << std::endl; }
class Test
{
public:
Test() { std::cout << "legacy::Test::Test()" << std::endl; }
virtual ~Test() { std::cout << "legacy::Test::~Test()" << std::endl; }
};
void use_test(Test&) {}
} // namespace legacy
namespace {
/// @brief Guard that will initialize or shutdown the legacy API.
struct legacy_api_guard
{
legacy_api_guard() { legacy::initialize(); }
~legacy_api_guard() { legacy::shutdown(); }
};
/// @brief Global shared guard for the legacy API.
std::weak_ptr<legacy_api_guard> legacy_api_guard_;
/// @brief Get (or create) guard for legacy API.
std::shared_ptr<legacy_api_guard> get_api_guard()
{
auto shared = legacy_api_guard_.lock();
if (!shared)
{
shared = std::make_shared<legacy_api_guard>();
legacy_api_guard_ = shared;
}
return shared;
}
} // namespace
/// @brief legacy_object_holder is a smart pointer that will hold
/// legacy types and help guarantee the legacy API is initialized
/// while these objects are alive. This smart pointer will remain
/// transparent to the legacy library and the user-facing Python.
template <typename T>
class legacy_object_holder
{
public:
typedef T element_type;
template <typename... Args>
legacy_object_holder(Args&&... args)
: legacy_guard_(::get_api_guard()),
ptr_(std::make_shared<T>(std::forward<Args>(args)...))
{}
legacy_object_holder(legacy_object_holder& rhs) = default;
element_type* get() const { return ptr_.get(); }
private:
// Order of declaration is critical here. The guard should be
// allocated first, then the element. This allows for the
// element to be destroyed first, followed by the guard.
std::shared_ptr<legacy_api_guard> legacy_guard_;
std::shared_ptr<element_type> ptr_;
};
/// @brief Helper function used to extract the pointed to object from
/// an object_holder. Boost.Python will use this through ADL.
template <typename T>
T* get_pointer(const legacy_object_holder<T>& holder)
{
return holder.get();
}
/// Auxiliary function to make exposing legacy objects easier.
template <typename T, typename ...Args>
legacy_object_holder<T>* make_legacy_object(Args&&... args)
{
return new legacy_object_holder<T>(std::forward<Args>(args)...);
}
// Wrap the legacy::use_test function, passing the managed object.
void legacy_use_test_wrap(legacy_object_holder<legacy::Test>& holder)
{
return legacy::use_test(*holder.get());
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<
legacy::Test, legacy_object_holder<legacy::Test>,
boost::noncopyable>("Test", python::no_init)
.def("__init__", python::make_constructor(
&make_legacy_object<legacy::Test>))
;
python::def("use_test", &legacy_use_test_wrap);
}
交互式使用:
>>> import example
>>> test1 = example.Test()
legacy::initialize()
legacy::Test::Test()
>>> test2 = example.Test()
legacy::Test::Test()
>>> test1 = None
legacy::Test::~Test()
>>> example.use_test(test2)
>>> exit()
legacy::Test::~Test()
legacy::shutdown()
请注意,基本的整体方法也适用于非延迟解决方案,其中遗留API在导入模块时初始化。需要使用shared_ptr
而不是weak_ptr
,并使用atexit.register()
注册清理函数:
/// @brief Global shared guard for the legacy API.
std::shared_ptr<legacy_api_guard> legacy_api_guard_;
/// @brief Get (or create) guard for legacy API.
std::shared_ptr<legacy_api_guard> get_api_guard()
{
if (!legacy_api_guard_)
{
legacy_api_guard_ = std::make_shared<legacy_api_guard>();
}
return legacy_api_guard_;
}
void release_guard()
{
legacy_api_guard_.reset();
}
...
BOOST_PYTHON_MODULE(example)
{
// Boost.Python may throw an exception, so try/catch around
// it to initialize and shutdown legacy API on failure.
namespace python = boost::python;
try
{
::get_api_guard(); // Initialize.
...
// Register a cleanup function to run at exit.
python::import("atexit").attr("register")(
python::make_function(&::release_guard)
);
}
// If an exception is thrown, perform cleanup and re-throw.
catch (const python::error_already_set&)
{
::release_guard();
throw;
}
}
请参阅here进行演示。