lispとrubyとpythonと その4 Cライブラリの呼び出し(python)

最後はpython
Cとの連携方法はいくつかあるみたい(boost.pythonとかswingとか)あるみたいなんだけど、ctypesを使用。
呼び出しているcライブラリはlispとrubyとpythonと その4 Cライブラリの呼び出し(lisp) - テンポラリと同じ。
参考は、まぁここ。
http://www.python.org/doc/2.5.2/lib/module-ctypes.html

int f4(person_t** person);の呼び出しかたがわかんなかったんだけど今日はもう力尽きた・・・。

# -*- coding:utf-8 -*-
import ctypes
from ctypes import *

so = ctypes.CDLL("./libffi-test.so")
so.f.restype = c_int

def f(i):
    return so.f(i)

print f(5)

so.f0.argtypes = [c_char_p,c_int]
so.f0.restype = c_int

def f0(buf,len):
    return so.f0(buf,len)

buf0 = create_string_buffer(10)
print f0(buf0,10)
print buf0.value

so.f1.argtype = [POINTER(c_char_p)]
so.f1.restype = c_int
def f1(buf):
    return so.f1(pointer(buf))

buf1 = c_char_p()
f1(buf1)
print buf1.value


so.f2.restype = c_char_p
def f2():
    return so.f2()

print f2()

class Person(Structure):
    _fields_ = [("name",c_char_p),
                ("age",c_int)]

so.f3.argtype = [POINTER(c_char_p),c_int]
so.f3.restype = c_int

def f3(person):
    person.name = cast(create_string_buffer(16),c_char_p)
    return so.f3(pointer(person),16)

p1 = Person()
f3(p1)

print p1.name
print p1.age

#so.f4.argtype = [POINTER(POINTER(Person))]
#so.f4.restype = c_int
#p2 = POINTER(POINTER(Person))
#print so.f4
#print p2
#so.f4(p2)
#so.f4(pointer(pointer(p2)))
#so.f4(p2)
#print p2.name