标签:
用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
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)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, “i|i”, &n1,&n2))
return NULL;
result = sub(n1,n2);
return Py_BuildValue(“i”, result);
}
PyObject wrap_mul(PyObject self, PyObject* args)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, “i|i”, &n1,&n2))
return NULL;
result = mul(n1,n2);
return Py_BuildValue(“i”, result);
}
PyObject wrap_div1(PyObject self, PyObject* args)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, “i|i”, &n1,&n2))
return NULL;
result = div1(n1,n2);
return Py_BuildValue(“i”, result);
}
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
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
标签:
原文地址:http://my.oschina.net/innovation/blog/380514