日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

用C语言扩展Python的功能的实例

發布時間:2025/3/15 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用C语言扩展Python的功能的实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用C語言擴展Python的功能的實例

分類: C/C++ 編程技巧 Programes 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']

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的用C语言扩展Python的功能的实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。