This is now a Python built-in module. I was a bit put off when I looked at the official docs, since all the early examples are windows and there is almost no mention of OS X. (However, I will have to make sure I understand what's there later), I'd start instead with Andrew Dalke's quick tutorial (here). So the first part of this is my code duplicating a few of his examples.
Getting started is literally one line, preceded by the import, and followed by the function call itself. That's wonderful!
It's pretty self-explanatory, if you're at all familiar with C, except for the constant
RTLD_GLOBAL. Under the hood, the library
libcuses the functions
dlopenand friends defined in
clfcn.h. (We touched on these once before, in the last example of this post).
The prototype for the C call to load a library is:
void *dlopen(const char *file, int mode);
RTLD_GLOBAL is one of several enumerated modes, and the value we provide is passed in as an argument to dlopen. You can read about dlopen here.
I'm not sure why this works, because in the standard C library,
printfreturns the number of characters printed, or a negative value to indicate an error. The next example introduces the idea that frequently we need to tell ctypes the type of input arguments or return values.
The assumed return type is int, and in order to get this example to work we have to specify that the returned value is a double. Here's the prototype:
double atof (const char *str);
Argument types are specified like this:
Finally, here's another function, this one from the math library:
And here is a glowing testimonial about controlling a camera via USB with code written in Python.
It'd be interesting to see what we could do on that in OS X.
Still to do:
• finish Andrew's tutorial (lots more there)
• read the official docs
• try to apply it to the problem of finding binding sites (here)