User guide
NumPy User Guide, Release 1.9.0
It’s disadvantages include
• It is difficult to distribute an extension module made using ctypes because of a lack of support for building
shared libraries in distutils (but I suspect this will change in time).
• You must have shared-libraries of your code (no static libraries).
• Very little support for C++ code and it’s different library-calling conventions. You will probably need a C-
wrapper around C++ code to use with ctypes (or just use Boost.Python instead).
Because of the difficulty in distributing an extension module made using ctypes, f2py is still the easiest way to extend
Python for package creation. However, ctypes is a close second and will probably be growing in popularity now that
it is part of the Python distribution. This should bring more features to ctypes that should eliminate the difficulty in
extending Python and distributing the extension using ctypes.
5.2.7 Additional tools you may find useful
These tools have been found useful by others using Python and so are included here. They are discussed separately
because I see them as either older ways to do things more modernly handled by f2py, weave, Pyrex, or ctypes (SWIG,
PyFort, PyInline) or because I don’t know much about them (SIP, Boost, Instant). I have not added links to these
methods because my experience is that you can find the most relevant link faster using Google or some other search
engine, and any links provided here would be quickly dated. Do not assume that just because it is included in this
list, I don’t think the package deserves your attention. I’m including information about these packages because many
people have found them useful and I’d like to give you as many options as possible for tackling the problem of easily
integrating your code.
SWIG
Simplified Wrapper and Interface Generator (SWIG) is an old and fairly stable method for wrapping C/C++-libraries
to a large variety of other languages. It does not specifically understand NumPy arrays but can be made useable
with NumPy through the use of typemaps. There are some sample typemaps in the numpy/tools/swig directory under
numpy.i together with an example module that makes use of them. SWIG excels at wrapping large C/C++ libraries
because it can (almost) parse their headers and auto-produce an interface. Technically, you need to generate a .i file
that defines the interface. Often, however, this .i file can be parts of the header itself. The interface usually needs a bit
of tweaking to be very useful. This ability to parse C/C++ headers and auto-generate the interface still makes SWIG
a useful approach to adding functionalilty from C/C++ into Python, despite the other methods that have emerged that
are more targeted to Python. SWIG can actually target extensions for several languages, but the typemaps usually
have to be language-specific. Nonetheless, with modifications to the Python-specific typemaps, SWIG can be used to
interface a library with other languages such as Perl, Tcl, and Ruby.
My experience with SWIG has been generally positive in that it is relatively easy to use and quite powerful. I used
to use it quite often before becoming more proficient at writing C-extensions. However, I struggled writing custom
interfaces with SWIG because it must be done using the concept of typemaps which are not Python specific and are
written in a C-like syntax. Therefore, I tend to prefer other gluing strategies and would only attempt to use SWIG to
wrap a very-large C/C++ library. Nonetheless, there are others who use SWIG quite happily.
SIP
SIP is another tool for wrapping C/C++ libraries that is Python specific and appears to have very good support for
C++. Riverbank Computing developed SIP in order to create Python bindings to the QT library. An interface file must
be written to generate the binding, but the interface file looks a lot like a C/C++ header file. While SIP is not a full
C++ parser, it understands quite a bit of C++ syntax as well as its own special directives that allow modification of
how the Python binding is accomplished. It also allows the user to define mappings between Python types and C/C++
structrues and classes.
78 Chapter 5. Using Numpy C-API