日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python数组 swig_python中SWIG

發(fā)布時間:2025/3/15 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数组 swig_python中SWIG 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Swig通過解析C頭文件并自動創(chuàng)建擴展代碼來操作。 要使用它,你先要有一個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);

一旦你有了這個頭文件,下一步就是編寫一個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);

一旦你寫好了接口文件,就可以在命令行工具中調(diào)用Swig了:

bash % swig -python -py3 sample.i

bash %

swig的輸出就是兩個文件,sample_wrap.c和sample.py。 后面的文件就是用戶需要導(dǎo)入的。 而sample_wrap.c文件是需要被編譯到名叫 _sample 的支持模塊的C代碼。 這個可以通過跟普通擴展模塊一樣的技術(shù)來完成。 例如,你創(chuàng)建了一個如下所示的 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']

)

]

)

要編譯和測試,在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 %

如果一切正常的話,你會發(fā)現(xiàn)你就可以很方便的使用生成的C擴展模塊了。例如:

>>> 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的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。