使用SWIG包装的C++类在打印时不调用__str__()
我正在尝试打印我用SWIG为Python封装的C++类。我已经按照文档和这个问题的指引进行了操作:如何在Python中将swig矩阵对象转换为字符串
我已经定义了一个扩展的 __str__
函数,但在Python中打印这个对象时并没有被调用。让我给你一个简单的例子:
TestClass.h
#include <iostream>
class TestClass{
private:
int my_int;
public:
TestClass():
my_int(0)
{}
friend std::ostream& operator<< (std::ostream& o, TestClass const& t){
o<< "TestClass: " << t.my_int;
return o;
}
};
TestClass.cpp
#include "TestClass.h"
int main(){
using namespace std;
TestClass t;
cout << t << endl;
}
TestClass.i
%define __STR__() \
const char* __str__() {
std::cout << *$self << std::endl;
std::ostringstream out;
out << *$self; \
return out.str().c_str();
}
const char* __unicode__() {
std::cout << "unicode: " << *$self << std::endl;
std::ostringstream out;
out << *$self; \
return out.str().c_str();
}
%enddef
%extend TestClass{
__STR__()
};
CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (battery_lib_cpp)
set(CMAKE_VERBOSE_MAKEFILE 1)
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
SET(CMAKE_SWIG_FLAGS "-Wall")
#look for Pythonlibs
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET_SOURCE_FILES_PROPERTIES(TestClass.i PROPERTIES CPLUSPLUS ON)
SET_PROPERTY(SOURCE TestClass.i PROPERTY SWIG_FLAGS "-builtin")
SWIG_ADD_MODULE(TestClass python TestClass.i TestClass.cpp)
在构建完成后(运行cmake .和make),我得到了以下结果:
matthias@rp3deb:~/dvl/swig_str_minimal$ python
Python 2.7.8 (default, Jul 26 2014, 15:25:14)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import TestClass
>>> t = TestClass.TestClass()
>>> print t
<Swig Object of type 'TestClass *' at 0x7f1dffcb2b90>
>>> print t.__str__()
TestClass: 0
TestClass: 0
>>> print t.__str__
<built-in method __str__ of TestClass object at 0x7f1dffcb2b90>
另一个关于 __unicode__()
的测试
>>> import TestClass
>>> t = TestClass.TestClass()
>>> print str(t)
<Swig Object of type 'TestClass *' at 0x7f6584ae4b58>
>>> print t
<Swig Object of type 'TestClass *' at 0x7f6584ae4b58>
>>> print t.__unicode__()
unicode: TestClass: 0
TestClass: 0
>>> print t.__unicode__
<built-in method __unicode__ of TestClass object at 0x7f6584ae4b58>
我期望的输出是,当我执行“print t”时,能够隐式调用 __str__()
。我漏掉了什么呢?
2 个回答
1
在Flexo的回答基础上,这让我实现了我想要的功能(源文件和cmake没有改变)。
测试类文件:TestClass.i
%module TestClass
%{
#include <sstream>
#include "TestClass.h"
%}
%include "TestClass.h"
//http://stackoverflow.com/questions/2548779/how-to-stringfy-a-swig-matrix-object-in-python
%define __STR__(class_name)
%feature("python:slot", "tp_str", functype="reprfunc") class_name::py_to_string();
%extend class_name{
const char* py_to_string() {
std::ostringstream out;
out << *$self;
return out.str().c_str();
}
}
%enddef
__STR__(TestClass);
测试:
matthias@rp3deb:~/dvl/swig_str_minimal$ python -c "import TestClass; print TestClass.TestClass()"
TestClass: 0
3
要让 str(x)
调用你自己写的 C++ 代码,前提是这个对象是用 swig -python -builtin
生成的 SWIG 包装对象,你需要使用合适的插槽来注册你的函数。这可以通过在 SWIG 中使用 %feature("python:slot", ...)
来实现,例如:
%module test
%include <std_string.i>
%feature("python:slot", "tp_str", functype="reprfunc") foo::as_string;
%inline %{
struct foo {
std::string as_string() const { return "Hello world"; }
};
%}
在 SWIG 2.0 和 Python 2.7 中,我可以运行:
import test
print str(test.foo())
print repr(test.foo())
这会得到:
Hello world
<Swig Object of type 'foo *' at 0xb727ac40>
插槽可以快速处理标准对象的函数调用,这也是 -builtin
带来的一个重要好处。
你可以查看完整的 可用插槽列表及其类型,还有对应的 SWIG 2.0 文档。