Creating a wrapper for a C library in Python

2020-07-14 09:56发布

I'm trying to create a wrapper of my own for FLAC, so that I can use FLAC in my own Python code.

I tried using ctypes first, but it showed a really weird interface to the library, e.g. all the init functions for FLAC streams and files became one function with no real information on how to initialize it. Especially since it wants a reference to a stream decoder, but Python has no way to store pointers ( BZZZT! ), and thus I can't store the pointer to the stream decoder. It doesn't help that the different init functions have a different number of arguments and some argument types differ. It also has a lot of enumerations and structures, and I don't know how to get these into my code.

I've been looking into Pyrex, but I kinda ran into the same problem with pointers, but I think I solved it, sort-of. The file isn't small either, and it's not even complete.

So I'm looking for alternatives, or guides that would help me understand the aforementioned ways better. It would really help if I could get a recommendation and/or help.

标签: python c
5条回答
老娘就宠你
2楼-- · 2020-07-14 10:20

Did you have a look at http://www.swig.org/:

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.

查看更多
叛逆
3楼-- · 2020-07-14 10:20

Some people use pyrex for this.

查看更多
beautiful°
4楼-- · 2020-07-14 10:34

Python has no way to store pointers, and thus I can't store the pointer to the stream decoder

ctypes has pointers, and ctypes can be used to wrap existing C libraries. Just a tip, you will need to wrap/rewrite all relavent C structures into ctypes.Structure. Take look at examples: code.google.com/p/pyxlib-ctypes and code.google.com/p/pycairo-ctypes. More info how to map function/procedure and its argtypes and restype at http://docs.python.org/library/ctypes.html

I've been looking into Pyrex, but I kinda ran into the same problem with pointers, but I think I solved it, sort-of. The file isn't small either, and it's not even complete.

cython may be what you need if you want clean syntax. www.cython.org

So I'm looking for alternatives, or guides that would help me understand the aforementioned ways better. It would really help if I could get a recommendation and/or help.

swig on other hand can always be used but it is more complicated if you are not used to it. www.swig.org

查看更多
我命由我不由天
5楼-- · 2020-07-14 10:36

but Python has no way to store pointers ( BZZZT! )

That is incorrect. You create a pointer like this:

pInt = POINTER(c_int)()

and you access it like this

pInt[0] # or p.contents
查看更多
戒情不戒烟
6楼-- · 2020-07-14 10:43

This post is old, but there's an alternative to ctypes: CFFI. It's a lot easier, somewhat faster, and works better under PyPy. Plus, it has great support for pointers. Here's an example:

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())
查看更多
登录 后发表回答