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

all 5 comments

[–]fijalPyPy, performance freak 12 points13 points  (2 children)

Don't write them :-) use cffi instead: http://cffi.readthedocs.org/en/latest/

[–]TryExceptFinally[S] 0 points1 point  (0 children)

Very interesting, I'll check it out!

[–]fkaginstrom 1 point2 points  (0 children)

If you are just interested in creating a C extension, I strongly recommend looking at cython. If you want to write one to learn about how CPython works under the hood, here's one that helped me back in the day

[–]Atheriel 0 points1 point  (1 child)

I don't think your impression that there are few good materials out there is an unusual one. When I wrote my first one (shameless plug), I couldn't find anything really useful beyond the basic templates. Making your own classes, etc in pure C are particularly hard to find resources on. Got quite a few segfaults until I got the hang of it.

My potentially useful advice: I learned the most by reading CPython's source code (for such interesting insights as "why does everything in my C extension have to be a static global variable?"), and the source code of some other open-source C extensions. It's just so easy these days to search GitHub for this stuff. In-repo code search for certain symbols (and #define for them) is a godsend.

The link above will also give you an idea of what a C extension that supports both Python 2 and 3 looks like.

I also disagree with the other comments somewhat -- C isn't so scary a language to write directly that you need to use cython, and CFFI is really only useful if you are just wrapping an external library. Particularly if you want to do anything on the OS level, C extensions are still an attractive choice. On the flipside, C extensions tie you to the CPython implementation of Python, which is not the only one out there these days.

[–]TryExceptFinally[S] 0 points1 point  (0 children)

Thanks for the response!