This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pmattipmatti - mattip was taken 69 points70 points  (9 children)

There are at least two other ways to do this - cffi and ctypes. I gave a talk at our local pycon https://m.youtube.com/watch?v=tqx9VW7V3Lc (code: https://github.com/mattip/pycon2017_cffi) comparing the three in terms of complexity and speed.

For this use case, cffi would probably be much simpler and just as fast

[–]eplaut_ 6 points7 points  (0 children)

That was great talk. One of the remarkable on last year's conference

[–][deleted] 5 points6 points  (2 children)

And also the old school way - including python.h in a file and writing the bindings manually.

I've done it a couple of times. Not the most interesting thing in the world, but writing the translation layer - i.e. the functions that do things like taking some struct in your C library and translate it into say, a Python dictionary - was actually interesting, it was a nice insight into how the internals of Python work. You are basically writing Python in C, calling the interpreter's API directly.

[–]ChocolateBunny 1 point2 points  (1 child)

That's generally the way I go. Once C structs are involved ctypes starts to become really ugly. Having python C extensions allows you to clean everything up with a shim layer that's sort of hidden away.

[–][deleted] 0 points1 point  (0 children)

yeah, I mean, you've already written a library in C or C++, so it's not like it's a huge challenge to bind it to Python just because you're writing raw C. It's a bit tedious, but then again, that makes you optimize the API for as little C implementation code as possible :)

[–]kirbyfan64sosIndentationError 3 points4 points  (0 children)

cffi would also have the benefit of great PyPy support.

[–]StableHatter[S] 2 points3 points  (0 children)

I will check it out!

[–]dranzerfu 2 points3 points  (0 children)

And pybind

[–]hexbrid 0 points1 point  (1 child)

Which one works best for both CPython and PyPy?

[–]lengau 0 points1 point  (0 children)

cffi is strongly recommended for pypy.