2010-01-28 5 views
1

어떻게 든 세계를 조작하는 C 라이브러리가 있다고 가정 해 봅니다.Python 내의 C 함수에 여러 인수 전달

파이썬에서이 라이브러리를 사용하고 싶습니다. 나는 세계 관리의 다른 시나리오를 나타내는 간단한 파이썬 스크립트를 작성할 수 있기를 원한다.

세계를 만들고 파괴하는 함수가 있습니다. void * create (void); int destroy (void * world);

import ctypes 

lib = ctypes.CDLL('manage_world.so') 

_create = lib.create 
_create.restype = ctypes.c_void_p 

_destroy = lib.destroy 
_destroy.argtypes = [ctypes.c_void_p,] 
_destroy.restype = ctypes.c_int 

def create_world(): 
    res = _create() 
    res = ctypes.cast(res, ctypes.c_void_p) 
    return res 

def destroy_world(world): 
    return _destroy(world) 

new_world = create_world() 
print type(new_world) 

print destroy_world(new_world) 

이 지금은 같은 기능을 추가 할 : 여기

는 일부 파이썬 코드 INT의 set_world_feature를 (무효 * 세계 feature_t f를, ...); int get_world_feature (void * world, feature_t f, ...);

내 파이썬 래퍼에 여러 가지 인수를 다양하게 전달하는 방법을 모르겠습니다.

때때로 3 개 또는 4 개의 인수로 set_world_feature()가 호출되기 때문에. 다시 파이썬에서

: 작동하는

def set_world_feature(world, *features): 
    res = lib.set_world_feature(world, *features) 
    return world_error[res] 

방법을 위해이 문제를 해결하려면?

답변

1

당신이 할 경우 :

def create_world(): 
    return _create 

당신은 너무 create_world 함수 포인터를 반환, _create 호출하지 않습니다. 월드 인스턴스에 대한 포인터를 쓰려면 대신 다음을 써야합니다.

def create_world(): 
    return _create()