用C语言扩展Python的功能的实例
用C語言擴展Python的功能的實例
分類: C/C++ 編程技巧 Programes 2008-04-23 09:31 1232人閱讀 評論(0)收藏 舉報 python擴展語言cmethodsnull請點擊這里查看關于用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']
總結
以上是生活随笔為你收集整理的用C语言扩展Python的功能的实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ggplot2绘图点的形状不够用怎么办?
- 下一篇: Python的C/C++扩展