1.使用C擴展
CPython還為開發者實現了一個有趣的特性,使用Python可以輕松調用C代碼
開發者有三種方法可以在自己的Python代碼中來調用C編寫的函數-ctypes,SWIG,Python/C API。每種方式也都有各自的利弊。
首先,我們要明確為什么要在Python中調用C?
常見原因如下: - 你要提升代碼的運行速度,而且你知道C要比Python快50倍以上 - C語言中有很多傳統類庫,而且有些正是你想要的,但你又不想用Python去重寫它們 - 想對從內存到文件接口這樣的底層資源進行訪問 - 不需要理由,就是想這樣做
2.CTypes
Python中的ctypes模塊可能是Python調用C方法中最簡單的一種。ctypes模塊提供了和C語言兼容的數據類型和函數來加載dll文件,因此在調用時不需對源文件做任何的修改。也正是如此奠定了這種方法的簡單性。
示例如下
實現兩數求和的C代碼,保存為add.c
//sample C file to add 2 numbers - int and floats
#include <stdio.h> int add_int(int, int); float add_float(float, float); int add_int(int num1, int num2){ return num1 + num2; } float add_float(float num1, float num2){ return num1 + num2; }
接下來將C文件編譯為.so文件(windows下為DLL)。下面操作會生成adder.so文件
#For Linux $ gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c #For Mac $ gcc -shared -Wl,-install_name,adder.so -o adder.so -fPIC add.c
現在在你的Python代碼中來調用它
from ctypes import * #load the shared object file adder = CDLL('./adder.so') #Find sum of integers res_int = adder.add_int(4,5) print "Sum of 4 and 5 = " + str(res_int) #Find sum of floats a = c_float(5.5) b = c_float(4.1) add_float = adder.add_float add_float.restype = c_float print "Sum of 5.5 and 4.1 = ", str(add_float(a, b))
輸出如下
Sum of 4 and 5 = 9
Sum of 5.5 and 4.1 = 9.60000038147
在這個例子中,C文件是自解釋的,它包含兩個函數,分別實現了整形求和和浮點型求和。
在Python文件中,一開始先導入ctypes模塊,然后使用CDLL函數來加載我們創建的庫文件。這樣我們就可以通過變量adder來使用C類庫中的函數了。當adder.add_int()被調用時,內部將發起一個對C函數add_int的調用。ctypes接口允許我們在調用C函數時使用原生Python中默認的字符串型和整型。
而對於其他類似布爾型和浮點型這樣的類型,必須要使用正確的ctype類型才可以。如向adder.add_float()函數傳參時, 我們要先將Python中的十進制值轉化為c_float類型,然后才能傳送給C函數。這種方法雖然簡單,清晰,但是卻很受限。例如,並不能在C中對對象進行操作。
3.SWIG
SWIG是Simplified Wrapper and Interface Generator的縮寫。是Python中調用C代碼的另一種方法。在這個方法中,開發人員必須編寫一個額外的接口文件來作為SWIG(終端工具)的入口。
Python開發者一般不會采用這種方法,因為大多數情況它會帶來不必要的復雜。而當你有一個C/C++代碼庫需要被多種語言調用時,這將是個非常不錯的選擇。
示例如下(來自SWIG官網)
example.c文件中的C代碼包含了不同的變量和函數
#include <time.h> double My_variable = 3.0; int fact(int n) { if (n <= 1) return 1; else return n*fact(n-1); } int my_mod(int x, int y) { return (x%y); } char *get_time() { time_t ltime; time(<ime); return ctime(<ime); }
example.i文件內容:
%module example
%{
/* Put headers and other declarations here */
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
%}
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
編譯它
unix % swig -python example.i unix % gcc -c -fpic example.c example_wrap.c -I /usr/include/python2.7 unix % ld -shared example.o example_wrap.o -o _example.so
最后,Python的輸出
>>> import example
>>> example.fact(5)
120
>>> example.my_mod(7,3)
1
>>> example.get_time()
'Sun Feb 11 23:01:07 1996'
>>>
我們可以看到,使用SWIG確實達到了同樣的效果,雖然下了更多的工夫,但如果你的目標是多語言還是很值得的。
4.Python/C API
Python/C API可能是被最廣泛使用的方法。它不僅簡單,而且可以在C代碼中操作你的Python對象。
這種方法需要以特定的方式來編寫C代碼以供Python去調用它。所有的Python對象都被表示為一種叫做PyObject的結構體,並且Python.h頭文件中提供了各種操作它的函數。例如,如果PyObject表示為PyListType(列表類型)時,那么我們便可以使用PyList_Size()函數來獲取該結構的長度,類似Python中的len(list)函數。大部分對Python原生對象的基礎函數和操作在Python.h頭文件中都能找到。
示例
編寫一個C擴展,添加所有元素到一個Python列表(所有元素都是數字)
來看一下我們要實現的效果,這里演示了用Python調用C擴展的代碼
#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文件並沒有什么分別,導入並使用了另一個叫做addList的Python模塊。唯一差別就是這個模塊並不是用Python編寫的,而是C。
接下來我們看看如何用C編寫addList模塊,這可能看起來有點讓人難以接受,但是一旦你了解了這之中的各種組成,你就可以一往無前了。
//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對象類型的表示)和函數定義(對Python對象的操作) - 接下來我們編寫將要在Python調用的函數, 函數傳統的命名方式由{模塊名}_{函數名}組成,所以我們將其命名為addList_add
- 然后填寫想在模塊內實現函數的相關信息表,每行一個函數,以空行作為結束 - 最后的模塊初始化塊簽名為PyMODINIT_FUNC init{模塊名}。
函數addList_add接受的參數類型為PyObject類型結構(同時也表示為元組類型,因為Python中萬物皆為對象,所以我們先用PyObject來定義)。傳入的參數則通過PyArg_ParseTuple()來解析。第一個參數是被解析的參數變量。第二個參數是一個字符串,告訴我們如何去解析元組中每一個元素。字符串的第n個字母正是代表着元組中第n個參數的類型。例如,"i"代表整形,"s"代表字符串類型, "O"則代表一個Python對象。接下來的參數都是你想要通過PyArg_ParseTuple()函數解析並保存的元素。這樣參數的數量和模塊中函數期待得到的參數數量就可以保持一致,並保證了位置的完整性。例如,我們想傳入一個字符串,一個整數和一個Python列表,可以這樣去寫
int n;
char *s;
PyObject* list;
PyArg_ParseTuple(args, "siO", &n, &s, &list);
在這種情況下,我們只需要提取一個列表對象,並將它存儲在listObj變量中。然后用列表對象中的PyList_Size()函數來獲取它的長度。就像Python中調用len(list)。
現在我們通過循環列表,使用PyList_GetItem(list, index)函數來獲取每個元素。這將返回一個PyObject*對象。既然Python對象也能表示PyIntType,我們只要使用PyInt_AsLong(PyObj *)函數便可獲得我們所需要的值。我們對每個元素都這樣處理,最后再得到它們的總和。
總和將被轉化為一個Python對象並通過Py_BuildValue()返回給Python代碼,這里的i表示我們要返回一個Python整形對象。
現在我們已經編寫完C模塊了。將下列代碼保存為setup.py
#build the modules
from distutils.core import setup, Extension setup(name='addList', version='1.0', \ ext_modules=[Extension('addList', ['adder.c'])])
並且運行
python setup.py install
現在應該已經將我們的C文件編譯安裝到我們的Python模塊中了。
在一番辛苦后,讓我們來驗證下我們的模塊是否有效
#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))
輸出結果如下
Sum of List - [1, 2, 3, 4, 5] = 15
如你所見,我們已經使用Python.h API成功開發出了我們第一個Python C擴展。這種方法看似復雜,但你一旦習慣,它將變的非常有效。
Python調用C代碼的另一種方式便是使用Cython讓Python編譯的更快。但是Cython和傳統的Python比起來可以將它理解為另一種語言,所以我們就不在這里過多描述了。
