天天看點

Python的C拓展簡介

Python的C拓展

1. 環境準備

如果是Linux隻需要安裝Python3.x + Python-dev。

Windows下稍微複雜點,VS2017 + Python3.6.3

VS2017可用社群版,需要選擇安裝的環境如下:

Python的C拓展簡介
Python的C拓展簡介

2. Hello World !

2.1 C子產品封裝

以計算兩個數相加為例,選擇任意檔案夾,建立如下C語言源碼:

// 檔案名 calc.c
#include <Python.h>

int add(int x, int y){ // C 函數
    return x + y;
}

static PyObject *calc_add(PyObject *self, PyObject *args){

    int x, y;
    // Python傳入參數
    // "ii" 表示傳入參數為2個int型參數,将其解析到x, y變量中
    if(!PyArg_ParseTuple(args, "ii", &x, &y))
        return NULL;
    return PyLong_FromLong(add(x, y));
}

// 子產品的方法清單
static PyMethodDef CalcMethods[] = {
     {"add", calc_add, METH_VARARGS, "函數描述"},
     {NULL, NULL, , NULL}
};

// 子產品
static struct PyModuleDef calcmodule = {
    PyModuleDef_HEAD_INIT,
    "calc", // 子產品名
    NULL, // 子產品文檔
    -,       /* size of per-interpreter state of the module,
                or -1 if the module keeps state in global variables. */
    CalcMethods
};

// 初始化
PyMODINIT_FUNC PyInit_calc(void)
{
    return PyModule_Create(&calcmodule);
}
           

其中,靜态函數 calc_add 以python的C接口方式封裝了add函數,命名方式

子產品名_函數名

靜态PyMethodDef清單 變量

CalcMethods

包含了該子產品方法的描述

靜态struct PyModuleDef結構體 變量

calcmodule

定義了子產品的描述

PyInit_calc 函數初始化了子產品,命名方式

PyInit_子產品名

2.2 C源碼編譯

在VS2017中可以直接生成 .dll 檔案,然後改名為 .pyd 就可在python程式中引入該子產品了,但是,這不“清真”,正确的姿勢是寫一個

setup.py

然後通過python調cl.exe編譯。

建立

setup.py

檔案,内容如下:

# setup.py

from distutils.core import setup, Extension

module1 = Extension('calc',
                    sources=['calc.c'])

setup(name='calc_model',
      version='1.0',
      description='Hello ?',
      ext_modules=[module1]
)
           

然後,從Windows的指令行(指令提示符)下進入到這個檔案夾下,執行:

python setup.py build

即可完成編譯,如果出現某

.bat

檔案未找到,說明你的VS沒有安裝相應的依賴(Linux下編譯不成功原因可能是沒有裝python-dev),按文章開頭給出的依賴庫添加修改(此時不需要重新安裝VS)。

編譯結束後,在該檔案夾下會出現 build 檔案夾,進入該檔案夾,出現如下兩個檔案夾:

Python的C拓展簡介

進入 lib.xxx那個檔案夾,裡面有個 .pyd 結尾的檔案(Linux下為 .so 結尾),這就是我們編譯好的python子產品了,如下:

Python的C拓展簡介

當然,你也可以改名為 calc.pyd 比較好看,不過這不影響調用。

2.3 Python調用

這部分就簡單了,進入含有編譯好的 .pyd 檔案夾,建立如下檔案:

import calc

print(calc.add(, ))
           

這就是一個普通庫,這樣調用就OK了。

3. Python的參數傳遞以及C的傳回值相關問題

這部分我直接甩出檔案就行,編譯及調用過程與上面一樣。

C 檔案

/**建構傳回值
Py_BuildValue("")                        None
Py_BuildValue("i", 123)                  123
Py_BuildValue("iii", 123, 456, 789)      (123, 456, 789)
Py_BuildValue("s", "hello")              'hello'
Py_BuildValue("y", "hello")              b'hello'
Py_BuildValue("ss", "hello", "world")    ('hello', 'world')
Py_BuildValue("s#", "hello", 4)          'hell'
Py_BuildValue("y#", "hello", 4)          b'hell'
Py_BuildValue("()")                      ()
Py_BuildValue("(i)", 123)                (123,)
Py_BuildValue("(ii)", 123, 456)          (123, 456)
Py_BuildValue("(i,i)", 123, 456)         (123, 456)
Py_BuildValue("[i,i]", 123, 456)         [123, 456]
Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456)    {'abc': 123, 'def': 456}
Py_BuildValue("((ii)(ii)) (ii)", 1, 2, 3, 4, 5, 6)          (((1, 2), (3, 4)), (5, 6))
**/

