gpt4 book ai didi

c++ - 在 boost python 中将 const wchar_t* 转换为 python 字符串

转载 作者:行者123 更新时间:2023-11-30 04:14:15 25 4
gpt4 key购买 nike

我试图弄清楚如何支持 const wchar_t* 作为 boost python 中公开的函数的返回类型。我的 boost 版本是 1.52,我正在使用 python 2.7,如果有任何不同的话。

不知怎么的,我无法让它接受我的转换函数。我在 Internet 上看到了解决此问题的片段,但没有任何实际可行的方法或说明如何正确执行此操作。

这是我简单的非工作示例:

#include <string>
#include <boost/python.hpp>
using namespace boost::python;


struct wchar_t_to_python_str
{
static PyObject* convert(const wchar_t* )
{
std::string s = "I'm more interested in the function signature than how to do wide char to non-wide char conversion";
return boost::python::incref(boost::python::object(s).ptr());
}
};


void init_module()
{
to_python_converter<const wchar_t*, wchar_t_to_python_str>();
}

const wchar_t* testWchar() {
return L"Hello World";
}
const char* testChar() {
return "Hello World";
}

BOOST_PYTHON_MODULE(test)
{
// This works nicely, const char* is supported
def("testChar", testChar);

// This doesn't work, fails with this error
// 'awBoost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<T>'
// def("testWchar", testWchar);

// Throwing in a return value policy fires a compile time assert make_instance_impl
// BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >));
// It seems like it gets confused by wchar_t not being a class, but it's hard to know
def("testWchar", testWchar, return_value_policy<reference_existing_object>());
}

最佳答案

这个问题有几个因素:

  • 使用 to_python_converter 启用的转换功能是运行时转换。
  • 未提供 Boost.Python builtin conversion支持 wchar*。它可能被忽略了,因为在更新 Boost.Python 以支持 Python 3 时添加了 std::wstring 支持。缺少内置转换支持导致内部类需要适当的 CallPolicy。在编译期间。均未提供 ResultConverterGenerator模型是这种转换的候选对象,因为它们主要影响对象所有权/生命周期而不是类型转换。

在这些限制条件下有两种方法:

下面是一个展示这两种方法的完整示例:

#include <string>
#include <boost/function_types/parameter_types.hpp>
#include <boost/python.hpp>

/// @brief ResultConverterGenerator used to transform wchar_t to PyObject.
struct wchar_result_converter
{
template <class T> struct apply
{
struct type
{
/// @brief Convert wchar_t to PyObject.
PyObject* operator()(const wchar_t* str) const
{
// Using the Python/C API may be slighly cleaner.
return PyUnicode_FromWideChar(str, wcslen(str));

// Alternatively, Boost.Python's object type can be used. While
// Boost.Python does not know how to convert wchar_t to an object,
// it does know how to convert std::wstring, so construct
// a temporary to help in the conversion.
// return boost::python::incref(
// boost::python::object(std::wstring(str)).ptr());
}

/// @brief Used for documentation.
const PyTypeObject* get_pytype() const { return 0; }
}; // struct type
}; // struct apply
};

/// @brief Modify the return type of a function using supplied CallPolicies.
template <typename ReturnType, typename Fn, typename Policy>
boost::python::object return_as(Fn fn, const Policy& policy)
{
// Build MPL type representing signature of function, injecting the
// explicitly provided return type.
typedef typename boost::mpl::push_front<
typename boost::function_types::parameter_types<Fn>::type,
ReturnType
>::type signature_type;

return boost::python::make_function(fn, policy, signature_type());
}

/// @brief Modify the return type of a function using default_call_policies.
template <typename ReturnType, typename Fn>
boost::python::object return_as(Fn fn)
{
return return_as<ReturnType>(fn, boost::python::default_call_policies());
}

// Test functions.
const char* testChar() { return "Hello World"; }
const wchar_t* testWchar() { return L"Hello World"; }

BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;

// Expose non-wide test char support.
python::def("testChar", testChar);

// Expose wide char support by:
// - providing a policy to convert the return value.
// - manipulating the return type.
python::def("testWchar1", &testWchar,
python::return_value_policy<wchar_result_converter>());
python::def("testWchar2", return_as<std::wstring>(&testWchar));
}

及其用法:

>>> import example
>>> a = example.testChar()
>>> print a
Hello World
>>> print type(a)
<type 'str'>
>>> b = example.testWchar1()
>>> print b
Hello World
>>> print type(b)
<type 'unicode'>
>>> c = example.testWchar2()
>>> print c
Hello World
>>> print type(c)
<type 'unicode'>

关于c++ - 在 boost python 中将 const wchar_t* 转换为 python 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com