views:

114

answers:

2

Let's say I have a c library that manipulates a world somehow.

I want to use this library with python. I want to be able to write simple python scripts that represent different scenarios of world management.

I have functions that create and destroy a world: void* create(void); int destroy(void* world);

Here is some python code:

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)

Now I want to add functions like: int set_world_feature(void* world, feature_t f, ...); int get_world_feature(void* world, feature_t f, ...);

The thing is that in my python wrapper I don't know how to pass variously multiple arguments.

Because sometimes set_world_feature() is called with 3 or 4 arguments.

In Python again:

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

How to fix this in order for it to work?

+1  A: 

When you do:

def create_world():
    return _create

You don't call _create, so create_world returns the function pointer. If you want the pointer to your world instance you should write instead:

def create_world():
    return _create()
Luper Rouch
A: 

Yes. It was a stupid mistake of mine. Thanks a lot.

Still, I have a problem.

manson54
strace traces system calls and signals, if your lib doesn't do any system call nothing will appear in strace. Use gdb to see where your application fails (and a segfault is *very* unlikely to occur in Python). Also you should edit your question to update it rather than adding another question in an answer like that.
Luper Rouch
so, as you said. segfault is very unlikely to occur in Python. It was something in the c part. Thank you a lot and sorry for the new answer. I am still a newbie here on stackoverflow.
manson54