Python调用c函数

如果你已经写好了一个c程序,但是又需要用脚本来实现同样的功能,应该怎么做?用python重新实现一次当然可以。但这样又得耗费不少精力,而且重复的劳动也没有意义。这种情况下就应该用ctypes模块了。

比如我想在脚本里面调用jenkins hash方法来计算某个序列的哈希值。python没有这样的模块可用。不过手头倒是有jenkins hash的c程序。看这段代码,如果用python再实现一次,是有点复杂的。所以最好的方式是把这段程序拿过来直接用。

[ccb_c height=”500″]
#include
#include
#define mix(a,b,c)
{
a -= b; a -= c; a ^= (c >> 13);
b -= c; b -= a; b ^= (a << 8); c -= a; c -= b; c ^= (b >> 13);
a -= b; a -= c; a ^= (c >> 12);
b -= c; b -= a; b ^= (a << 16); c -= a; c -= b; c ^= (b >> 5);
a -= b; a -= c; a ^= (c >> 3);
b -= c; b -= a; b ^= (a << 10); c -= a; c -= b; c ^= (b >> 15);
}

unsigned int bob_hash(void *val, unsigned int length)
{
char *k = (char *)val;
unsigned long a,b,c,len;

/* Set up the internal state */
len = length;
a = b = c = 0x9e3779b9; /* the golden ratio; an arbitrary value */

/* Handle most of the key */
while (len >= 12)
{
a += (k[0] +((unsigned long)k[1] << 8) +((unsigned long)k[2] << 16) +((unsigned long)k[3] << 24)); b += (k[4] +((unsigned long)k[5] << 8) +((unsigned long)k[6] << 16) +((unsigned long)k[7] << 24)); c += (k[8] +((unsigned long)k[9] << 8) +((unsigned long)k[10]<< 16)+((unsigned long)k[11] << 24)); mix(a,b,c); k += 12; len -= 12; } /* Handle the last 11 bytes */ c += length; switch(len) /* all the case statements fall through */ { case 11: c+=((unsigned long)k[10] << 24); case 10: c+=((unsigned long)k[9] << 16); case 9 : c+=((unsigned long)k[8] << 8); /* the first byte of c is reserved for the length */ case 8 : b+=((unsigned long)k[7] << 24); case 7 : b+=((unsigned long)k[6] << 16); case 6 : b+=((unsigned long)k[5] << 8); case 5 : b+=k[4]; case 4 : a+=((unsigned long)k[3] << 24); case 3 : a+=((unsigned long)k[2] << 16); case 2 : a+=((unsigned long)k[1] << 8); case 1 : a+=k[0]; } mix(a,b,c); return c; } int hash_string(void *tmpstr) { int hash = (int)bob_hash(tmpstr, strlen(tmpstr)); if(hash < 0) { hash = -hash; } return (int)(hash % 200); } int main() { printf("%d", hash_string("15882917276")); } [/ccb_c] 首先把这段程序编译为so文件。 [ccb_bash] gcc -fPIC -shared bob_hash.c -o bob_hash.so [/ccb_bash] 然后在python里面用ctypes加载刚才的动态库。argtypes和restype分别对应函数的参数和返回值的类型。这样就可以直接调用了。 [ccb_python] >>> from ctypes import CDLL, c_int, c_void_p
>>> bob_hash = CDLL(‘/home/jxq/code/bob_hash.so’)
>>> hash_string = bob_hash.hash_string
>>> hash_string.argtypes = [c_void_p]
>>> hash_string.restype = c_int
>>> hash_string(‘123’)
90
[/ccb_python]