【发布时间】:2017-06-28 03:12:35
【问题描述】:
我有类似以下的内容:
swigtest.cpp
#include <string>
class Foo {
public:
std::string bar() {
return "baz";
}
};
inline Foo* new_foo() {
return new Foo;
}
swigtest.i
%module swigtest
%{
#include "swigtest.hpp"
%}
%include <std_string.i>
%include "swigtest.hpp"
%newobject new_foo;
useswig.py
from swigtest import new_foo
foo = new_foo()
print(foo.bar())
print(type(foo.bar()))
$ swig -c++ -python -modern swigtest.i $ g++ -fpic -shared -I/usr/include/python2.7 -DSWIG_PYTHON_2_UNICODE swigtest_wrap.cxx -lpython2.7 -o _swigtest.so $ python2 useswig.py baz <type 'str'>
有什么办法可以输出
<type 'unicode'>
相反?我从docs 了解到
当将 SWIG_PYTHON_2_UNICODE 宏添加到生成的代码中时...... Unicode 字符串将被成功接受并从 UTF-8 转换,但请注意它们作为普通 Python 2 字符串返回
有没有办法实现这一点,可能以某种方式使用自定义类型映射?
【问题讨论】:
-
感谢MCVE。我不得不为 Windows 稍微调整一下,但仍然 +1。
标签: python c++ python-2.7 unicode swig