繁体   English   中英

在Boost Python中使用Unicode,使用带有std :: wstring的C ++函数

[英]Using Unicode in Boost Python with a C++ function that takes std::wstring

我正在使用Boost Python库来包装我拥有的C ++类,以便我可以从Python调用它的方法。 我的C ++类Clazz有公共方法:

void doSomething(std::string& s) { ... }
void doSomethingWide(std::wstring& ws) { ... }

我创建了一个指向这两种方法的BOOST_PYTHON_MODULE 第一个使用std::string我可以调用。 但是,当我尝试使用Python Unicode字符串调用第二个时:

x = u'hello'
Clazz.doSomethingWide(x)

我收到错误:

ArgumentError: Python argument types in Clazz.doSomethingWide(Clazz, unicode) did not match C++ signature: doSomething(Clazz, std::wstring)

我曾希望unicode会自动与std::wstring接口,因为常规的Python字符串类型与std::string 但是,情况似乎并非如此。

在另一个帖子中,有人建议先进行转换:

x = str(x.encode('utf-8'))

但是,我正在处理非常大的字符串,这会破坏我的代码的性能,因为它是x的字符数O(n)

有修改,我试图与接口的C ++库的能力。 有没有办法以我可以使用它的方式将Python unicode类型传递到我的C ++库中? 我已经远程搜索了互联网,并发现了一些转换器和其他东西的引用,但实现它们并没有修复上面的错误信息(很可能我没有正确使用它们)。

简而言之,类型转换通常会产生rvalue对象,因此参数必须通过值或const引用接受。 因此,改变:

void doSomethingWide(std::wstring&);

以下任一项:

void doSomethingWide(std::wstring);
void doSomethingWide(const std::wstring&);

Boost.Python在11-SEP-2003上添加了std::wstring转换 作为一般规则,当在Boost.Python中发生类型转换时,结果对象将被视为右值。 boost::python::extract观察者规范中间接注意到此行为:

将存储的指针转换为result_type ,它是TT const&

如果支持左值转换,则可能会为某些类型引入笨拙的语义。 例如,C ++函数可以修改不可变的Python字符串。


这是一个完整的最小例子:

#include <iostream>
#include <string>
#include <boost/python.hpp>

class spam
{
public:
  void doSomething(const std::string& str)
  {
    std::cout << "spam::doSomething(): " << str << std::endl;
  }

  void doSomethingWide(const std::wstring& str)
  {
    std::wcout << "spam::doSomethingWide(): " << str << std::endl;
  }
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<spam>("Spam")
    .def("doSomething", &spam::doSomething)
    .def("doSomethingWide", &spam::doSomethingWide)
    ;
}

互动用法:

>>> import example
>>> spam = example.Spam()
>>> spam.doSomething("test")
spam::doSomething(): test
>>> spam.doSomethingWide(u"test")
spam::doSomethingWide(): test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM