Phillip Pearson - web + electronics notes

tech notes and web hackery from a new zealander who was vaguely useful on the web back in 2002 (see: python community server, the blogging ecosystem, the new zealand coffee review, the internet topic exchange).


Extending Python

It turns out that writing C modules for Python is really trivial. I spent quite a while the other day trying to figure out how to compile and link everything, but it turns out that the distutils module will take care of all this for you.

The trick is to make a file that looks like this:

from distutils.core import setup, Extension
setup(name="foo", version="1.0",
      ext_modules=[Extension("foo", ["foo.c"])])
(That example comes from the Python manual).

Now, when you run python build, it will invoke GCC or MSVC (your platform's default C compiler) and build you a .so or a .dll out of foo.c

Here's an example of the sort of C code you have to write to be callable from Python. You also need to register your module with the Python interpreter - more on that later.