鍍金池/ 教程/ Python/ Python/C API
<code>open</code>函數(shù)
Python 2系列版本
可迭代對象(Iterable)
異常
在函數(shù)中嵌入裝飾器
你的第一個(gè)裝飾器
上下文管理器(Context managers)
<code>set</code>(集合)數(shù)據(jù)結(jié)構(gòu)
裝飾器類
字典推導(dǎo)式(<code>dict</code> comprehensions)
<code>Reduce</code>
捐贈名單
<code>Filter</code>
<code>try/else</code>從句
*args 的用法
<code>dir</code>
處理異常
<code>else</code>從句
對象自省
For - Else
18. 一行式
Python 3.2及以后版本
Global和Return
基于類的實(shí)現(xiàn)
容器(<code>Collections</code>)
23. 協(xié)程
推薦閱讀
譯者后記
<code>*args</code> 和 <code>**kwargs</code>
**kwargs 的用法
生成器(Generators)
迭代(Iteration)
基于生成器的實(shí)現(xiàn)
將函數(shù)作為參數(shù)傳給另一個(gè)函數(shù)
日志(Logging)
三元運(yùn)算符
<code>inspect</code>模塊
枚舉
Map,F(xiàn)ilter 和 Reduce
各種推導(dǎo)式(comprehensions)
從函數(shù)中返回函數(shù)
列表推導(dǎo)式(<code>list</code> comprehensions)
處理多個(gè)異常
帶參數(shù)的裝飾器
對象變動(Mutation)
22. 目標(biāo)Python2+3
迭代器(Iterator)
虛擬環(huán)境(virtualenv)
<code>__slots__</code>魔法
什么時(shí)候使用它們?
Python/C API
<code>Map</code>
SWIG
授權(quán)(Authorization)
裝飾器
一切皆對象
使用C擴(kuò)展
使用 <code>*args</code> 和 <code>**kwargs</code> 來調(diào)用函數(shù)
17. <code>lambda</code>表達(dá)式
集合推導(dǎo)式(<code>set</code> comprehensions)
<code>type</code>和<code>id</code>
在函數(shù)中定義函數(shù)
<code>finally</code>從句
CTypes
調(diào)試(Debugging)
使用場景
生成器(Generators)
多個(gè)return值
關(guān)于原作者
函數(shù)緩存 (Function caching)
Python進(jìn)階

Python/C API

Python/C API可能是被最廣泛使用的方法。它不僅簡單,而且可以在C代碼中操作你的Python對象。

這種方法需要以特定的方式來編寫C代碼以供Python去調(diào)用它。所有的Python對象都被表示為一種叫做PyObject的結(jié)構(gòu)體,并且Python.h頭文件中提供了各種操作它的函數(shù)。例如,如果PyObject表示為PyListType(列表類型)時(shí),那么我們便可以使用PyList_Size()函數(shù)來獲取該結(jié)構(gòu)的長度,類似Python中的len(list)函數(shù)。大部分對Python原生對象的基礎(chǔ)函數(shù)和操作在Python.h頭文件中都能找到。

示例

編寫一個(gè)C擴(kuò)展,添加所有元素到一個(gè)Python列表(所有元素都是數(shù)字)

來看一下我們要實(shí)現(xiàn)的效果,這里演示了用Python調(diào)用C擴(kuò)展的代碼

#Though it looks like an ordinary python import, the addList module is implemented in C
import addList

l = [1,2,3,4,5]
print "Sum of List - " + str(l) + " = " +  str(addList.add(l))

上面的代碼和普通的Python文件并沒有什么分別,導(dǎo)入并使用了另一個(gè)叫做addList的Python模塊。唯一差別就是這個(gè)模塊并不是用Python編寫的,而是C。

接下來我們看看如何用C編寫addList模塊,這可能看起來有點(diǎn)讓人難以接受,但是一旦你了解了這之中的各種組成,你就可以一往無前了。

//Python.h has all the required function definitions to manipulate the Python objects
#include <Python.h>

//This is the function that is called from your python code
static PyObject* addList_add(PyObject* self, PyObject* args){

    PyObject * listObj;

    //The input arguments come as a tuple, we parse the args to get the various variables
    //In this case it's only one list variable, which will now be referenced by listObj
    if (! PyArg_ParseTuple( args, "O", &listObj ))
        return NULL;

    //length of the list
    long length = PyList_Size(listObj);

    //iterate over all the elements
    int i, sum =0;
    for (i = 0; i < length; i++) {
        //get an element out of the list - the element is also a python objects
        PyObject* temp = PyList_GetItem(listObj, i);
        //we know that object represents an integer - so convert it into C long
        long elem = PyInt_AsLong(temp);
        sum += elem;
    }

    //value returned back to python code - another python object
    //build value here converts the C long to a python integer
    return Py_BuildValue("i", sum);

}

