1

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 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.