我使用SWIG和Numpy。我定義了一個名爲inplace()
的C函數來快速處理數據數組,並且我想做一些錯誤檢查(如果兩個數組具有相同的維數)。%在swig中重命名和%inline,錯誤檢查
我在.i
文件中使用%rename
和%inline
。據我所知,重命名映射函數名稱',所以每次有人使用inplace
,safe_inplace
運行並檢查錯誤。 。
但它不工作:(據我注意到,不執行safe_inplace,蟒蛇直接inplace
運行,而無需觸摸功能的安全版本
# .i
%include "inplace.h"
%rename (inplace) safe_inplace;
%inline %{
void safe_inplace(int* datain, int in_dx, int in_dy, int in_dz,
int* dataout, int out_dx, int out_dy)
{
if ((in_dx != out_dx) || (in_dy != out_dy)) {
PyErr_Format(PyExc_ValueError, /*... messgage*/)
return;
}
inplace(/* .. pass the arguments to original function*/);
}
頭文件:
# .h
void inplace(int* datain, int in_dx, int in_dy, int in_dz, int* dataout, int out_dx, int out_dy);
的Python:
#.py
inplace.inplace(a,b)
原始示例,我修改ç被發現here
謝謝!這裏的問題:爲什麼在'unsafe_foo'的例子中包含兩次'test.h'?不,這意味着'foo'既可以作爲'foo'又可以作爲'unsafe_foo'(開銷)? – 2012-01-13 10:17:56
@JakubM。 nope,它不會在那裏出現兩次,一個'#include',在一個'%{''%}'對內,另一個是'%include'。後者告訴SWIG將文件的內容導入.i文件(與C或C++中的#include完全相同)。前者告訴SWIG,生成的包裝文件(本例中爲test_wrap.cxx)需要逐字地包含該確切代碼,也就是說test_wrap.cxx將包含確切的'#include',否則''% {%}'對SWIG本身沒有影響。 – Flexo 2012-01-13 10:27:22