tools, this makes the system useful for wrapping existing libraries, as well as
developing new extensions from scratch.
Writing interface code for large libraries can be more involved than the code gen-
eration approaches of SWIG and SIP, but it’s easier than manually wrapping li-
braries and may afford greater control than a fully automated wrapping tool. In
addition, the Py++ and older Pyste systems provide Boost.Python code generators,
in which users specify classes and functions to be exported using a simple interface
file. Both use GCC-XML to parse all the headers and extract the necessary infor-
mation to generate C++ code.
Cython (and Pyrex)
Cython, a successor to the Pyrex system, is a language specifically for writing
Python extension modules. It lets you write files that mix Python code and C da-
tatypes as you wish, and compiles the combination into a C extension for Python.
In principle, developers need not deal with the Python/C API at all, because Cython
takes care of things such as error-checking and reference counts automatically.
Technically, Cython is a distinct language that is Python-like, with extensions for
mixing in C datatype declarations and C function calls. However, almost any Py-
thon code is also valid Cython code. The Cython compiler converts Python code
into C code, which makes calls to the Python/C API. In this aspect, Cython is
similar to the now much older Python2C conversion project. By combining Python
and C code, Cython offers a different approach than the generation or coding of
integration code in other systems.
CXX, weave, and more
The CXX system is roughly a C++ version of Python’s usual C API, which handles
reference counters, exception translation, and much of the type checking and
cleanup inherent in C++ extensions. As such, CXX lets you focus on the
application-specific parts of your code. CXX also exposes parts of the C++ Stand-
ard Template Library containers to be compatible with Python sequences.
The weave package allows the inclusion of C/C++ in Python code. It’s part of the
SciPy package (http://www.scipy.org) but is also available as a standalone system.
A page at http://www.python.org chronicles additional projects in this domain,
which we don’t have space to mention here.
Other languages: Java, C#, FORTRAN, Objective-C, and others
Although we’re focused on C and C++ in this chapter, you’ll also find direct sup-
port for mixing Python with other programming languages in the open source
world. This includes languages that are compiled to binary form like C, as well as
some that are not.
For example, by providing full byte code compilers, the Jython and IronPython
systems allow code written in Python to interface with Java and C#/.NET com-
ponents in a largely seamless fashion. Alternatively, the JPype and Python for .NET
projects support Java and C#/.NET integration for normal CPython (the standard
Other Extending Tools | 1513