details on these and others. Like SWIG, all of the following began life as third-party
tools installed separately, though Python 2.5 and later incorporates the ctypes extension
as a standard library module.
SIP
Just as a sip is a smaller swig in the drinking world, so too is the SIP system a lighter
alternative to SWIG in the Python world (in fact, it was named on purpose for the
joke). According to its web page, SIP makes it easy to create Python bindings for
C and C++ libraries. Originally developed to create the PyQt Python bindings for
the Qt toolkit, it can be used to create bindings for any C or C++ library. SIP
includes a code generator and a Python support module.
Much like SWIG, the code generator processes a set of specification files and gen-
erates C or C++ code, which is compiled to create the bindings extension module.
The SIP Python module provides support functions to the automatically generated
code. Unlike SWIG, SIP is specifically designed just for bringing together Python
and C/C++. SWIG also generates wrappers for many other scripting languages,
and so is viewed by some as a more complex project.
ctypes
The ctypes system is a foreign function interface (FFI) module for Python. It allows
Python scripts to access and call compiled functions in a binary library file directly
and dynamically, by writing dispatch code in Python itself, instead of generating
or writing the integration C wrapper code we’ve studied in this chapter. That is,
library glue code is written in pure Python instead of C. The main advantage is that
you don’t need C code or a C build system to access C functions from a Python
script. The disadvantage is potential speed loss on dispatch, though this depends
upon the alternative measured.
According to its documentation, ctypes allows Python to call functions exposed
from DLLs and shared libraries and has facilities to create, access, and manipulate
complex C datatypes in Python. It is also possible to implement C callback func-
tions in pure Python, and an experimental ctypes code generator feature allows
automatic creation of library wrappers from C header files. ctypes works on Win-
dows, Mac OS X, Linux, Solaris, FreeBSD, and OpenBSD. It may run on additional
systems, provided that the libffi package it employs is supported. For Windows,
ctypes contains a ctypes.com package, which allows Python code to call and im-
plement custom COM interfaces. See Python’s library manuals for more on the
ctypes functionality included in the standard library.
Boost.Python
The Boost.Python system is a C++ library that enables seamless interoperability
between C++ and the Python programming language through an IDL-like model.
Using it, developers generally write a small amount of C++ wrapper code to create
a shared library for use in Python scripts. Boost.Python handles references, call-
backs, type mappings, and cleanup tasks. Because it is designed to wrap C++ in-
terfaces nonintrusively, C++ code need not be changed to be wrapped. Like other
1512 | Chapter 20: Python/C Integration