#include<Python.h>


static PyObject *value_commonArgs(PyObject *self, PyObject *args){
    // 傳入普通參數,例如: s = value.com(1, 2.3, "Hello C")
    int x;
    double y;
    char *z;
    if(!PyArg_ParseTuple(args, "ids", &x, &y, &z))
        return NULL;
    printf("The args is %d and %f and %s .\n", x, y, z);
    // 傳回(x, y, z)的元組
    return Py_BuildValue("(i,d,s)",x, y, z);
}



static PyObject *value_tupleTest(PyObject *self, PyObject *args){
    // t = value.tut((1, 3), "Tuple")
    int x, y;
    char *z;
    if(!PyArg_ParseTuple(args, "(ii)s", &x, &y, &z))
        return NULL;
    printf("The args is (%d, %d), %s .\n", x, y, z);

    // return ([1, 2], "hello")
    return Py_BuildValue("[i,i]s", x, y, z);
}



static PyObject *value_some(PyObject *self, PyObject *args){
    /* 可選參數,可能是下面幾種, “|” 代表後面的參數可選
        c = value.som(1)
        value.som(1, 3)
        value.som(1, 2, "hello")
    */
    int x = , y = ;
    char *z = NULL;
    if(!PyArg_ParseTuple(args, "i|is", &x, &y, &z))
        return NULL;
    printf("x is: %d\n", x);
    printf("y is: %d\n", y);
    if(z != NULL)printf("z is: %s\n", z);
    return Py_BuildValue("[i,i,s]", x, y, z);
}



static PyObject *value_kwargs(PyObject *self, PyObject *args, PyObject *kwargs){
    /* 帶有鍵的參數
        value.kws(c=3)
        value.kws(d=2)
        value.kws(c=45, d=89)
    */

    int c = , d = ;

    static char *keys[] = {"c", "d", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", keys, &c, &d))
        return NULL;
    printf("The c : %d and d : %d\n", c, d);
    Py_RETURN_NONE;
}



static PyObject *value_listkwargs(PyObject *self, PyObject *args, PyObject *kwargs){
    /* 帶鍵與不帶鍵的參數
        value.lks(b=1, a=2)
        value.lks(1, 2, c=45)
        value.lks(3, 4, c=5, d=6)
        value.lks(d=3, c=4, b=5, a=6)
    */

    int a = , b = ;
    int c = , d = ;

    static char *keys[] = {"a", "b", "c", "d", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", keys, &a, &b, &c, &d))
        return NULL;
    printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, d);
    Py_RETURN_NONE;
}



// 子產品的方法清單
static PyMethodDef ValueMethods[] = {
     {"com", value_commonArgs, METH_VARARGS, "Common args."},
     {"tut", value_tupleTest, METH_VARARGS, "Tuple args."},
     {"som", value_some, METH_VARARGS, "Some args."},
     {"kws", (PyCFunction)value_kwargs, METH_VARARGS | METH_KEYWORDS,"kwargs doc."},
     {"lks", (PyCFunction)value_listkwargs, METH_VARARGS | METH_KEYWORDS,"List and kwargs doc."},
     {NULL, NULL, , NULL}
};



// 子產品
static struct PyModuleDef valuemodule = {
    PyModuleDef_HEAD_INIT,
    "value", // 子產品名
    "The python and c demo.", // 子產品文檔
    -,
    ValueMethods
};



// 初始化
PyMODINIT_FUNC PyInit_value(void)
{
    return PyModule_Create(&valuemodule);
}
           

編譯檔案

from distutils.core import setup, Extension

module1 = Extension('value',
                    sources=['value.c'])

setup(name='valueTest_model',
      version='1.0',
      description='',
      ext_modules=[module1]
)
           

Python 調用檔案

import value
s = value.com(, , "Hello C")
print("Args: ", s)

t = value.tut((, ), "Tuple")
print("Tuple: ", t)

c = value.som()
value.som(, )
value.som(, , "hello")

value.kws(c=)
value.kws(d=)
value.kws(c=, d=)

value.lks(b=, a=)
value.lks(, , c=)
value.lks(, , c=, d=)
value.lks(d=, c=, b=, a=)
           
Python的C拓展簡介