close

1. 使用c library or bulit-in function: (這個比較簡單)

    python 裡有一個模組 "ctype" 利用這個模組 你可以直接使用c library

example:

from ctypes import * #import all  built-in c function

p = create_string_buffer(3) # create a 3 byte buffer

print sizeof(p) # print the size of p


reference:http://docs.python.org/library/ctypes.html#module-ctypes

2. 使用自己寫的function(c code) :(這個複雜一點 必須利用extension modules)

english http://superjared.com/entry/anatomy-python-c-module/
chinese http://kmol.cycu.org/?e=140
useful chinese http://blog.xuite.net/csiewap/cc/30190524
http://www.cis.nctu.edu.tw/~is92004/article/PyExt_Doc.html#sec3

http://hi.baidu.com/cyclone/blog/item/8ee887d618722c2207088b1a.html

(1)Interface: 處理c 與 python 參數的對應

PyArg_ParseTuple:處理輸入 http://docs.python.org/release/1.5.2p2/ext/parseTuple.html

Py_BuildValue :處理輸出  http://docs.python.org/release/1.5.2/ext/buildValue.html

(2)registration table: 處理c與 python function name的對應(function name在c與 python 中可不同)

PyMethodDef

(3)Initialization:處理c與 python module name的對應
Py_InitModule

arrow
arrow
    全站熱搜
    創作者介紹

    quantum2nd 發表在 痞客邦 留言(0) 人氣()