用c语言实现python的扩展模块
示例1:
1 example.c
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
return a -b;
int mul(int a,int b)
return a*b;
int div1(int a,int b)
if(0 == b)
{
return b;
}
return a/b;
2 wrap.c
#include <python.h> //python.h中已经包含了常用的头文件
pyobject* wrap_add(pyobject* self, pyobject* args)
int n1,n2, result;
if (! pyarg_parsetuple(args, "i|i", &n1,&n2))
return null;
result = add(n1,n2);
return py_buildvalue("i", result);
pyobject* wrap_sub(pyobject* self, pyobject* args)
result = sub(n1,n2);
pyobject* wrap_mul(pyobject* self, pyobject* args)
result = mul(n1,n2);
pyobject* wrap_div1(pyobject* self, pyobject* args)
result = div1(n1,n2);
static pymethoddef examplemethods[] =
{"add", wrap_add, meth_varargs, "caculate 1!"},
{"sub", wrap_sub, meth_varargs, "caculate 2!"},
{"mul", wrap_mul, meth_varargs, "caculate 3!"},
{"div1", wrap_div1, meth_varargs, "caculate 4!"},
{null, null,0,null}
};
void initexample()
pyobject* m;
m = py_initmodule("example", examplemethods);
3 编译
gcc -fpic -c -i /usr/include/python2.6/ -i /usr/lib/python2.6/config example.c wrap.c
gcc -shared -o wrap.so wrap.o
4 功能演示截图
5 主要参考:
5.1 python的c语言扩展
http://www.ibm.com/developerworks/cn/linux/l-pythc/
5.2 浅谈 python 程序和 c 程序的整合
http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/index.html?ca=drs-cn-0506
示例2:(支持回调python中定义的函数)
1 mytest.c
#include <python.h>
static pyobject *my_callback = null;
static pyobject* my_strlen(pyobject *self, pyobject *args)
char *string;
int len;
if (!pyarg_parsetuple(args, "s", &string))
return null;
len = strlen(string);
return py_buildvalue("i", len);
static pyobject* my_strcat(pyobject *self, pyobject *args)
char* string1;
char* string2;
char* newstring;
if (!pyarg_parsetuple(args, "s|s", &string1, &string2))
return null;
newstring = strcat(string1, string2);
return py_buildvalue("s", newstring);
static pyobject* my_set_callback(pyobject *self, pyobject *args)
pyobject *result = null;
pyobject *temp;
if (pyarg_parsetuple(args, "o", &temp))
{
if (!pycallable_check(temp))
pyerr_setstring(pyexc_typeerror, "parameter must be callable");
return null;
}
py_xincref(temp);
py_xdecref(my_callback);
my_callback = temp;
py_incref(py_none);
result = py_none;
}
return result;
static pyobject* my_test_callback(pyobject *self, pyobject *args)
pyobject * arglist;
pyobject * result = null;
result = pyeval_callobject(my_callback, args);
if (result == null)
{
return null;
}
py_decref(result);
py_incref(py_none);
return py_none;
static pymethoddef mytestmethods[] =
{"mystrlen", my_strlen, meth_varargs, "we test strlen of c"},
{"mystrcat", my_strcat, meth_varargs, "we test strcat of c"},
{"mysetcallback", my_set_callback, meth_varargs, "we set a call back function so that call it in c"},
{"mytestcallback", my_test_callback, meth_varargs, "we use this function to test call back function"},
{null, null, 0, null}
void initmytest()
(void) py_initmodule("mytest", mytestmethods);
2 编译
gcc -fpic -c -i /usr/include/python2.6/ -i /usr/lib/python2.6/config mytest.c
gcc -shared -o mytest.so mytest.o
3 功能演示截图
4 主要参考
4.1 . calling python functions from c
4.2 在windows上扩展python
http://blog.chinaunix.net/space.php?uid=46552&do=blog&id=2116527
4.3 [精华] 在windows上扩展python(2)--从c中调用python函数
http://www.linuxforum.net/forum/gshowthreaded.php?cat=&board=python&number=485550&page=3&view=collapsed&sb=5&o=all&vc=1