请点击这里查看关于用C语言扩展Python的功能。
只要安装了Python,在用C进行Python的扩展编程时不需要额外安装任何东西,Python会将头文件防置于/usr/include/python下,根据不同的版本稍有不同。
下面给出一个例程,它将产生一个可以被python导入的模块,名为Example,其中包含一个splitwords的函数,这个函数接受两个参数,第一个是包含单词的字符串,第二个是单词的分隔符,这也是一个字符串,其中的每个字符都会用来作为分割单词的字符。为方便起见,所有的函数都放置于一个文件Example.c中。
// Filename Example.c
#include <Python.h>
PyObject* splitwords(const char *sz, const char *sp)
{
// Python Objects
PyObject *List = PyList_New(0);
if (NULL == List || NULL == sz)
return List;
if (NULL == sp) {
PyList_Append(List, Py_BuildValue("s", sz));
return List;
}
// Split words
const char *pbgn=NULL, *pend;
size_t begin=0, end;
size_t len = strlen(sz);
char buf[0x20];
while (begin < len) {
if (strchr(sp, sz[begin])) {
// String begins w/ 'sp'
++begin;
continue;
}
else {
size_t loc = begin;
while (loc < len) {
if (strchr(sp, sz[loc])) {
// Word stops here.
end = loc;
break;
}
else {
++loc;
continue;
}
}
if (loc == len) end = len;
size_t l = end - begin;
if (l >= 0x20) l = 0x19;
strncpy(buf, sz+begin, l);
buf[l] = '/0';
//puts(buf);
PyList_Append(List, Py_BuildValue("s", buf));
// Next word
begin = end;
}
}
return List;
}
PyObject* wrap_splitwords(PyObject *self, PyObject *args)
{
const char *sp, *sz;
if (!PyArg_ParseTuple(args, "ss", &sp, &sz))
return NULL;
return (splitwords(sp, sz));
}
static PyMethodDef Methods[] = {
{"splitwords", wrap_splitwords, METH_VARARGS, "Split Words"},
{NULL, NULL, 0, NULL}
};
void initExample()
{
PyObject *m;
m = Py_InitModule("Example", Methods);
}
这里产生的Python对象返回给Python,所以就不需要考虑引用计数的问题了。splitwords为C语言的扩展函数,用wrap_splitwords封装,Example中的方法列表由Methods给出,初始化函数为initExample,只要看过上面的参考链接,这些都不需再解释。
由于我的python版本是2.4.4,它的头文件目录位于/usr/include/python2.4下。使用gcc来编译,不要使用g++,以这个Example为例,由gcc产生的so中可以找到'initExample'符号,而g++产生的为'_Z15initExamplev',python在import时会出现找不到初始化函数的错误:
ImportError: dynamic module does not define init function (initExample)
如果一定要使用g++,或许可以将C++的扩展程序单独放到一个文件,从中产生一个静态库文件,而封装的程序仍使用gcc。这个设想还没有试验过,下面仅使用gcc:
gcc -fpic -c -I/usr/include/python2.4 -I/usr/lib/python2.4/config Example.c
gcc -shared -o Example.so Example.o
只要Example.so可以被找到(在python运行的当前目录,或者是在/usr/lib下,或者由LD_LIBRARY_PATH指定)python就可以直接import:
>>> import Example
>>> s = 'ab,cde:ghij klmno'
>>> Example.splitwords(s, ' ')
['ab,cde:ghij', 'klmno']
>>> Example.splitwords(s, ' :')
['ab,cde', 'ghij', 'klmno']
>>> Example.splitwords(s, ' :,')
['ab', 'cde', 'ghij', 'klmno']