Shell for C library in Python

I am trying to create my own wrapper for FLAC, so that I can use FLAC in my own Python code.

At first I tried using ctypes, but it showed a really strange interface for the library, for example. all init functions for FLAC streams and files became one function without real information on how to initialize it. Moreover, he wants a link to the stream decoder, but Python has no way to store pointers ( BZZZT! ), And therefore I cannot save the pointer to the stream decoder. This does not help that different init functions have a different number of arguments, and some types of arguments differ. It also has many enumerations and structures, and I don’t know how to include them in my code.

I looked at Pyrex, but somehow I ran into the same issue with pointers, but I think I solved it, sort of. The file is also small, and it is not even complete .

So, I'm looking for alternatives or guides that will help me better understand the above methods. This will really help if I can get a recommendation and / or help.

+5
source share
5 answers

Python is not able to store pointers, and therefore I cannot save a pointer to a stream decoder

ctypes , ctypes C. , / relavent C ctypes.Structure.   : code.google.com/p/pyxlib-ctypes code.google.com/p/pycairo-ctypes. , / http://docs.python.org/library/ctypes.html

Pyrex, - , , , . , .

cython , . www.cython.org

, , . , / .

swig , , . www.swig.org

+10

http://www.swig.org/:

SWIG - , C ++ .

+5

Python (BZZZT!)

. :

pInt = POINTER(c_int)()

pInt[0] # or p.contents
+4

, ctypes: CFFI. , PyPy. , . :

from cffi import FFI

ffi = cffi.FFI()

ffi.cdef('''
struct x { void *a; }

void* get_buffer();
struct x* make_x(void*);
void change_x(struct x*, void*);
''')

dll = ffi.dlopen('libmyawesomelibrary.so')

buf = dll.get_buffer()
tst = dll.new('struct x*')
tst.a = buf
change_x(tst, get_buffer())
tst2 = make_x(get_buffer())
+2

Some people use pyrex for this.

0
source