python数组 swig_python中SWIG
Swig通過(guò)解析C頭文件并自動(dòng)創(chuàng)建擴(kuò)展代碼來(lái)操作。 要使用它,你先要有一個(gè)C頭文件。例如,我們示例的頭文件如下:
/* sample.h */
#include
extern int gcd(int, int);
extern int in_mandel(double x0, double y0, int n);
extern int divide(int a, int b, int *remainder);
extern double avg(double *a, int n);
typedef struct Point {
double x,y;
} Point;
extern double distance(Point *p1, Point *p2);
一旦你有了這個(gè)頭文件,下一步就是編寫(xiě)一個(gè)Swig”接口”文件。 按照約定,這些文件以”.i”后綴并且類似下面這樣:
// sample.i - Swig interface
%module sample
%{
#include "sample.h"
%}
/* Customizations */
%extend Point {
/* Constructor for Point objects */
Point(double x, double y) {
Point *p = (Point *) malloc(sizeof(Point));
p->x = x;
p->y = y;
return p;
};
};
/* Map int *remainder as an output argument */
%include typemaps.i
%apply int *OUTPUT { int * remainder };
/* Map the argument pattern (double *a, int n) to arrays */
%typemap(in) (double *a, int n)(Py_buffer view) {
view.obj = NULL;
if (PyObject_GetBuffer($input, &view, PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) {
SWIG_fail;
}
if (strcmp(view.format,"d") != 0) {
PyErr_SetString(PyExc_TypeError, "Expected an array of doubles");
SWIG_fail;
}
$1 = (double *) view.buf;
$2 = view.len / sizeof(double);
}
%typemap(freearg) (double *a, int n) {
if (view$argnum.obj) {
PyBuffer_Release(&view$argnum);
}
}
/* C declarations to be included in the extension module */
extern int gcd(int, int);
extern int in_mandel(double x0, double y0, int n);
extern int divide(int a, int b, int *remainder);
extern double avg(double *a, int n);
typedef struct Point {
double x,y;
} Point;
extern double distance(Point *p1, Point *p2);
一旦你寫(xiě)好了接口文件,就可以在命令行工具中調(diào)用Swig了:
bash % swig -python -py3 sample.i
bash %
swig的輸出就是兩個(gè)文件,sample_wrap.c和sample.py。 后面的文件就是用戶需要導(dǎo)入的。 而sample_wrap.c文件是需要被編譯到名叫 _sample 的支持模塊的C代碼。 這個(gè)可以通過(guò)跟普通擴(kuò)展模塊一樣的技術(shù)來(lái)完成。 例如,你創(chuàng)建了一個(gè)如下所示的 setup.py 文件:
# setup.py
from distutils.core import setup, Extension
setup(name='sample',
py_modules=['sample.py'],
ext_modules=[
Extension('_sample',
['sample_wrap.c'],
include_dirs = [],
define_macros = [],
undef_macros = [],
library_dirs = [],
libraries = ['sample']
)
]
)
要編譯和測(cè)試,在setup.py上執(zhí)行python3,如下:
bash % python3 setup.py build_ext --inplace
running build_ext
building '_sample' extension
gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
-I/usr/local/include/python3.3m -c sample_wrap.c
-o build/temp.macosx-10.6-x86_64-3.3/sample_wrap.o
sample_wrap.c: In function ‘SWIG_InitializeModule’:
sample_wrap.c:3589: warning: statement with no effect
gcc -bundle -undefined dynamic_lookup build/temp.macosx-10.6-x86_64-3.3/sample.o
build/temp.macosx-10.6-x86_64-3.3/sample_wrap.o -o _sample.so -lsample
bash %
如果一切正常的話,你會(huì)發(fā)現(xiàn)你就可以很方便的使用生成的C擴(kuò)展模塊了。例如:
>>> import sample
>>> sample.gcd(42,8)
2
>>> sample.divide(42,8)
[5, 2]
>>> p1 = sample.Point(2,3)
>>> p2 = sample.Point(4,5)
>>> sample.distance(p1,p2)
2.8284271247461903
>>> p1.x
2.0
>>> p1.y
3.0
>>> import array
>>> a = array.array('d',[1,2,3])
>>> sample.avg(a)
2.0
>>>
總結(jié)
以上是生活随笔為你收集整理的python数组 swig_python中SWIG的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android开发 文件分享到应用,An
- 下一篇: C语言编程基础 打印图形