Playing With Ctypes and Ghostscript
I have been playing around with the ctypes library in python recently. It is a foreign function interface for dynamic linked libraries(DLL)/shared libraries. It works back to python 2.3, and it was added into the standard library in python 2.5. Anyway, it provides a much nicer way to access C libraries then using one of the various methods to create a C extension, though it is a bit slower. As I learned at pycon, one of the benefits is that PyPy (one of the craziest things I have ever seen) can use ctypes. I was thinking of staying for the PyPy sprint at pycon, but I think they were going to work on a pure python implementation of the library, which was far too intimidating to me. I will post about pycon at some point soon.
I guess I find ctypes so special just because it is the first thing in recent history with python where I was really amazed. I first saw it with µTidylib, which is a python wrapper around the HTML tidy library. What got me was just playing with it on the interpreter:
>>> from ctypes import *
>>> libc = CDLL("libc.so.6")
>>> libc.printf("Hello, World!\n")
Hello, World!
14
>>>
Obviously, it is a lot more powerful than this example illustrates, but you get the idea. I have been fixing up some of the filters in the print system at the Maclab over spring break, so I have been working with ghostscript a bit. There is an API for ghostscript, so I have started writing a wrapper library for it in my free time to practice using ctypes, and, well, because one doesn’t exist. I am trying to think of a name that somehow combines ghosts, snakes, and desktop publishing….
Posted in C, Python, coding, postscript | No Comments


