Chinaunix首页 | 论坛 | 博客
  • 博客访问: 309500
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: Python/Ruby

2011-12-07 12:45:36

python的扩展 源文件

#include "Python.h"

 

static char py_gcd_doc[]="Computes the gcd of two integers";

static PyObject*

py_gcd(PyObject *self,PyObject *args) {

       int x,y,r;

       if (!PyArg_ParseTuple(args,"ii:gcd",&x,&y)) {

              return NULL;

       }

       while(x > 0) {

              r = x;

              x = y % x;

              y = r;

       }

       return Py_BuildValue("i",r);

}

 

static char py_distance_doc[]="Computes the distance between two poionts";

static PyObject *

py_distance(PyObject *self,PyObject *args) {

       PyErr_SetString(PyExc_NotImplementedError,"distance() not implemented.");

       return NULL;

}

 

static PyMethodDef _examplemethods[]={

{"gcd",py_gcd,METH_VARARGS,py_gcd_doc},

{"distance",py_distance,METH_VARARGS,py_distance_doc},

{NULL,NULL,0,NULL}

};

 

void init_example(void) {

       PyObject *mod;

       mod=Py_InitModule("_example",_examplemethods);

//     PyModule_AddIntMacro(mod,MAGIC);

}

 

 

编译

$ gcc -c -fpic -I/usr/local/include/python2.7   pyexample.c

其他目录下也有头文件Python.h,但如果在  -I/其他包含Python.h的目录,则会出错

生成动态库

$ gcc -shared pyexample.o  -o  _examplemodule.so

 

奇怪的是,可以不用pythoh的库文件libpython2.7.so    libpython2.7.a

 

结果:

>>> import _example

>>> _example.gcd(78,120)

6

>>> _example.distance()

Traceback (most recent call last):

  File "", line 1, in

NotImplementedError: distance() not implemented.

>>> 

 

 

 

例子2

#include "Python.h"

/*

typedef  struct {

   int x;

   int y;

}point;

*/

typedef  struct {

   double x;

   double y;

}point;

 

static char py_gcd_doc[]="Computes the gcd of two integers";

static PyObject*

py_gcd(PyObject *self,PyObject *args) {

       int x,y,r;

       if (!PyArg_ParseTuple(args,"ii:gcd",&x,&y)) {

              return NULL;

       }

       while(x > 0) {

              r = x;

              x = y % x;

              y = r;

       }

       return Py_BuildValue("i",r);

}

 

static char py_distance_doc[]="Computes the distance between two poionts";

 

 

int convers(PyObject *obj,PyObject *arg)

{

       point *p=(point *)arg;

       return PyArg_ParseTuple(obj,"ii",&p->x,&p->y);

}

 

/*  

static PyObject *

py_distance(PyObject *self,PyObject *args) {

//     PyErr_SetString(PyExc_NotImplementedError,"distance() not implemented.");

       point p1, p2;

       double r;

       PyArg_ParseTuple(args,"O&O&",convers, &p1,convers,&p2);

       r = sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));

       return Py_BuildValue("d",r); 

      

//     return NULL;

}

*/

PyObject *

py_distance(PyObject *self,PyObject *args) {

       point p1,p2;

       double r;

//     double re;

       if (!PyArg_ParseTuple(args,"(dd)(dd)",&p1.x,&p1.y,&p2.x,&p2.y) )

              return NULL;

       printf("%f,%f\n",p1.x,p1.y);

       printf("%f,%f\n",p2.x,p2.y);

       r = sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));

//     printf("re=%f\n",re);

//    r=5;  

       return Py_BuildValue("d",r);

}

 

static PyMethodDef _examplemethods[]={

{"gcd",py_gcd,METH_VARARGS,py_gcd_doc},

{"distance",py_distance,METH_VARARGS,py_distance_doc},

{NULL,NULL,0,NULL}

};

 

void init_example(void) {

       PyObject *mod;

       mod=Py_InitModule("_example",_examplemethods);

//     PyModule_AddIntMacro(mod,MAGIC);

}

 

编译

$ gcc -c -fpic -I/usr/local/include/python2.7   pyexample.c

$ gcc -shared pyexample.o  -o  _examplemodule.so

 

运行结果

>>> import _example

>>> _example.distance((0,0),(3,4))

0.000000,0.000000

3.000000,4.000000

5.0

 

注意

参数的数据类型要完全一致,否则会出现意想不到的错误。

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

python的内嵌

#include "Python.h"

 

int main(char argc,char **argv)

{

       char buf[256];

       PyObject *re;

       PyObject *re_comp;

       PyObject *arg;

       PyObject *search;

       PyObject *pat;

      

       if (argc != 2) {

              fprintf(stderr,"usage:%s pattern\n",argv[0]);

              exit(1);

       }

 

       Py_Initialize();

       re=PyImport_ImportModule("re");

       re_comp=PyObject_GetAttrString(re,"compile");

       arg=Py_BuildValue("(s)",argv[1]);

       pat = PyEval_CallObject(re_comp,arg);

       search=PyObject_GetAttrString(pat,"search");

      

      

       Py_DECREF(arg);

      

       while(fgets(buf,255,stdin)) {

              PyObject *match;

              arg=Py_BuildValue("(s)",buf);

              match=PyEval_CallObject(search,arg);

              Py_DECREF(arg);

              if (match != Py_None)

                     printf("%s",buf);

              Py_XDECREF(match);

       }

      

       Py_DECREF(re);

       Py_DECREF(re_comp);

       Py_DECREF(search);

       Py_DECREF(pat);

      

       Py_Finalize();

       return 0;      

}

 

编译

$ gcc  -I/usr/local/include/python2.7 pyemd.c    -lpython2.7

 

运行结果

[afa@localhost tmp_liyuan]$ ./a.out brooks.lee

hello

hello,brooks.lee

hello,brooks.lee

hello,brooks

my name is brooks.lee

my name is brooks.lee

 

阅读(560) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~