2011-11-03 143 views
4

使用SWIG爲C++應用程序生成一個Python接口,有什麼辦法讓它註釋生成的.py文件中的函數嗎?實際上,我在拉動整個.h文件中到.i文件,但對於一個小例子:SWIG:添加註釋到生成的.py文件

%module example 

bool do_something_interesting(int number, string text); 

swig -python example.i生成:

... 
def do_something_interesting(*args): 
    return _example.do_something_interesting(*args) 
do_something_interesting = _example.do_something_interesting 

理想,我會自動喜歡它使用原始方法簽名添加評論

#bool do_something_interesting(int number, string text) 
def do_something_interesting(*args): 
    return _example.do_something_interesting(*args) 
do_something_interesting = _example.do_something_interesting 

但是我會非常適合在某處(特別是如果評論可能以某種方式在.h文件中)。我認爲%pythonprepend可能是一個可能的解決方案,但它將代碼插入函數定義而不是之前。

編輯:這是我在.h文件中想到的。不是最漂亮的東西是永遠不會,但它會做:

#ifdef SWIG 
    %pythonprepend do_something_interesting(int, string) %{ 
    """Do some interesting thing 

    number:Int -- a number 
    text:String -- some text 

    """ 
    %} 
#endif 
bool do_something_interesting(int number, string text); 

回答