//This is the docstring that corresponds to our 'add' function.
static char addList_docs[] =
"add(  ): add all elements of the list\n";

/* This table contains the relavent info mapping -
   <function-name in python module>, <actual-function>,
   <type-of-args the function expects>, <docstring associated with the function>
 */
static PyMethodDef addList_funcs[] = {
    {"add", (PyCFunction)addList_add, METH_VARARGS, addList_docs},
    {NULL, NULL, 0, NULL}

};

/*
   addList is the module name, and this is the initialization block of the module.
   <desired module name>, <the-info-table>, <module's-docstring>
 */
PyMODINIT_FUNC initaddList(void){
    Py_InitModule3("addList", addList_funcs,
            "Add all ze lists");

}

逐步解釋

  • Python.h頭文件中包含了所有需要的類型(Python對象類型的表示)和函數(shù)定義(對Python對象的操作)
  • 接下來我們編寫將要在Python調(diào)用的函數(shù), 函數(shù)傳統(tǒng)的命名方式由{模塊名}_{函數(shù)名}組成,所以我們將其命名為addList_add
  • 然后填寫想在模塊內(nèi)實(shí)現(xiàn)函數(shù)的相關(guān)信息表,每行一個(gè)函數(shù),以空行作為結(jié)束
  • 最后的模塊初始化塊簽名為PyMODINIT_FUNC init{模塊名}。

函數(shù)addList_add接受的參數(shù)類型為PyObject類型結(jié)構(gòu)(同時(shí)也表示為元組類型,因?yàn)镻ython中萬物皆為對象,所以我們先用PyObject來定義)。傳入的參數(shù)則通過PyArg_ParseTuple()來解析。第一個(gè)參數(shù)是被解析的參數(shù)變量。第二個(gè)參數(shù)是一個(gè)字符串,告訴我們?nèi)绾稳ソ馕鲈M中每一個(gè)元素。字符串的第n個(gè)字母正是代表著元組中第n個(gè)參數(shù)的類型。例如,"i"代表整形,"s"代表字符串類型, "O"則代表一個(gè)Python對象。接下來的參數(shù)都是你想要通過PyArg_ParseTuple()函數(shù)解析并保存的元素。這樣參數(shù)的數(shù)量和模塊中函數(shù)期待得到的參數(shù)數(shù)量就可以保持一致,并保證了位置的完整性。例如,我們想傳入一個(gè)字符串,一個(gè)整數(shù)和一個(gè)Python列表,可以這樣去寫

int n;
char *s;
PyObject* list;
PyArg_ParseTuple(args, "siO", &n, &s, &list);

在這種情況下,我們只需要提取一個(gè)列表對象,并將它存儲在listObj變量中。然后用列表對象中的PyList_Size()函數(shù)來獲取它的長度。就像Python中調(diào)用len(list)。

現(xiàn)在我們通過循環(huán)列表,使用PyList_GetItem(list, index)函數(shù)來獲取每個(gè)元素。這將返回一個(gè)PyObject*對象。既然Python對象也能表示PyIntType,我們只要使用PyInt_AsLong(PyObj *)函數(shù)便可獲得我們所需要的值。我們對每個(gè)元素都這樣處理,最后再得到它們的總和。

總和將被轉(zhuǎn)化為一個(gè)Python對象并通過Py_BuildValue()返回給Python代碼,這里的i表示我們要返回一個(gè)Python整形對象。

現(xiàn)在我們已經(jīng)編寫完C模塊了。將下列代碼保存為setup.py

#build the modules

from distutils.core import setup, Extension

setup(name='addList', version='1.0',  \
      ext_modules=[Extension('addList', ['adder.c'])])

并且運(yùn)行

python setup.py install

現(xiàn)在應(yīng)該已經(jīng)將我們的C文件編譯安裝到我們的Python模塊中了。

在一番辛苦后,讓我們來驗(yàn)證下我們的模塊是否有效

#module that talks to the C code
import addList

l = [1,2,3,4,5]
print "Sum of List - " + str(l) + " = " +  str(addList.add(l))

輸出結(jié)果如下

Sum of List - [1, 2, 3, 4, 5] = 15

如你所見,我們已經(jīng)使用Python.h API成功開發(fā)出了我們第一個(gè)Python C擴(kuò)展。這種方法看似復(fù)雜,但你一旦習(xí)慣,它將變的非常有效。

Python調(diào)用C代碼的另一種方式便是使用Cython讓Python編譯的更快。但是Cython和傳統(tǒng)的Python比起來可以將它理解為另一種語言,所以我們就不在這里過多描述了。