blob: fd07301d439aed208d325681a085d842c8d67cdc [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SWIG and Python</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff">
<H1><a name="Python">33 SWIG and Python</a></H1>
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#Python_nn2">Overview</a>
<li><a href="#Python_nn3">Preliminaries</a>
<ul>
<li><a href="#Python_nn4">Running SWIG</a>
<li><a href="#Python_nn6">Using distutils</a>
<li><a href="#Python_nn7">Hand compiling a dynamic module</a>
<li><a href="#Python_nn8">Static linking</a>
<li><a href="#Python_nn9">Using your module</a>
<li><a href="#Python_nn10">Compilation of C++ extensions</a>
<li><a href="#Python_nn11">Compiling for 64-bit platforms</a>
<li><a href="#Python_nn12">Building Python extensions under Windows</a>
<li><a href="#Python_commandline">Additional Python commandline options</a>
</ul>
<li><a href="#Python_nn13">A tour of basic C/C++ wrapping</a>
<ul>
<li><a href="#Python_nn14">Modules</a>
<li><a href="#Python_nn15">Functions</a>
<li><a href="#Python_nn16">Global variables</a>
<li><a href="#Python_nn17">Constants and enums</a>
<li><a href="#Python_nn18">Pointers</a>
<li><a href="#Python_nn19">Structures</a>
<li><a href="#Python_nn20">C++ classes</a>
<li><a href="#Python_nn21">C++ inheritance</a>
<li><a href="#Python_nn22">Pointers, references, values, and arrays</a>
<li><a href="#Python_nn23">C++ overloaded functions</a>
<li><a href="#Python_nn24">C++ operators</a>
<li><a href="#Python_nn25">C++ namespaces</a>
<li><a href="#Python_nn26">C++ templates</a>
<li><a href="#Python_nn27">C++ Smart Pointers</a>
<ul>
<li><a href="#Python_smart_pointers_shared_ptr">The shared_ptr Smart Pointer</a>
<li><a href="#Python_smart_pointers_generic">Generic Smart Pointers</a>
</ul>
<li><a href="#Python_nn27a">C++ reference counted objects</a>
</ul>
<li><a href="#Python_nn28">Further details on the Python class interface</a>
<ul>
<li><a href="#Python_nn29">Proxy classes</a>
<li><a href="#Python_builtin_types">Built-in Types</a>
<ul>
<li><a href="#Python_builtin_limitations">Limitations</a>
<li><a href="#Python_builtin_overloads">Operator overloads and slots -- use them!</a>
</ul>
<li><a href="#Python_nn30">Memory management</a>
</ul>
<li><a href="#Python_directors">Cross language polymorphism</a>
<ul>
<li><a href="#Python_nn33">Enabling directors</a>
<li><a href="#Python_nn34">Director classes</a>
<li><a href="#Python_nn35">Ownership and object destruction</a>
<li><a href="#Python_nn36">Exception unrolling</a>
<li><a href="#Python_nn37">Overhead and code bloat</a>
<li><a href="#Python_nn38">Typemaps</a>
<li><a href="#Python_nn39">Miscellaneous</a>
</ul>
<li><a href="#Python_nn40">Common customization features</a>
<ul>
<li><a href="#Python_nn41">C/C++ helper functions</a>
<li><a href="#Python_nn42">Adding additional Python code</a>
<li><a href="#Python_nn43">Class extension with %extend</a>
<li><a href="#Python_nn44">Exception handling with %exception</a>
<li><a href="#Python_optimization">Optimization options</a>
<ul>
<li><a href="#Python_fastproxy">-fastproxy</a>
</ul>
</ul>
<li><a href="#Python_nn45">Tips and techniques</a>
<ul>
<li><a href="#Python_nn46">Input and output parameters</a>
<li><a href="#Python_nn47">Simple pointers</a>
<li><a href="#Python_nn48">Unbounded C Arrays</a>
<li><a href="#Python_nn49">String handling</a>
<li><a href="#Python_default_args">Default arguments</a>
</ul>
<li><a href="#Python_nn53">Typemaps</a>
<ul>
<li><a href="#Python_nn54">What is a typemap?</a>
<li><a href="#Python_nn55">Python typemaps</a>
<li><a href="#Python_nn56">Typemap variables</a>
<li><a href="#Python_nn57">Useful Python Functions</a>
</ul>
<li><a href="#Python_nn58">Typemap Examples</a>
<ul>
<li><a href="#Python_nn59">Converting Python list to a char ** </a>
<li><a href="#Python_nn60">Expanding a Python object into multiple arguments</a>
<li><a href="#Python_nn61">Using typemaps to return arguments</a>
<li><a href="#Python_nn62">Mapping Python tuples into small arrays</a>
<li><a href="#Python_nn63">Mapping sequences to C arrays</a>
<li><a href="#Python_nn64">Pointer handling</a>
<li><a href="#Python_memory_management_member_variables">Memory management when returning references to member variables</a>
</ul>
<li><a href="#Python_nn65">Docstring Features</a>
<ul>
<li><a href="#Python_nn66">Module docstring</a>
<li><a href="#Python_nn67">%feature("autodoc")</a>
<ul>
<li><a href="#Python_nn68">%feature("autodoc", "0")</a>
<li><a href="#Python_nn69">%feature("autodoc", "1")</a>
<li><a href="#Python_autodoc2">%feature("autodoc", "2")</a>
<li><a href="#Python_autodoc3">%feature("autodoc", "3")</a>
<li><a href="#Python_nn70">%feature("autodoc", "docstring")</a>
</ul>
<li><a href="#Python_nn71">%feature("docstring")</a>
</ul>
<li><a href="#Python_nn72">Python Packages</a>
<ul>
<li><a href="#Python_modulepackage">Setting the Python package</a>
<li><a href="#Python_absrelimports">Absolute and relative imports</a>
<li><a href="#Python_absimport">Enforcing absolute import semantics</a>
<li><a href="#Python_importfrominit">Importing from __init__.py</a>
<li><a href="#Python_implicit_namespace_packages">Implicit namespace packages</a>
<li><a href="#Python_package_search">Location of modules</a>
<ul>
<li><a href="#Python_package_search_both_package_modules">Both modules in the same package</a>
<li><a href="#Python_package_search_both_global_modules">Both modules are global</a>
<li><a href="#Python_package_search_wrapper_split">Split modules custom configuration</a>
<li><a href="#Python_custom_module_import">More on customizing the module import code</a>
<li><a href="#Python_package_search_static">Statically linked C modules</a>
</ul>
</ul>
<li><a href="#Python_python3support">Python 3 Support</a>
<ul>
<li><a href="#Python_nn74">Function annotation</a>
<li><a href="#Python_nn75">Buffer interface</a>
<li><a href="#Python_nn76">Abstract base classes</a>
<li><a href="#Python_nn77">Byte string output conversion</a>
<li><a href="#Python_2_unicode">Python 2 Unicode</a>
</ul>
<li><a href="#Python_multithreaded">Support for Multithreaded Applications</a>
<ul>
<li><a href="#Python_thread_UI">UI for Enabling Multithreading Support</a>
<li><a href="#Python_thread_performance">Multithread Performance</a>
</ul>
</ul>
</div>
<!-- INDEX -->
<p>
<b>Caution: This chapter is under repair!</b>
</p>
<p>
This chapter describes SWIG's support of Python. SWIG is compatible
with all recent Python versions (Python 2.7 and Python &gt;= 3.2). If you
still need to generate bindings which work with older versions of Python,
you'll have to use SWIG 3.0.x.
</p>
<p>
This chapter covers most SWIG features, but certain low-level details
are covered in less depth than in earlier chapters. At the
very least, make sure you read the "<a href="SWIG.html#SWIG">SWIG
Basics</a>" chapter.
</p>
<H2><a name="Python_nn2">33.1 Overview</a></H2>
<p>
To build Python extension modules, SWIG uses a layered approach in which
parts of the extension module are defined in C and other parts are
defined in Python. The C layer contains low-level wrappers whereas Python code
is used to define high-level features.
</p>
<p>
This layered approach recognizes the fact that certain aspects of
extension building are better accomplished in each language (instead
of trying to do everything in C or C++). Furthermore, by generating code in both
languages, you get a lot more flexibility since you can enhance the extension
module with support code in either language.
</p>
<p>
In describing the Python interface, this chapter starts by covering the
basics of configuration, compiling, and installing Python modules.
Next, the Python interface to common C and C++ programming features is
described. Advanced customization features such as typemaps are then
described followed by a discussion of low-level implementation
details.
</p>
<H2><a name="Python_nn3">33.2 Preliminaries</a></H2>
<H3><a name="Python_nn4">33.2.1 Running SWIG</a></H3>
<p>
Suppose that you defined a SWIG module such as the following:
</p>
<div class="code">
<pre>
/* File: example.i */
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
int fact(int n);
</pre>
</div>
<p>
The <tt>#define SWIG_FILE_WITH_INIT</tt> line inserts a macro that specifies that the
resulting C file should be built as a Python extension, inserting the module
<tt>init</tt> code. This <tt>.i</tt> file wraps the following simple C file:
</p>
<div class="code">
<pre>
/* File: example.c */
#include "example.h"
int fact(int n) {
if (n &lt; 0) { /* This should probably return an error, but this is simpler */
return 0;
}
if (n == 0) {
return 1;
} else {
/* testing for overflow would be a good idea here */
return n * fact(n-1);
}
}
</pre>
</div>
<p>
With the header file:
</p>
<div class="code">
<pre>
/* File: example.h */
int fact(int n);
</pre>
</div>
<p>
To build a Python module, run SWIG using the <tt>-python</tt> option:
</p>
<div class="shell"><pre>
$ swig -python example.i
</pre></div>
<p>
If building a C++ extension, add the <tt>-c++</tt> option:
</p>
<div class="shell"><pre>
$ swig -c++ -python example.i
</pre></div>
<p>
This creates two different files; a C/C++ source file <tt>example_wrap.c</tt> or
<tt>example_wrap.cxx</tt> and a Python source file <tt>example.py</tt>. The generated C
source file contains the low-level wrappers that need to be compiled and linked with the
rest of your C/C++ application to create an extension module. The Python source file
contains high-level support code. This is the file that you will import to use the module.
</p>
<p>
The name of the wrapper file is derived from the name of the input file. For example, if the
input file is <tt>example.i</tt>, the name of the wrapper file is <tt>example_wrap.c</tt>.
To change this, you can use the <tt>-o</tt> option. The name of the Python file is derived
from the module name specified with <tt>%module</tt>. If the module name is <tt>example</tt>,
then a file <tt>example.py</tt> is created.
</p>
<p>
The following sections have further practical examples and details on
how you might go about compiling and using the generated files.
</p>
<H3><a name="Python_nn6">33.2.2 Using distutils</a></H3>
<p>
The preferred approach to building an extension module for Python is to compile it with
distutils, which comes with all recent versions of Python
(<a href="https://docs.python.org/3/library/distutils.html">Distutils Docs</a>).
</p>
<p>
Distutils takes care of making sure that your extension is built with all the correct
flags, headers, etc. for the version of Python it is run with. Distutils will compile your
extension into a shared object file or DLL (<tt>.so</tt> on Linux, <tt>.pyd</tt> on
Windows, etc). In addition, distutils can handle installing your package into
site-packages, if that is desired. A configuration file (conventionally called: <tt>setup.py</tt>)
describes the extension (and related Python modules). The distutils will
then generate all the right compiler directives to build it for you.
</p>
<p>
Here is a sample <tt>setup.py</tt> file for the above example:
</p>
<div class="code">
<pre>
#!/usr/bin/env python
"""
setup.py file for SWIG example
"""
from distutils.core import setup, Extension
example_module = Extension('_example',
sources=['example_wrap.c', 'example.c'],
)
setup (name = 'example',
version = '0.1',
author = "SWIG Docs",
description = """Simple swig example from docs""",
ext_modules = [example_module],
py_modules = ["example"],
)
</pre>
</div>
<p>
In this example, the line: <tt>example_module = Extension(....)</tt> creates an Extension
module object, defining the name as <tt>_example</tt>, and using the source code files:
<tt>example_wrap.c</tt>, generated by swig, and <tt>example.c</tt>, your original c
source. The swig (and other Python extension modules) tradition is for the compiled
extension to have the name of the Python portion, prefixed by an underscore. If the name
of your Python module is "<tt>example.py</tt>", then the name of the corresponding object file
will be"<tt>_example.so</tt>"
</p>
<p>
The <tt>setup</tt> call then sets up distutils to build your package, defining
some meta data, and passing in your extension module object.
Once this is saved as <tt>setup.py</tt>, you can build your extension with these commands:
</p>
<div class="shell"><pre>
$ swig -python example.i
$ python setup.py build_ext --inplace
</pre></div>
<p>
And a .so, or .pyd or... will be created for you. It will build a version that matches the
Python that you run the command with. Taking apart the command line:
</p>
<ul>
<li> <tt>python</tt> -- the version of Python you want to build for
<li> <tt>setup.py</tt> -- the name of your setup script (it can be called anything, but
setup.py is the tradition)
<li> <tt>build_ext</tt> -- telling distutils to build extensions
<li> <tt>--inplace</tt> -- this tells distutils to put the extension lib in the current dir.
Otherwise, it will put it inside a build hierarchy, and you'd have to move it to use it.
</ul>
<p>
The distutils have many other features, consult the Python distutils docs for details.
</p>
<p>
This same approach works on all platforms if the appropriate compiler is installed. (it
can even build extensions to the standard Windows Python using MingGW)
</p>
<H3><a name="Python_nn7">33.2.3 Hand compiling a dynamic module</a></H3>
<p>
While the preferred approach to building an extension module is to use the distutils, some
people like to integrate building extensions with a larger build system, and thus may wish
to compile their modules without the distutils. To do this, you need to compile your
program using commands like this (shown for Linux):
</p>
<div class="shell"><pre>
$ swig -python example.i
$ gcc -O2 -fPIC -c example.c
$ gcc -O2 -fPIC -c example_wrap.c -I/usr/local/include/python2.5
$ gcc -shared example.o example_wrap.o -o _example.so
</pre></div>
<p>
The exact commands for doing this vary from platform to platform.
However, SWIG tries to guess the right options when it is installed. Therefore,
you may want to start with one of the examples in the <tt>SWIG/Examples/python</tt>
directory. If that doesn't work, you will need to read the man-pages for
your compiler and linker to get the right set of options. You might also
check the <a href="https://github.com/swig/swig/wiki">SWIG Wiki</a> for
additional information.
</p>
<p>
When linking the module, <b>the name of the output file has to match the name
of the module prefixed by an underscore</b>. If the name of your module is "<tt>example</tt>", then the
name of the corresponding object file should be
"<tt>_example.so</tt>" or "<tt>_examplemodule.so</tt>".
The name of the module is specified using the <tt>%module</tt> directive or the
<tt>-module</tt> command line option.
</p>
<p>
<b>Compatibility Note:</b> In SWIG-1.3.13 and earlier releases, module
names did not include the leading underscore. This is because modules
were normally created as C-only extensions without the extra Python
support file (instead, creating Python code was supported as an optional
feature). This has been changed in SWIG-1.3.14 and is consistent with
other Python extension modules. For example, the <tt>socket</tt>
module actually consists of two files; <tt>socket.py</tt> and
<tt>_socket.so</tt>. Many other built-in Python modules follow a similar convention.
</p>
<H3><a name="Python_nn8">33.2.4 Static linking</a></H3>
<p>
An alternative approach to dynamic linking is to rebuild the Python
interpreter with your extension module added to it. In the past,
this approach was sometimes necessary due to limitations in dynamic loading
support on certain machines. However, the situation has improved greatly
over the last few years and you should not consider this approach
unless there is really no other option.
</p>
<p>
The usual procedure for adding a new module to Python involves finding
the Python source, adding an entry to the <tt>Modules/Setup</tt> file,
and rebuilding the interpreter using the Python Makefile. However,
newer Python versions have changed the build process. You may need to edit
the 'setup.py' file in the Python distribution instead.
</p>
<p>
In earlier versions of SWIG, the <tt>embed.i</tt> library file could be used to
rebuild the interpreter. For example:
</p>
<div class="code"><pre>
%module example
%inline %{
extern int fact(int);
extern int mod(int, int);
extern double My_variable;
%}
%include "embed.i" // Include code for a static version of Python
</pre></div>
<p>
The <tt>embed.i</tt> library file includes supporting code that
contains everything needed to rebuild Python. To rebuild the interpreter,
you simply do something like this:
</p>
<div class="shell"><pre>
$ swig -python -lembed.i example.i
$ gcc example.c example_wrap.c \
-Xlinker -export-dynamic \
-DHAVE_CONFIG_H -I/usr/include/python2.7 \
-I/usr/lib/python2.7/config-x86_64-linux-gnu \
-I/usr/lib/python2.7/config \
-L/usr/lib/python2.7/config -lpython2.7 -lm -ldl \
-o mypython
</pre></div>
<p>
You will need to supply the same libraries that were used to build Python the first
time. This may include system libraries such as <tt>-lsocket</tt>, <tt>-lnsl</tt>,
and <tt>-lpthread</tt>. Assuming this actually works, the new version of Python
should be identical to the default version except that your extension module will be
a built-in part of the interpreter.
</p>
<p>
<b>Comment:</b> In practice, you should probably try to avoid static
linking if possible. Some programmers may be inclined
to use static linking in the interest of getting better performance.
However, the performance gained by static linking tends to be rather
minimal in most situations (and quite frankly not worth the extra
hassle in the opinion of this author).
</p>
<p>
<b>Compatibility note:</b> The <tt>embed.i</tt> library file is
deprecated and has not been actively maintained for many years. Even though it
appears to "work" with Python 2.7, no future support is guaranteed.
If using static linking, you might want to rely on a different approach
(perhaps using distutils).
</p>
<H3><a name="Python_nn9">33.2.5 Using your module</a></H3>
<p>
To use your module, simply use the Python <tt>import</tt> statement. If
all goes well, you will be able to run this:
</p>
<div class="targetlang"><pre>
$ python
&gt;&gt;&gt; import example
&gt;&gt;&gt; example.fact(4)
24
&gt;&gt;&gt;
</pre></div>
<p>
A common error received by first-time users is the following:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; import example
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in ?
File "example.py", line 2, in ?
import _example
ImportError: No module named _example
</pre>
</div>
<p>
If you get this message, it means that you either forgot to compile the wrapper
code into an extension module or you didn't give the extension module the right
name. Make sure that you compiled the wrappers into a module called <tt>_example.so</tt>. And
don't forget the leading underscore (_).
</p>
<p>
Another possible error is the following:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; import example
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in ?
ImportError: dynamic module does not define init function (init_example)
&gt;&gt;&gt;
</pre>
</div>
<p>
This error is almost always caused when a bad name is given to the shared object file.
For example, if you created a file <tt>example.so</tt> instead of <tt>_example.so</tt> you would
get this error. Alternatively, this error could arise if the name of the module is
inconsistent with the module name supplied with the <tt>%module</tt> directive.
Double-check the interface to make sure the module name and the shared object
filename match. Another possible cause of this error is forgetting to link the SWIG-generated
wrapper code with the rest of your application when creating the extension module.
</p>
<p>
Another common error is something similar to the following:
</p>
<div class="targetlang">
<pre>
Traceback (most recent call last):
File "example.py", line 3, in ?
import example
ImportError: ./_example.so: undefined symbol: fact
</pre>
</div>
<p>
This error usually indicates that you forgot to include some object
files or libraries in the linking of the shared library file. Make
sure you compile both the SWIG wrapper file and your original program
into a shared library file. Make sure you pass all of the required libraries
to the linker.
</p>
<p>
Sometimes unresolved symbols occur because a wrapper has been created
for a function that doesn't actually exist in a library. This usually
occurs when a header file includes a declaration for a function that
was never actually implemented or it was removed from a library
without updating the header file. To fix this, you can either edit
the SWIG input file to remove the offending declaration or you can use
the <tt>%ignore</tt> directive to ignore the declaration.
</p>
<p>
Finally, suppose that your extension module is linked with another library like this:
</p>
<div class="shell">
<pre>
$ gcc -shared example.o example_wrap.o -L/home/beazley/projects/lib <b>-lfoo</b> \
-o _example.so
</pre>
</div>
<p>
If the <tt>foo</tt> library is compiled as a shared library, you might encounter the following
problem when you try to use your module:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; import example
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in ?
ImportError: libfoo.so: cannot open shared object file: No such file or directory
&gt;&gt;&gt;
</pre>
</div>
<p>
This error is generated because the dynamic linker can't locate the
<tt>libfoo.so</tt> library. When shared libraries are loaded, the
system normally only checks a few standard locations such as
<tt>/usr/lib</tt> and <tt>/usr/local/lib</tt>. To fix this problem,
there are several things you can do. First, you can recompile your extension
module with extra path information. For example, on Linux you can do this:
</p>
<div class="shell">
<pre>
$ gcc -shared example.o example_wrap.o -L/home/beazley/projects/lib -lfoo \
<b>-Xlinker -rpath /home/beazley/projects/lib </b> \
-o _example.so
</pre>
</div>
<p>
Alternatively, you can set the <tt>LD_LIBRARY_PATH</tt> environment variable to
include the directory with your shared libraries.
If setting <tt>LD_LIBRARY_PATH</tt>, be aware that setting this variable can introduce
a noticeable performance impact on all other applications that you run.
To set it only for Python, you might want to do this instead:
</p>
<div class="shell">
<pre>
$ env LD_LIBRARY_PATH=/home/beazley/projects/lib python
</pre>
</div>
<p>
Finally, you can use a command such as <tt>ldconfig</tt> (Linux) or
<tt>crle</tt> (Solaris) to add additional search paths to the default
system configuration (this requires root access and you will need to
read the man pages).
</p>
<H3><a name="Python_nn10">33.2.6 Compilation of C++ extensions</a></H3>
<p>
Compilation of C++ extensions has traditionally been a tricky problem.
Since the Python interpreter is written in C, you need to take steps to
make sure C++ is properly initialized and that modules are compiled
correctly. This should be a non-issue if you're using distutils, as
it takes care of all that for you. The following is included for
historical reasons, and in case you need to compile on your own.
</p>
<p>
On most machines, C++ extension modules should be linked using the C++
compiler. For example:
</p>
<div class="shell"><pre>
$ swig -c++ -python example.i
$ g++ -O2 -fPIC -c example.cxx
$ g++ -O2 -fPIC -c example_wrap.cxx -I/usr/local/include/python2.5
$ g++ -shared example.o example_wrap.o -o _example.so
</pre></div>
<p>
The -fPIC option tells GCC to generate position-independent code (PIC)
which is required for most architectures (it's not vital on x86, but
still a good idea as it allows code pages from the library to be shared between
processes). Other compilers may need a different option specified instead of
-fPIC.
</p>
<p>
In addition to this, you may need to include additional library
files to make it work. For example, if you are using the Sun C++ compiler on
Solaris, you often need to add an extra library <tt>-lCrun</tt> like this:
</p>
<div class="shell"><pre>
$ swig -c++ -python example.i
$ CC -c example.cxx
$ CC -c example_wrap.cxx -I/usr/local/include/python2.5
$ CC -G example.o example_wrap.o -L/opt/SUNWspro/lib -o _example.so -lCrun
</pre></div>
<p>
Of course, the extra libraries to use are completely non-portable---you will
probably need to do some experimentation.
</p>
<p>
Sometimes people have suggested that it is necessary to relink the
Python interpreter using the C++ compiler to make C++ extension modules work.
In the experience of this author, this has never actually appeared to be
necessary. Relinking the interpreter with C++ really only includes the
special run-time libraries described above---as long as you link your extension
modules with these libraries, it should not be necessary to rebuild Python.
</p>
<p>
If you aren't entirely sure about the linking of a C++ extension, you
might look at an existing C++ program. On many Unix machines, the
<tt>ldd</tt> command will list library dependencies. This should give
you some clues about what you might have to include when you link your
extension module. For example:
</p>
<div class="shell">
<pre>
$ ldd swig
libstdc++-libc6.1-1.so.2 =&gt; /usr/lib/libstdc++-libc6.1-1.so.2 (0x40019000)
libm.so.6 =&gt; /lib/libm.so.6 (0x4005b000)
libc.so.6 =&gt; /lib/libc.so.6 (0x40077000)
/lib/ld-linux.so.2 =&gt; /lib/ld-linux.so.2 (0x40000000)
</pre>
</div>
<p>
As a final complication, a major weakness of C++ is that it does not
define any sort of standard for binary linking of libraries. This
means that C++ code compiled by different compilers will not link
together properly as libraries nor is the memory layout of classes and
data structures implemented in any kind of portable manner. In a
monolithic C++ program, this problem may be unnoticed. However, in Python, it
is possible for different extension modules to be compiled with
different C++ compilers. As long as these modules are self-contained,
this probably won't matter. However, if these modules start sharing data,
you will need to take steps to avoid segmentation faults and other
erratic program behavior. If working with lots of software components, you
might want to investigate using a more formal standard such as COM.
</p>
<H3><a name="Python_nn11">33.2.7 Compiling for 64-bit platforms</a></H3>
<p>
On platforms that support 64-bit applications (Solaris, Irix, etc.),
special care is required when building extension modules. On these
machines, 64-bit applications are compiled and linked using a different
set of compiler/linker options. In addition, it is not generally possible to mix
32-bit and 64-bit code together in the same application.
</p>
<p>
To utilize 64-bits, the Python executable will need to be recompiled
as a 64-bit application. In addition, all libraries, wrapper code,
and every other part of your application will need to be compiled for
64-bits. If you plan to use other third-party extension modules, they
will also have to be recompiled as 64-bit extensions.
</p>
<p>
If you are wrapping commercial software for which you have no source
code, you will be forced to use the same linking standard as used by
that software. This may prevent the use of 64-bit extensions. It may
also introduce problems on platforms that support more than one
linking standard (e.g., -o32 and -n32 on Irix).
</p>
<p> On the Linux x86_64 platform (Opteron or EM64T), besides of the
required compiler option -fPIC discussed above, you will need to be
careful about the libraries you link with or the library path you
use. In general, a Linux distribution will have two set of libraries,
one for native x86_64 programs (under /usr/lib64), and another for 32
bits compatibility (under /usr/lib). Also, the compiler options -m32
and -m64 allow you to choose the desired binary format for your Python
extension.
</p>
<H3><a name="Python_nn12">33.2.8 Building Python extensions under Windows</a></H3>
<p>
Building a SWIG extension to Python under Windows is roughly similar to
the process used with Unix. Using the distutils, it is essentially
identical. If you have the same version of the MS compiler that Python
was built with (the python2.4 and python2.5 distributed by python.org
are built with Visual Studio 2003), the standard <tt>python setup.py
build</tt> should just work.
</p>
<p>
As of python2.5, the distutils support building extensions with MingGW out
of the box. Following the instruction here:
<a href="http://boodebr.org/main/python/build-windows-extensions">Building
Python extensions for Windows with only free tools</a> should get you started.
</p>
<p>
If you need to build it on your own, the following notes are provided:
</p>
<p>
You will need to create a DLL that can be loaded into the interpreter.
This section briefly describes the use of SWIG with Microsoft Visual
C++. As a starting point, many of SWIG's examples include project
files (.dsp files) for Visual C++ 6. These can be opened by more
recent versions of Visual Studio.
You might want to take a quick look at these examples in addition to
reading this section.
</p>
<p>
In Developer Studio, SWIG should be invoked as a custom build option.
This is usually done as follows:
</p>
<ul>
<li>Open up a new workspace and use the AppWizard to select a DLL
project.
<li>Add both the SWIG interface file (the .i file), any supporting C
files, and the name of the wrapper file that will be created by SWIG
(ie. <tt>example_wrap.c</tt>). Note : If using C++, choose a
different suffix for the wrapper file such as
<tt>example_wrap.cxx</tt>. Don't worry if the wrapper file doesn't
exist yet--Developer Studio keeps a reference to it.
<li>Select the SWIG interface file and go to the settings menu. Under
settings, select the "Custom Build" option.
<li>Enter "SWIG" in the description field.
<li>Enter "<tt>swig -python -o $(ProjDir)\$(InputName)_wrap.c $(InputPath)</tt>" in the "Build command(s) field"
<li>Enter "<tt>$(ProjDir)\$(InputName)_wrap.c</tt>" in the "Output files(s) field".
<li>Next, select the settings for the entire project and go to
"C++:Preprocessor". Add the include directories for your Python
installation under "Additional include directories".
<li>Define the symbol __WIN32__ under preprocessor options.
<li>Finally, select the settings for the entire project and go to
"Link Options". Add the Python library file to your link libraries.
For example "python27.lib". Also, set the name of the output file to
match the name of your Python module, i.e. <tt>_example.pyd</tt>
<li>Build your project.
</ul>
<p>
If all went well, SWIG will be automatically invoked whenever
you build your project. Any changes made to the interface file will
result in SWIG being automatically executed to produce a new version of
the wrapper file.
</p>
<p>
To run your new Python extension, simply run Python
and use the <tt>import</tt> command as normal. For example :
</p>
<div class="targetlang"><pre>
$ python
&gt;&gt;&gt; import example
&gt;&gt;&gt; print example.fact(4)
24
&gt;&gt;&gt;
</pre></div>
<p>
If you get an <tt>ImportError</tt> exception when importing the module, you may
have forgotten to include additional library files when you built your module.
If you get an access violation or some kind of general protection fault
immediately upon import, you have a more serious problem. This
is often caused by linking your extension module against the wrong
set of Win32 debug or thread libraries. You will have to fiddle around with
the build options of project to try and track this down.
</p>
<p>
A 'Debug' build of the wrappers requires a debug build of the Python interpreter.
This normally requires building the Python interpreter from source, which is not a
job for the feint-hearted. Alternatively you can use the 'Release' build of the
Python interpreter with a 'Debug' build of your wrappers by defining the <tt>SWIG_PYTHON_INTERPRETER_NO_DEBUG</tt>
symbol under the preprocessor options. Or you can ensure this macro is defined at the beginning
of the wrapper code using the following in your interface file, where <tt>_MSC_VER</tt> ensures it is
only used by the Visual Studio compiler:
</p>
<div class="code"><pre>
%begin %{
#ifdef _MSC_VER
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
#endif
%}
</pre></div>
<p>
Some users have reported success in building extension modules using Cygwin
and other compilers. However, the problem of building usable DLLs with these
compilers tends to be rather problematic. For the latest information,
you may want to consult the <a href="https://github.com/swig/swig/wiki">
SWIG Wiki</a>.
</p>
<H3><a name="Python_commandline">33.2.9 Additional Python commandline options</a></H3>
<p>
The following table lists the additional commandline options available for the Python module. They can also be seen by using:
</p>
<div class="code"><pre>
swig -python -help
</pre></div>
<table summary="Python specific options">
<tr>
<th>Python specific options</th>
</tr>
<tr><td>-builtin </td><td>Create Python built-in types rather than proxy classes, for better performance</td></tr>
<tr><td>-castmode </td><td>Enable the casting mode, which allows implicit cast between types in Python</td></tr>
<tr><td>-debug-doxygen-parser </td><td>Display doxygen parser module debugging information</td></tr>
<tr><td>-debug-doxygen-translator </td><td>Display doxygen translator module debugging information</td></tr>
<tr><td>-dirvtable </td><td>Generate a pseudo virtual table for directors for faster dispatch</td></tr>
<tr><td>-doxygen </td><td>Convert C++ doxygen comments to pydoc comments in proxy classes</td></tr>
<tr><td>-extranative </td><td>Return extra native wrappers for C++ std containers wherever possible</td></tr>
<tr><td>-fastproxy </td><td>Use fast proxy mechanism for member methods</td></tr>
<tr><td>-globals &lt;name&gt; </td><td>Set &lt;name&gt; used to access C global variable (default: 'cvar')</td></tr>
<tr><td>-interface &lt;mod&gt;</td><td>Set low-level C/C++ module name to &lt;mod&gt; (default: module name prefixed by '_')</td></tr>
<tr><td>-keyword </td><td>Use keyword arguments</td></tr>
<tr><td>-nofastunpack </td><td>Use traditional UnpackTuple method to parse the argument functions</td></tr>
<tr><td>-noh </td><td>Don't generate the output header file</td></tr>
<tr><td>-noproxy </td><td>Don't generate proxy classes</td></tr>
<tr><td>-nortti </td><td>Disable the use of the native C++ RTTI with directors</td></tr>
<tr><td>-nothreads </td><td>Disable thread support for the entire interface</td></tr>
<tr><td>-olddefs </td><td>Keep the old method definitions when using -fastproxy</td></tr>
<tr><td>-py3 </td><td>Generate code with Python 3 specific features and syntax</td></tr>
<tr><td>-relativeimport </td><td>Use relative Python imports</td></tr>
<tr><td>-threads </td><td>Add thread support for all the interface</td></tr>
<tr><td>-O </td><td>Enable the following optimization options: -fastdispatch -fastproxy -fvirtual</td></tr>
</table>
<p>
Many of these options are covered later on and their use should become clearer by the time you have finished reading this section on SWIG and Python.
</p>
<H2><a name="Python_nn13">33.3 A tour of basic C/C++ wrapping</a></H2>
<p>
By default, SWIG tries to build a very natural Python interface
to your C/C++ code. Functions are wrapped as functions, classes are wrapped as classes, and so forth.
This section briefly covers the essential aspects of this wrapping.
</p>
<H3><a name="Python_nn14">33.3.1 Modules</a></H3>
<p>
The SWIG <tt>%module</tt> directive specifies the name of the Python
module. If you specify `<tt>%module example</tt>', then everything is
wrapped into a Python '<tt>example</tt>' module. Underneath the covers,
this module consists of a Python source file <tt>example.py</tt> and a low-level
extension module <tt>_example.so</tt>. When choosing a
module name, make sure you don't use the same name as a built-in
Python command or standard module name.
</p>
<H3><a name="Python_nn15">33.3.2 Functions</a></H3>
<p>
Global functions are wrapped as new Python built-in functions. For example,
</p>
<div class="code"><pre>
%module example
int fact(int n);
</pre></div>
<p>
creates a built-in function <tt>example.fact(n)</tt> that works exactly
like you think it does:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; import example
&gt;&gt;&gt; print example.fact(4)
24
&gt;&gt;&gt;
</pre></div>
<H3><a name="Python_nn16">33.3.3 Global variables</a></H3>
<p>
C/C++ global variables are fully supported by SWIG. However, the underlying
mechanism is somewhat different than you might expect due to the way that
Python assignment works. When you type the following in Python
</p>
<div class="targetlang"><pre>
a = 3.4
</pre></div>
<p>
"a" becomes a name for an object containing the value 3.4. If you later type
</p>
<div class="targetlang"><pre>
b = a
</pre></div>
<p>
then "a" and "b" are both names for the object containing the value
3.4. Thus, there is only one object containing 3.4 and "a"
and "b" are both names that refer to it. This is quite
different than C where a variable name refers to a memory location in which
a value is stored (and assignment copies data into that location).
Because of this, there is no direct way to map variable
assignment in C to variable assignment in Python.
</p>
<p>
To provide access to C global variables, SWIG creates a special
object called `<tt>cvar</tt>' that is added to each SWIG generated
module. Global variables are then accessed as attributes of this object.
For example, consider this interface
</p>
<div class="code"><pre>
// SWIG interface file with global variables
%module example
...
%inline %{
extern int My_variable;
extern double density;
%}
...
</pre></div>
<p>
Now look at the Python interface:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; import example
&gt;&gt;&gt; # Print out value of a C global variable
&gt;&gt;&gt; print example.cvar.My_variable
4
&gt;&gt;&gt; # Set the value of a C global variable
&gt;&gt;&gt; example.cvar.density = 0.8442
&gt;&gt;&gt; # Use in a math operation
&gt;&gt;&gt; example.cvar.density = example.cvar.density*1.10
</pre></div>
<p>
If you make an error in variable assignment, you will receive an
error message. For example:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; example.cvar.density = "Hello"
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in ?
TypeError: C variable 'density (double )'
&gt;&gt;&gt;
</pre></div>
<p>
If a variable is declared as <tt>const</tt>, it is wrapped as a
read-only variable. Attempts to modify its value will result in an
error.
</p>
<p>
To make ordinary variables read-only, you can use the <tt>%immutable</tt> directive. For example:
</p>
<div class="code">
<pre>
%{
extern char *path;
%}
%immutable;
extern char *path;
%mutable;
</pre>
</div>
<p>
The <tt>%immutable</tt> directive stays in effect until it is explicitly disabled or cleared using
<tt>%mutable</tt>.
See the <a href="SWIG.html#SWIG_readonly_variables">Creating read-only variables</a> section for further details.
</p>
<p>
If you just want to make a specific variable immutable, supply a declaration name. For example:
</p>
<div class="code">
<pre>
%{
extern char *path;
%}
%immutable path;
...
extern char *path; // Read-only (due to %immutable)
</pre>
</div>
<p>
If you would like to access variables using a name other than "<tt>cvar</tt>", it can be
changed using the <tt>-globals</tt> option :
</p>
<div class="shell"><pre>
$ swig -python -globals myvar example.i
</pre></div>
<p>
Some care is in order when importing multiple SWIG modules.
If you use the "<tt>from &lt;file&gt; import *</tt>" style of
importing, you will get a name clash on the variable `<tt>cvar</tt>'
and you will only be able to access global variables from the last
module loaded. To prevent this, you might consider renaming
<tt>cvar</tt> or making it private to the module by giving it a name
that starts with a leading underscore. SWIG does not create <tt>cvar</tt>
if there are no global variables in a module.
</p>
<H3><a name="Python_nn17">33.3.4 Constants and enums</a></H3>
<p>
C/C++ constants are installed as Python objects containing the
appropriate value. To create a constant, use <tt>#define</tt>, <tt>enum</tt>, or the
<tt>%constant</tt> directive. For example:
</p>
<div class="code">
<pre>
#define PI 3.14159
#define VERSION "1.0"
enum Beverage { ALE, LAGER, STOUT, PILSNER };
%constant int FOO = 42;
%constant const char *path = "/usr/local";
</pre>
</div>
<p>
For enums, make sure that the definition of the enumeration actually appears in a header
file or in the wrapper file somehow---if you just stick an enum in a SWIG interface without
also telling the C compiler about it, the wrapper code won't compile.
</p>
<p>
Note: declarations declared as <tt>const</tt> are wrapped as read-only variables and
will be accessed using the <tt>cvar</tt> object described in the previous section. They
are not wrapped as constants. For further discussion about this, see the <a href="SWIG.html#SWIG">SWIG Basics</a> chapter.
</p>
<p>
Constants are not guaranteed to remain constant in Python---the name
of the constant could be accidentally reassigned to refer to some
other object. Unfortunately, there is no easy way for SWIG to
generate code that prevents this. You will just have to be careful.
</p>
<H3><a name="Python_nn18">33.3.5 Pointers</a></H3>
<p>
C/C++ pointers are fully supported by SWIG. Furthermore, SWIG has no
problem working with incomplete type information. Here is a rather
simple interface:
</p>
<div class="code">
<pre>
%module example
FILE *fopen(const char *filename, const char *mode);
int fputs(const char *, FILE *);
int fclose(FILE *);
</pre>
</div>
<p>
When wrapped, you will be able to use the functions in a natural way from Python. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; import example
&gt;&gt;&gt; f = example.fopen("junk", "w")
&gt;&gt;&gt; example.fputs("Hello World\n", f)
&gt;&gt;&gt; example.fclose(f)
</pre>
</div>
<p>
If this makes you uneasy, rest assured that there is no
deep magic involved. Underneath the covers, pointers to C/C++ objects are
simply represented as opaque values using an especial Python container object:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; print f
&lt;Swig Object of type 'FILE *' at 0xb7d6f470&gt;
</pre></div>
<p>
This pointer value can be freely passed around to different C functions that
expect to receive an object of type <tt>FILE *</tt>. The only thing you can't do is
dereference the pointer from Python. Of course, that isn't much of a concern in this example.
</p>
<p>
In older versions of SWIG (1.3.22 or older), pointers were represented
using a plain string object. If you have an old package that still
requires that representation, or you just feel nostalgic, you can
always retrieve it by casting the pointer object to a string:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; print str(f)
_c0671108_p_FILE
</pre></div>
<p>
Also, if you need to pass the raw pointer value to some external
Python library, you can do it by casting the pointer object to an
integer:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; print int(f)
135833352
</pre></div>
<p>
However, the inverse operation is not possible, i.e., you can't build
a SWIG pointer object from a raw integer value.
</p>
<p>
Note also that the '0' or NULL pointer is always represented by
<tt>None</tt>, no matter what type swig is addressing. In the
previous example, you can call:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; example.fclose(None)
</pre>
</div>
<p>
and that will be equivalent to the following, but not really useful, C
code:
</p>
<div class="code">
<pre>
FILE *f = NULL;
fclose(f);
</pre>
</div>
<p>
As much as you might be inclined to modify a pointer value directly
from Python, don't. The hexadecimal encoding is not necessarily the
same as the logical memory address of the underlying object. Instead
it is the raw byte encoding of the pointer value. The encoding will
vary depending on the native byte-ordering of the platform (i.e.,
big-endian vs. little-endian). Similarly, don't try to manually cast
a pointer to a new type by simply replacing the type-string. This may
not work like you expect, it is particularly dangerous when casting
C++ objects. If you need to cast a pointer or change its value,
consider writing some helper functions instead. For example:
</p>
<div class="code">
<pre>
%inline %{
/* C-style cast */
Bar *FooToBar(Foo *f) {
return (Bar *) f;
}
/* C++-style cast */
Foo *BarToFoo(Bar *b) {
return dynamic_cast&lt;Foo*&gt;(b);
}
Foo *IncrFoo(Foo *f, int i) {
return f+i;
}
%}
</pre>
</div>
<p>
Also, if working with C++, you should always try
to use the new C++ style casts. For example, in the above code, the
C-style cast may return a bogus result whereas as the C++-style cast will return
<tt>None</tt> if the conversion can't be performed.
</p>
<H3><a name="Python_nn19">33.3.6 Structures</a></H3>
<p>
If you wrap a C structure, it is wrapped by a Python class. This provides
a very natural interface. For example,
</p>
<div class="code"><pre>
struct Vector {
double x, y, z;
};
</pre></div>
<p>
is used as follows:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; v = example.Vector()
&gt;&gt;&gt; v.x = 3.5
&gt;&gt;&gt; v.y = 7.2
&gt;&gt;&gt; print v.x, v.y, v.z
3.5 7.2 0.0
&gt;&gt;&gt;
</pre></div>
<p>
Similar access is provided for unions and the data members of C++ classes.
</p>
<p>
If you print out the value of <tt>v</tt> in the above example, you will see
something like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; print v
&lt;C Vector instance at _18e31408_p_Vector&gt;
</pre>
</div>
<p>
This object is actually a Python instance that has been wrapped around a pointer to the low-level
C structure. This instance doesn't actually do anything--it just serves as a proxy.
The pointer to the C object can be found in the <tt>.this</tt>
attribute. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; print v.this
_18e31408_p_Vector
&gt;&gt;&gt;
</pre>
</div>
<p>
Further details about the Python proxy class are covered a little later.
</p>
<p>
<tt>const</tt> members of a structure are read-only. Data members
can also be forced to be read-only using the <tt>%immutable</tt> directive. For example:
</p>
<div class="code">
<pre>
struct Foo {
...
%immutable;
int x; /* Read-only members */
char *name;
%mutable;
...
};
</pre>
</div>
<p>
When <tt>char *</tt> members of a structure are wrapped, the contents are assumed to be
dynamically allocated using <tt>malloc</tt> or <tt>new</tt> (depending on whether or not
SWIG is run with the -c++ option). When the structure member is set, the old contents will be
released and a new value created. If this is not the behavior you want, you will have to use
a typemap (described later).
</p>
<p>
If a structure contains arrays, access to those arrays is managed through pointers. For
example, consider this:
</p>
<div class="code">
<pre>
struct Bar {
int x[16];
};
</pre>
</div>
<p>
If accessed in Python, you will see behavior like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; b = example.Bar()
&gt;&gt;&gt; print b.x
_801861a4_p_int
&gt;&gt;&gt;
</pre>
</div>
<p>
This pointer can be passed around to functions that expect to receive
an <tt>int *</tt> (just like C). You can also set the value of an array member using
another pointer. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; c = example.Bar()
&gt;&gt;&gt; c.x = b.x # Copy contents of b.x to c.x
</pre>
</div>
<p>
For array assignment, SWIG copies the entire contents of the array starting with the data pointed
to by <tt>b.x</tt>. In this example, 16 integers would be copied. Like C, SWIG makes
no assumptions about bounds checking---if you pass a bad pointer, you may get a segmentation
fault or access violation.
</p>
<p>
When a member of a structure is itself a structure, it is handled as a
pointer. For example, suppose you have two structures like this:
</p>
<div class="code">
<pre>
struct Foo {
int a;
};
struct Bar {
Foo f;
};
</pre>
</div>
<p>
Now, suppose that you access the <tt>f</tt> attribute of <tt>Bar</tt> like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; b = Bar()
&gt;&gt;&gt; x = b.f
</pre>
</div>
<p>
In this case, <tt>x</tt> is a pointer that points to the <tt>Foo</tt> that is inside <tt>b</tt>.
This is the same value as generated by this C code:
</p>
<div class="code">
<pre>
Bar b;
Foo *x = &amp;b-&gt;f; /* Points inside b */
</pre>
</div>
<p>
Because the pointer points inside the structure, you can modify the contents and
everything works just like you would expect. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; b = Bar()
&gt;&gt;&gt; b.f.a = 3 # Modify attribute of structure member
&gt;&gt;&gt; x = b.f
&gt;&gt;&gt; x.a = 3 # Modifies the same structure
</pre>
</div>
<p>
Note that there is a limitation with structs within structs that will cause a problem
if the outer struct is not a named variable in Python. The following will cause a segfault:
</p>
<div class="targetlang">
<pre>
Bar().f.a = 3
</pre>
</div>
<p>
because the unnamed Python proxy class for <tt>Bar()</tt> has its reference count
decremented by the Python interpreter after <tt>f</tt> has been obtained from it and
before <tt>f</tt> is used to obtain <tt>a</tt>.
This results in the underlying <tt>Bar</tt> instance being deleted, which of course also deletes
<tt>f</tt> inside it. Hence the pointer to <tt>f</tt> points to deleted
memory and use of it results in a segfault or some sort of other undefined behaviour.
</p>
<H3><a name="Python_nn20">33.3.7 C++ classes</a></H3>
<p>
C++ classes are wrapped by Python classes as well. For example, if you have this class,
</p>
<div class="code"><pre>
class List {
public:
List();
~List();
int search(char *item);
void insert(char *item);
void remove(char *item);
char *get(int n);
int length;
};
</pre></div>
<p>
you can use it in Python like this:
</p>
<div class="targetlang"><pre>
&gt;&gt;&gt; l = example.List()
&gt;&gt;&gt; l.insert("Ale")
&gt;&gt;&gt; l.insert("Stout")
&gt;&gt;&gt; l.insert("Lager")
&gt;&gt;&gt; l.get(1)
'Stout'
&gt;&gt;&gt; print l.length
3
&gt;&gt;&gt;
</pre></div>
<p>
Class data members are accessed in the same manner as C structures.
</p>
<p>
Static class members present a special problem for Python. Prior to Python-2.2,
Python classes had no support for static methods and no version of Python
supports static member variables in a manner that SWIG can utilize. Therefore,
SWIG generates wrappers that try to work around some of these issues. To illustrate,
suppose you have a class like this:
</p>
<div class="code">
<pre>
class Spam {
public:
static void foo();
static int bar;
};
</pre>
</div>
<p>
In Python, the static member can be access in three different ways:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; example.Spam_foo() # Spam::foo()
&gt;&gt;&gt; s = example.Spam()
&gt;&gt;&gt; s.foo() # Spam::foo() via an instance
&gt;&gt;&gt; example.Spam.foo() # Spam::foo(). Python-2.2 only
</pre>
</div>
<p>
The first two methods of access are supported in all versions of Python. The
last technique is only available in Python-2.2 and later versions.
</p>
<p>
Static member variables are currently accessed as global variables. This means,
they are accessed through <tt>cvar</tt> like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; print example.cvar.Spam_bar
7
</pre>
</div>
<H3><a name="Python_nn21">33.3.8 C++ inheritance</a></H3>
<p>
SWIG is fully aware of issues related to C++ inheritance. Therefore, if you have
classes like this
</p>
<div class="code">
<pre>
class Foo {
...
};
class Bar : public Foo {
...
};
</pre>
</div>
<p>
those classes are wrapped into a hierarchy of Python classes that reflect the same inheritance
structure. All of the usual Python utility functions work normally:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; b = Bar()
&gt;&gt;&gt; instance(b, Foo)
1
&gt;&gt;&gt; issubclass(Bar, Foo)
1
&gt;&gt;&gt; issubclass(Foo, Bar)
0
</pre>
</div>
<p>
Furthermore, if you have functions like this
</p>
<div class="code">
<pre>
void spam(Foo *f);
</pre>
</div>
<p>
then the function <tt>spam()</tt> accepts <tt>Foo *</tt> or a pointer to any class derived from <tt>Foo</tt>.
</p>
<p>
It is safe to use multiple inheritance with SWIG.
</p>
<H3><a name="Python_nn22">33.3.9 Pointers, references, values, and arrays</a></H3>
<p>
In C++, there are many different ways a function might receive
and manipulate objects. For example:
</p>
<div class="code">
<pre>
void spam1(Foo *x); // Pass by pointer
void spam2(Foo &amp;x); // Pass by reference
void spam3(const Foo &amp;x);// Pass by const reference
void spam4(Foo x); // Pass by value
void spam5(Foo x[]); // Array of objects
</pre>
</div>
<p>
In Python, there is no detailed distinction like this--specifically,
there are only "objects". There are no pointers, references, arrays,
and so forth. Because of this, SWIG unifies all of these types
together in the wrapper code. For instance, if you actually had the
above functions, it is perfectly legal to do this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = Foo() # Create a Foo
&gt;&gt;&gt; spam1(f) # Ok. Pointer
&gt;&gt;&gt; spam2(f) # Ok. Reference
&gt;&gt;&gt; spam3(f) # Ok. Const reference
&gt;&gt;&gt; spam4(f) # Ok. Value.
&gt;&gt;&gt; spam5(f) # Ok. Array (1 element)
</pre>
</div>
<p>
Similar behavior occurs for return values. For example, if you had
functions like this,
</p>
<div class="code">
<pre>
Foo *spam6();
Foo &amp;spam7();
Foo spam8();
const Foo &amp;spam9();
</pre>
</div>
<p>
then all three functions will return a pointer to some <tt>Foo</tt> object.
Since the third function (spam8) returns a value, newly allocated memory is used
to hold the result and a pointer is returned (Python will release this memory
when the return value is garbage collected). The fourth case (spam9)
which returns a const reference, in most of the cases will be
treated as a returning value, and it will follow the same
allocation/deallocation process.
</p>
<H3><a name="Python_nn23">33.3.10 C++ overloaded functions</a></H3>
<p>
C++ overloaded functions, methods, and constructors are mostly supported by SWIG. For example,
if you have two functions like this:
</p>
<div class="code">
<pre>
void foo(int);
void foo(char *c);
</pre>
</div>
<p>
You can use them in Python in a straightforward manner:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; foo(3) # foo(int)
&gt;&gt;&gt; foo("Hello") # foo(char *c)
</pre>
</div>
<p>
Similarly, if you have a class like this,
</p>
<div class="code">
<pre>
class Foo {
public:
Foo();
Foo(const Foo &amp;);
...
};
</pre>
</div>
<p>
you can write Python code like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = Foo() # Create a Foo
&gt;&gt;&gt; g = Foo(f) # Copy f
</pre>
</div>
<p>
Overloading support is not quite as flexible as in C++. Sometimes there are methods that SWIG
can't disambiguate. For example:
</p>
<div class="code">
<pre>
void spam(int);
void spam(short);
</pre>
</div>
<p>
or
</p>
<div class="code">
<pre>
void foo(Bar *b);
void foo(Bar &amp;b);
</pre>
</div>
<p>
If declarations such as these appear, you will get a warning message like this:
</p>
<div class="shell">
<pre>
example.i:12: Warning 509: Overloaded method spam(short) effectively ignored,
example.i:11: Warning 509: as it is shadowed by spam(int).
</pre>
</div>
<p>
To fix this, you either need to ignore or rename one of the methods. For example:
</p>
<div class="code">
<pre>
%rename(spam_short) spam(short);
...
void spam(int);
void spam(short); // Accessed as spam_short
</pre>
</div>
<p>
or
</p>
<div class="code">
<pre>
%ignore spam(short);
...
void spam(int);
void spam(short); // Ignored
</pre>
</div>
<p>
SWIG resolves overloaded functions and methods using a disambiguation scheme that ranks and sorts
declarations according to a set of type-precedence rules. The order in which declarations appear
in the input does not matter except in situations where ambiguity arises--in this case, the
first declaration takes precedence.
</p>
<p>
Please refer to the "SWIG and C++" chapter for more information about overloading.
</p>
<H3><a name="Python_nn24">33.3.11 C++ operators</a></H3>
<p>
Certain C++ overloaded operators can be handled automatically by SWIG. For example,
consider a class like this:
</p>
<div class="code">
<pre>
class Complex {
private:
double rpart, ipart;
public:
Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
Complex(const Complex &amp;c) : rpart(c.rpart), ipart(c.ipart) { }
Complex &amp;operator=(const Complex &amp;c);
Complex operator+=(const Complex &amp;c) const;
Complex operator+(const Complex &amp;c) const;
Complex operator-(const Complex &amp;c) const;
Complex operator*(const Complex &amp;c) const;
Complex operator-() const;
double re() const { return rpart; }
double im() const { return ipart; }
};
</pre>
</div>
<p>
When wrapped, it works like you expect:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; c = Complex(3, 4)
&gt;&gt;&gt; d = Complex(7, 8)
&gt;&gt;&gt; e = c + d
&gt;&gt;&gt; e.re()
10.0
&gt;&gt;&gt; e.im()
12.0
&gt;&gt;&gt; c += d
&gt;&gt;&gt; c.re()
10.0
&gt;&gt;&gt; c.im()
12.0
</pre>
</div>
<p>
One restriction with operator overloading support is that SWIG is not
able to fully handle operators that aren't defined as part of the class.
For example, if you had code like this
</p>
<div class="code">
<pre>
class Complex {
...
friend Complex operator+(double, const Complex &amp;c);
...
};
</pre>
</div>
<p>
then SWIG ignores it and issues a warning. You can still wrap the operator,
but you may have to encapsulate it in a special function. For example:
</p>
<div class="code">
<pre>
%rename(Complex_add_dc) operator+(double, const Complex &amp;);
</pre>
</div>
<p>
There are ways to make this operator appear as part of the class using the <tt>%extend</tt> directive.
Keep reading.
</p>
<p>
Also, be aware that certain operators don't map cleanly to Python. For instance,
overloaded assignment operators don't map to Python semantics and will be ignored.
</p>
<p>
Operator overloading is implemented in the <tt>pyopers.swg</tt> library file.
In particular overloaded operators are marked with the <tt>python:maybecall</tt> feature, also known as <tt>%pythonmaybecall</tt>.
This feature forces SWIG to generate code that return an instance of Python's <tt>NotImplemented</tt>
instead of raising an exception when the comparison fails, that is, on any kind of error.
This follows the guidelines in <a href="https://www.python.org/dev/peps/pep-0207/">PEP 207 - Rich Comparisons</a> and <a href="https://docs.python.org/3/library/constants.html#NotImplemented">NotImplemented Python constant</a>.
</p>
<H3><a name="Python_nn25">33.3.12 C++ namespaces</a></H3>
<p>
SWIG is aware of C++ namespaces, but namespace names do not appear in
the module nor do namespaces result in a module that is broken up into
submodules or packages. For example, if you have a file like this,
</p>
<div class="code">
<pre>
%module example
namespace foo {
int fact(int n);
struct Vector {
double x, y, z;
};
};
</pre>
</div>
<p>
it works in Python as follows:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; import example
&gt;&gt;&gt; example.fact(3)
6
&gt;&gt;&gt; v = example.Vector()
&gt;&gt;&gt; v.x = 3.4
&gt;&gt;&gt; print v.y
0.0
&gt;&gt;&gt;
</pre>
</div>
<p>
If your program has more than one namespace, name conflicts (if any) can be resolved using <tt>%rename</tt>
For example:
</p>
<div class="code">
<pre>
%rename(Bar_spam) Bar::spam;
namespace Foo {
int spam();
}
namespace Bar {
int spam();
}
</pre>
</div>
<p>
If you have more than one namespace and your want to keep their
symbols separate, consider wrapping them as separate SWIG modules.
For example, make the module name the same as the namespace and create
extension modules for each namespace separately. If your program
utilizes thousands of small deeply nested namespaces each with
identical symbol names, well, then you get what you deserve.
</p>
<H3><a name="Python_nn26">33.3.13 C++ templates</a></H3>
<p>
C++ templates don't present a huge problem for SWIG. However, in order
to create wrappers, you have to tell SWIG to create wrappers for a particular
template instantiation. To do this, you use the <tt>%template</tt> directive.
For example:
</p>
<div class="code">
<pre>
%module example
%{
#include "pair.h"
%}
template&lt;class T1, class T2&gt;
struct pair {
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair();
pair(const T1&amp;, const T2&amp;);
~pair();
};
%template(pairii) pair&lt;int, int&gt;;
</pre>
</div>
<p>
In Python:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; import example
&gt;&gt;&gt; p = example.pairii(3, 4)
&gt;&gt;&gt; p.first
3
&gt;&gt;&gt; p.second
4
</pre>
</div>
<p>
Obviously, there is more to template wrapping than shown in this example.
More details can be found in the <a href="SWIGPlus.html#SWIGPlus">SWIG and C++</a> chapter.
Some more complicated
examples will appear later.
</p>
<H3><a name="Python_nn27">33.3.14 C++ Smart Pointers</a></H3>
<H4><a name="Python_smart_pointers_shared_ptr">33.3.14.1 The shared_ptr Smart Pointer</a></H4>
<p>
The C++11 standard provides <tt>std::shared_ptr</tt> which was derived from the Boost
implementation, <tt>boost::shared_ptr</tt>.
Both of these are available for Python in the SWIG library and usage is outlined
in the <a href="Library.html#Library_std_shared_ptr">shared_ptr smart pointer</a> library section.
</p>
<H4><a name="Python_smart_pointers_generic">33.3.14.2 Generic Smart Pointers</a></H4>
<p>
In certain C++ programs, it is common to use classes that have been wrapped by
so-called "smart pointers." Generally, this involves the use of a template class
that implements <tt>operator-&gt;()</tt> like this:
</p>
<div class="code">
<pre>
template&lt;class T&gt; class SmartPtr {
...
T *operator-&gt;();
...
}
</pre>
</div>
<p>
Then, if you have a class like this,
</p>
<div class="code">
<pre>
class Foo {
public:
int x;
int bar();
};
</pre>
</div>
<p>
A smart pointer would be used in C++ as follows:
</p>
<div class="code">
<pre>
SmartPtr&lt;Foo&gt; p = CreateFoo(); // Created somehow (not shown)
...
p-&gt;x = 3; // Foo::x
int y = p-&gt;bar(); // Foo::bar
</pre>
</div>
<p>
To wrap this in Python, simply tell SWIG about the <tt>SmartPtr</tt> class and the low-level
<tt>Foo</tt> object. Make sure you instantiate <tt>SmartPtr</tt> using <tt>%template</tt> if necessary.
For example:
</p>
<div class="code">
<pre>
%module example
...
%template(SmartPtrFoo) SmartPtr&lt;Foo&gt;;
...
</pre>
</div>
<p>
Now, in Python, everything should just "work":
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; p = example.CreateFoo() # Create a smart-pointer somehow
&gt;&gt;&gt; p.x = 3 # Foo::x
&gt;&gt;&gt; p.bar() # Foo::bar
</pre>
</div>
<p>
If you ever need to access the underlying pointer returned by <tt>operator-&gt;()</tt> itself,
simply use the <tt>__deref__()</tt> method. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = p.__deref__() # Returns underlying Foo *
</pre>
</div>
<H3><a name="Python_nn27a">33.3.15 C++ reference counted objects</a></H3>
<p>
The <a href="SWIGPlus.html#SWIGPlus_ref_unref">C++ reference counted objects</a> section contains
Python examples of memory management using referencing counting.
</p>
<H2><a name="Python_nn28">33.4 Further details on the Python class interface</a></H2>
<p>
In the previous section, a high-level view of Python wrapping was
presented. A key component of this wrapping is that structures and
classes are wrapped by Python proxy classes. This provides a very
natural Python interface and allows SWIG to support a number of
advanced features such as operator overloading. However, a number
of low-level details were omitted. This section provides a brief overview
of how the proxy classes work.
</p>
<p><b>New in SWIG version 2.0.4:</b>
The use of Python proxy classes has performance implications that may be
unacceptable for a high-performance library. The new <tt>-builtin</tt>
option instructs SWIG to forego the use of proxy classes, and instead
create wrapped types as new built-in Python types. When this option is used,
the following section ("Proxy classes") does not apply. Details on the use of
the <tt>-builtin</tt> option are in the <a href="#Python_builtin_types">Built-in Types</a>
section.
</p>
<H3><a name="Python_nn29">33.4.1 Proxy classes</a></H3>
<p>
In the <a href="SWIG.html#SWIG">"SWIG basics"</a> and <a href="SWIGPlus.html#SWIGPlus">"SWIG and C++"</a> chapters,
details of low-level structure and class wrapping are described. To summarize those chapters, if you
have a class like this
</p>
<div class="code">
<pre>
class Foo {
public:
int x;
int spam(int);
...
</pre>
</div>
<p>
then SWIG transforms it into a set of low-level procedural wrappers. For example:
</p>
<div class="code">
<pre>
Foo *new_Foo() {
return new Foo();
}
void delete_Foo(Foo *f) {
delete f;
}
int Foo_x_get(Foo *f) {
return f-&gt;x;
}
void Foo_x_set(Foo *f, int value) {
f-&gt;x = value;
}
int Foo_spam(Foo *f, int arg1) {
return f-&gt;spam(arg1);
}
</pre>
</div>
<p>
These wrappers can be found in the low-level extension module (e.g., <tt>_example</tt>).
</p>
<p>
Using these wrappers, SWIG generates a high-level Python proxy class (also known as a shadow class) like this (shown
for Python 2.2):
</p>
<div class="targetlang">
<pre>
import _example
class Foo(object):
def __init__(self):
self.this = _example.new_Foo()
self.thisown = 1
def __del__(self):
if self.thisown:
_example.delete_Foo(self.this)
def spam(self, arg1):
return _example.Foo_spam(self.this, arg1)
x = property(_example.Foo_x_get, _example.Foo_x_set)
</pre>
</div>
<p>
This class merely holds a pointer to the underlying C++ object (<tt>.this</tt>) and dispatches methods and
member variable access to that object using the low-level accessor functions. From a user's point of
view, it makes the class work normally:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = example.Foo()
&gt;&gt;&gt; f.x = 3
&gt;&gt;&gt; y = f.spam(5)
</pre>
</div>
<p>
The fact that the class has been wrapped by a real Python class offers certain advantages. For instance,
you can attach new Python methods to the class and you can even inherit from it (something not supported
by Python built-in types until Python 2.2).
</p>
<H3><a name="Python_builtin_types">33.4.2 Built-in Types</a></H3>
<p>
The <tt>-builtin</tt> option provides a significant performance improvement
in the wrapped code. To understand the difference between proxy classes
and built-in types, let's take a look at what a wrapped object looks like
under both circumstances.
</p>
<p>When proxy classes are used, each wrapped object in Python is an instance
of a pure Python class. As a reminder, here is what the <tt>__init__</tt> method looks
like in a proxy class:
</p>
<div class="targetlang">
<pre>
class Foo(object):
def __init__(self):
self.this = _example.new_Foo()
self.thisown = 1
</pre>
</div>
<p>When a <tt>Foo</tt> instance is created, the call to <tt>_example.new_Foo()</tt>
creates a new C++ <tt>Foo</tt> instance; wraps that C++ instance inside an instance of
a Python built-in type called <tt>SwigPyObject</tt>; and stores the <tt>SwigPyObject</tt>
instance in the 'this' field of the Python Foo object. Did you get all that? So, the
Python <tt>Foo</tt> object is composed of three parts:</p>
<ul>
<li> The Python <tt>Foo</tt> instance, which contains...</li>
<li> ... an instance of <tt>struct SwigPyObject</tt>, which contains...</li>
<li> ... a C++ <tt>Foo</tt> instance</li>
</ul>
<p>When <tt>-builtin</tt> is used, the pure Python layer is stripped off. Each
wrapped class is turned into a new Python built-in type which inherits from
<tt>SwigPyObject</tt>, and <tt>SwigPyObject</tt> instances are returned directly
from the wrapped methods. For more information about Python built-in extensions,
please refer to the Python documentation:</p>
<p><a href="https://docs.python.org/3/extending/newtypes.html">https://docs.python.org/3/extending/newtypes.html</a></p>
<H4><a name="Python_builtin_limitations">33.4.2.1 Limitations</a></H4>
<p>Use of the <tt>-builtin</tt> option implies a couple of limitations:
<ul>
<li><p>Some legacy syntax is no longer supported; in particular:</p>
<ul>
<li>The functional interface is no longer exposed. For example, you may no longer call <tt>Whizzo.new_CrunchyFrog()</tt>. Instead, you must use <tt>Whizzo.CrunchyFrog()</tt>.</li>
<li>Static member variables are no longer accessed through the 'cvar' field (e.g., <tt>Dances.cvar.FishSlap</tt>).
They are instead accessed in the idiomatic way (<tt>Dances.FishSlap</tt>).</li>
</ul>
</li>
<li><p>Wrapped types may not be raised as Python exceptions. Here's why: the Python internals expect that all sub-classes of Exception will have this struct layout:</p>
<div class="code">
<pre>
typedef struct {
PyObject_HEAD
PyObject *dict;
PyObject *args;
PyObject *message;
} PyBaseExceptionObject;
</pre>
</div>
<p>But swig-generated wrappers expect that all swig-wrapped classes will have this struct layout:</p>
<div class="code">
<pre>
typedef struct {
PyObject_HEAD
void *ptr;
swig_type_info *ty;
int own;
PyObject *next;
PyObject *dict;
} SwigPyObject;
</pre>
</div>
<p>There are workarounds for this. For example, if you wrap this class:
<div class="code">
<pre>
class MyException {
public:
MyException (const char *msg_);
~MyException ();
const char *what () const;
private:
char *msg;
};
</pre>
</div>
<p>... you can define this Python class, which may be raised as an exception:</p>
<div class="targetlang">
<pre>
class MyPyException(Exception):
def __init__(self, msg, *args):
Exception.__init__(self, *args)
self.myexc = MyException(msg)
def what(self):
return self.myexc.what()
</pre>
</div>
</li>
<li><p>Reverse binary operators (e.g., <tt>__radd__</tt>) are not supported.</p>
<p>To illustrate this point, if you have a wrapped class called <tt>MyString</tt>,
and you want to use instances of <tt>MyString</tt> interchangeably with native Python
strings, you can define an <tt>'operator+ (const char*)'</tt> method :</p>
<div class="code">
<pre>
class MyString {
public:
MyString (const char *init);
MyString operator+ (const char *other) const;
...
};
</pre>
</div>
<p>
SWIG will automatically create an operator overload in Python that will allow this:
</p>
<div class="targetlang">
<pre>
from MyModule import MyString
mystr = MyString("No one expects")
episode = mystr + " the Spanish Inquisition"
</pre>
</div>
<p>
This works because the first operand (<tt>mystr</tt>) defines a way
to add a native string to itself. However, the following will <b>not</b> work:
</p>
<div class="targetlang">
<pre>
from MyModule import MyString
mystr = MyString("Parrot")
episode = "Dead " + mystr
</pre>
</div>
<p>
The above code fails, because the first operand -- a native Python string --
doesn't know how to add an instance of <tt>MyString</tt> to itself.
</p>
</li>
<li><p>If you have multiple SWIG modules that share type information (<a href="Modules.html#Modules_nn2">more info</a>),
the <tt>-builtin</tt> option requires a bit of extra discipline to ensure that base classes are initialized before derived classes. Specifically:</p>
<ul>
<li><p>There must be an unambiguous dependency graph for the modules.</p></li>
<li><p>Module dependencies must be explicitly stated with <tt>%import</tt> statements in the SWIG interface file.</p>
</ul>
<p>As an example, suppose module <tt>A</tt> has this interface in <tt>A.i</tt> :</p>
<div class="code"><pre>
%module "A";
class Base {
...
};
</pre></div>
<p>If you want to wrap another module containing a class that inherits from <tt>A</tt>, this is how it would look :</p>
<div class="code"><pre>
%module "B";
%import "A.i"
class Derived : public Base {
...
};
</pre></div>
<p>The <tt>import "A.i"</tt> statement is required, because module <tt>B</tt> depends on module <tt>A</tt>.</p>
<p>As long as you obey these requirements, your Python code may import the modules in any order :</p>
<div class="targetlang"><pre>
import B
import A
assert(issubclass(B.Derived, A.Base))
</pre></div>
</li>
</ul>
<H4><a name="Python_builtin_overloads">33.4.2.2 Operator overloads and slots -- use them!</a></H4>
<p>The entire justification for the <tt>-builtin</tt> option is improved
performance. To that end, the best way to squeeze maximum performance out
of your wrappers is to <b>use operator overloads.</b>
Named method dispatch is slow in Python, even when compared to other scripting languages.
However, Python built-in types have a large number of "slots",
analogous to C++ operator overloads, which allow you to short-circuit named method dispatch
for certain common operations.
</p>
<p>By default, SWIG will translate most C++ arithmetic operator overloads into Python
slot entries. For example, suppose you have this class:
<div class="code">
<pre>
class Twit {
public:
Twit operator+ (const Twit&amp; twit) const;
// Forward to operator+
Twit add (const Twit&amp; twit) const {
return *this + twit;
}
};
</pre>
</div>
<p>SWIG will automatically register <tt>operator+</tt> as a Python slot operator for addition. You may write Python code like this:</p>
<div class="targetlang">
<pre>
from MyModule import Twit
nigel = Twit()
emily = Twit()
percival = nigel + emily
percival = nigel.add(emily)
</pre>
</div>
<p>The last two lines of the Python code are equivalent,
but <b>the line that uses the '+' operator is much faster</b>.
</p>
<p>In-place operators (e.g., <tt>operator+=</tt>) and comparison operators
(<tt>operator==, operator&lt;</tt>, etc.) are also converted to Python
slot operators. For a complete list of C++ operators that are
automatically converted to Python slot operators, refer to the file
<tt>python/pyopers.swig</tt> in the SWIG library.
</p>
<p>
Read about all of the available Python slots here:
<a href="https://docs.python.org/3/c-api/typeobj.html">https://docs.python.org/3/c-api/typeobj.html</a></p>
<p>
There are two ways to define a Python slot function: dispatch to a
statically defined function; or dispatch to a method defined on the
operand.
</p>
<p>
To dispatch to a statically defined function, use %feature("python:&lt;slot&gt;"),
where &lt;slot&gt; is the name of a field in a <tt>PyTypeObject, PyNumberMethods,
PyMappingMethods, PySequenceMethods</tt> or <tt>PyBufferProcs</tt>.
You may override (almost) all of these slots.
</p>
<p>
Let's consider an example setting the <tt>tp_hash</tt> slot for the <tt>MyClass</tt> type.
This is akin to providing a <tt>__hash__</tt> method (for non-builtin types) to make a type hashable.
The hashable type can then for example be added to a Python <tt>dict</tt>.
</p>
<div class="code">
<pre>
%feature("python:tp_hash") MyClass "myHashFunc";
class MyClass {
public:
long field1;
long field2;
...
};
%{
#if PY_VERSION_HEX &gt;= 0x03020000
static Py_hash_t myHashFunc(PyObject *pyobj)
#else
static long myHashFunc(PyObject *pyobj)
#endif
{
MyClass *cobj;
// Convert pyobj to cobj
return (cobj-&gt;field1 * (cobj-&gt;field2 &lt;&lt; 7));
}
%}
</pre>
</div>
<p>
If you examine the generated code, the supplied hash function will now be
the function callback in the tp_hash slot for the builtin type for <tt>MyClass</tt>:
</p>
<div class="code">
<pre>
static PyHeapTypeObject SwigPyBuiltin__MyClass_type = {
...
(hashfunc) myHashFunc, /* tp_hash */
...
</pre>
</div>
<p>
NOTE: It is the responsibility of the programmer (that's you!) to ensure
that a statically defined slot function has the correct signature, the <tt>hashfunc</tt>
typedef in this case.
</p>
<p>
If, instead, you want to dispatch to an instance method, you can
use %feature("python:slot"). For example:
</p>
<div class="code">
<pre>
%feature("python:slot", "tp_hash", functype="hashfunc") MyClass::myHashFunc;
#if PY_VERSION_HEX &lt; 0x03020000
#define Py_hash_t long
#endif
class MyClass {
public:
Py_hash_t myHashFunc() const;
...
};
</pre>
</div>
<p>
NOTE: Some Python slots use a method signature which does not
match the signature of SWIG-wrapped methods. For those slots,
SWIG will automatically generate a "closure" function to re-marshal
the arguments before dispatching to the wrapped method. Setting
the "functype" attribute of the feature enables SWIG to generate
the chosen closure function.
</p>
<p>
There is further information on <tt>%feature("python:slot")</tt>
in the file <tt>python/pyopers.swig</tt> in the SWIG library.
</p>
<H3><a name="Python_nn30">33.4.3 Memory management</a></H3>
<p>NOTE: Although this section refers to proxy objects, everything here also applies
when the <tt>-builtin</tt> option is used.</p>
<p>
Associated with proxy object, is an ownership flag <tt>.thisown</tt> The value of this
flag determines who is responsible for deleting the underlying C++ object. If set to 1,
the Python interpreter will destroy the C++ object when the proxy class is
garbage collected. If set to 0 (or if the attribute is missing), then the destruction
of the proxy class has no effect on the C++ object.
</p>
<p>
When an object is created by a constructor or returned by value, Python automatically takes
ownership of the result. For example:
</p>
<div class="code">
<pre>
class Foo {
public:
Foo();
Foo bar();
};
</pre>
</div>
<p>
In Python:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = Foo()
&gt;&gt;&gt; f.thisown
1
&gt;&gt;&gt; g = f.bar()
&gt;&gt;&gt; g.thisown
1
</pre>
</div>
<p>
On the other hand, when pointers are returned to Python, there is often no way to know where
they came from. Therefore, the ownership is set to zero. For example:
</p>
<div class="code">
<pre>
class Foo {
public:
...
Foo *spam();
...
};
</pre>
</div>
<br>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = Foo()
&gt;&gt;&gt; s = f.spam()
&gt;&gt;&gt; print s.thisown
0
&gt;&gt;&gt;
</pre>
</div>
<p>
This behavior is especially important for classes that act as
containers. For example, if a method returns a pointer to an object
that is contained inside another object, you definitely don't want
Python to assume ownership and destroy it!
</p>
<p>
A good way to indicate that ownership should be set for a returned pointer
is to use the <a href="Library.html#Library_nn11">%newobject directive</a>.
</p>
<p>
Related to containers, ownership issues can arise whenever an object is assigned to a member
or global variable. For example, consider this interface:
</p>
<div class="code">
<pre>
%module example
struct Foo {
int value;
Foo *next;
};
Foo *head = 0;
</pre>
</div>
<p>
When wrapped in Python, careful observation will reveal that ownership changes whenever an object
is assigned to a global variable. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = example.Foo()
&gt;&gt;&gt; f.thisown
1
&gt;&gt;&gt; example.cvar.head = f
&gt;&gt;&gt; f.thisown
0
&gt;&gt;&gt;
</pre>
</div>
<p>
In this case, C is now holding a reference to the object---you probably don't want Python to destroy it.
Similarly, this occurs for members. For example:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; f = example.Foo()
&gt;&gt;&gt; g = example.Foo()
&gt;&gt;&gt; f.thisown
1
&gt;&gt;&gt; g.thisown
1
&gt;&gt;&gt; f.next = g
&gt;&gt;&gt; g.thisown
0
&gt;&gt;&gt;
</pre>
</div>
<p>
For the most part, memory management issues remain hidden. However,
there are occasionally situations where you might have to manually
change the ownership of an object. For instance, consider code like this:
</p>
<div class="code">
<pre>
class Node {
Object *value;
public:
void set_value(Object *v) { value = v; }
...
};
</pre>
</div>
<p>
Now, consider the following Python code:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; v = Object() # Create an object
&gt;&gt;&gt; n = Node() # Create a node
&gt;&gt;&gt; n.set_value(v) # Set value
&gt;&gt;&gt; v.thisown
1
&gt;&gt;&gt; del v
</pre>
</div>
<p>
In this case, the object <tt>n</tt> is holding a reference to
<tt>v</tt> internally. However, SWIG has no way to know that this
has occurred. Therefore, Python still thinks that it has ownership of the
object. Should the proxy object be destroyed, then the C++ destructor
will be invoked and <tt>n</tt> will be holding a stale-pointer. If
you're lucky, you will only get a segmentation fault.
</p>
<p>
To work around this, it is always possible to flip the ownership flag. For example,
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; v.thisown = 0
</pre>
</div>
<p>
It is also possible to deal with situations like this using
typemaps--an advanced topic discussed later.
</p>
<H2><a name="Python_directors">33.5 Cross language polymorphism</a></H2>
<p>
Proxy classes provide a more natural, object-oriented way to access
extension classes. As described above, each proxy instance has an
associated C++ instance, and method calls to the proxy are passed to the
C++ instance transparently via C wrapper functions.
</p>
<p>
This arrangement is asymmetric in the sense that no corresponding
mechanism exists to pass method calls down the inheritance chain from
C++ to Python. In particular, if a C++ class has been extended in Python
(by extending the proxy class), these extensions will not be visible
from C++ code. Virtual method calls from C++ are thus not able access
the lowest implementation in the inheritance chain.
</p>
<p>
Changes have been made to SWIG 1.3.18 to address this problem and
make the relationship between C++ classes and proxy classes more
symmetric. To achieve this goal, new classes called directors are
introduced at the bottom of the C++ inheritance chain. The job of the
directors is to route method calls correctly, either to C++
implementations higher in the inheritance chain or to Python
implementations lower in the inheritance chain. The upshot is that C++
classes can be extended in Python and from C++ these extensions look
exactly like native C++ classes. Neither C++ code nor Python code needs
to know where a particular method is implemented: the combination of
proxy classes, director classes, and C wrapper functions takes care of
all the cross-language method routing transparently.
</p>
<H3><a name="Python_nn33">33.5.1 Enabling directors</a></H3>
<p>
The director feature is disabled by default. To use directors you
must make two changes to the interface file. First, add the "directors"
option to the %module directive, like this:
</p>
<div class="code">
<pre>
%module(directors="1") modulename
</pre>
</div>
<p>
Without this option no director code will be generated. Second, you
must use the %feature("director") directive to tell SWIG which classes
and methods should get directors. The %feature directive can be applied
globally, to specific classes, and to specific methods, like this:
</p>
<div class="code">
<pre>
// generate directors for all classes that have virtual methods
%feature("director");
// generate directors for the virtual methods in class Foo
%feature("director") Foo;
</pre>
</div>
<p>
You can use the %feature("nodirector") directive to turn off
directors for specific classes or methods. So for example,
</p>
<div class="code">
<pre>
%feature("director") Foo;
%feature("nodirector") Foo::bar;
</pre>
</div>
<p>
will generate directors for the virtual methods of class Foo except
bar().
</p>
<p>
Directors can also be generated implicitly through inheritance.
In the following, class Bar will get a director class that handles
the methods one() and two() (but not three()):
</p>
<div class="code">
<pre>
%feature("director") Foo;
class Foo {
public:
Foo(int foo);
virtual ~Foo();
virtual void one();
virtual void two();
};
class Bar: public Foo {
public:
virtual void three();
};
</pre>
</div>
<p>
then at the Python side you can define
</p>
<div class="targetlang">
<pre>
import mymodule
class MyFoo(mymodule.Foo):
def __init__(self, foo):
mymodule.Foo.__init__(self, foo)
# super().__init__(foo) # Alternative construction for Python3
def one(self):
print "one from Python"
</pre>
</div>
<H3><a name="Python_nn34">33.5.2 Director classes</a></H3>
<p>
For each class that has directors enabled, SWIG generates a new class
that derives from both the class in question and a special
<tt>Swig::Director</tt> class. These new classes, referred to as director
classes, can be loosely thought of as the C++ equivalent of the Python
proxy classes. The director classes store a pointer to their underlying
Python object and handle various issues related to object ownership.
Indeed, this is quite similar to the "this" and "thisown" members of the
Python proxy classes.
</p>
<p>
For simplicity let's ignore the <tt>Swig::Director</tt> class and refer to the
original C++ class as the director's base class. By default, a director
class extends all virtual methods in the inheritance chain of its base
class (see the preceding section for how to modify this behavior).
Virtual methods that have a final specifier are unsurprisingly excluded.
Thus the virtual method calls, whether they originate in C++ or in
Python via proxy classes, eventually end up in at the implementation in
the director class. The job of the director methods is to route these
method calls to the appropriate place in the inheritance chain. By
"appropriate place" we mean the method that would have been called if
the C++ base class and its extensions in Python were seamlessly
integrated. That seamless integration is exactly what the director
classes provide, transparently skipping over all the messy extension API
glue that binds the two languages together.
</p>
<p>
In reality, the "appropriate place" is one of only two possibilities:
C++ or Python. Once this decision is made, the rest is fairly easy. If
the correct implementation is in C++, then the lowest implementation of
the method in the C++ inheritance chain is called explicitly. If the
correct implementation is in Python, the Python API is used to call the
method of the underlying Python object (after which the usual virtual
method resolution in Python automatically finds the right
implementation).
</p>
<p>
Now how does the director decide which language should handle the method call?
The basic rule is to handle the method in Python, unless there's a good
reason not to. The reason for this is simple: Python has the most
"extended" implementation of the method. This assertion is guaranteed,
since at a minimum the Python proxy class implements the method. If the
method in question has been extended by a class derived from the proxy
class, that extended implementation will execute exactly as it should.
If not, the proxy class will route the method call into a C wrapper
function, expecting that the method will be resolved in C++. The wrapper
will call the virtual method of the C++ instance, and since the director
extends this the call will end up right back in the director method. Now
comes the "good reason not to" part. If the director method were to blindly
call the Python method again, it would get stuck in an infinite loop. We avoid this
situation by adding special code to the C wrapper function that tells
the director method to not do this. The C wrapper function compares the
pointer to the Python object that called the wrapper function to the
pointer stored by the director. If these are the same, then the C
wrapper function tells the director to resolve the method by calling up
the C++ inheritance chain, preventing an infinite loop.
</p>
<p>
One more point needs to be made about the relationship between director
classes and proxy classes. When a proxy class instance is created in
Python, SWIG creates an instance of the original C++ class and assigns
it to <tt>.this</tt>. This is exactly what happens without directors and
is true even if directors are enabled for the particular class in
question. When a class <i>derived</i> from a proxy class is created,
however, SWIG then creates an instance of the corresponding C++ director
class. The reason for this difference is that user-defined subclasses
may override or extend methods of the original class, so the director
class is needed to route calls to these methods correctly. For
unmodified proxy classes, all methods are ultimately implemented in C++
so there is no need for the extra overhead involved with routing the
calls through Python.
</p>
<H3><a name="Python_nn35">33.5.3 Ownership and object destruction</a></H3>
<p>
Memory management issues are slightly more complicated with directors
than for proxy classes alone. Python instances hold a pointer to the
associated C++ director object, and the director in turn holds a pointer
back to the Python object. By default, proxy classes own their C++
director object and take care of deleting it when they are garbage
collected.
</p>
<p>
This relationship can be reversed by calling the special
<tt>__disown__()</tt> method of the proxy class. After calling this
method, the <tt>.thisown</tt> flag is set to zero, and the director
class increments the reference count of the Python object. When the
director class is deleted it decrements the reference count. Assuming no
outstanding references to the Python object remain, the Python object
will be destroyed at the same time. This is a good thing, since
directors and proxies refer to each other and so must be created and
destroyed together. Destroying one without destroying the other will
likely cause your program to segfault.
</p>
<p>
To help ensure that no references to the Python object remain after
calling <tt>__disown__()</tt>, this method returns a weak reference to
the Python object. Weak references are only available in Python versions
2.1 and higher, so for older versions you must explicitly delete all
references. Here is an example:
</p>
<div class="code">
<pre>
class Foo {
public:
...
};
class FooContainer {
public:
void addFoo(Foo *);
...
};
</pre>
</div>
<br>
<div class="targetlang">
<pre>
&gt;&gt;&gt; c = FooContainer()
&gt;&gt;&gt; a = Foo().__disown__()
&gt;&gt;&gt; c.addFoo(a)
&gt;&gt;&gt; b = Foo()
&gt;&gt;&gt; b = b.__disown__()
&gt;&gt;&gt; c.addFoo(b)
&gt;&gt;&gt; c.addFoo(Foo().__disown__())
</pre>
</div>
<p>
In this example, we are assuming that FooContainer will take care of
deleting all the Foo pointers it contains at some point. Note that no hard
references to the Foo objects remain in Python.
</p>
<H3><a name="Python_nn36">33.5.4 Exception unrolling</a></H3>
<p>
With directors routing method calls to Python, and proxies routing them
to C++, the handling of exceptions is an important concern. By default, the
directors ignore exceptions that occur during method calls that are
resolved in Python. To handle such exceptions correctly, it is necessary
to temporarily translate them into C++ exceptions. This can be done with
the %feature("director:except") directive. The following code should
suffice in most cases:
</p>
<div class="code">
<pre>
%feature("director:except") {
if ($error != NULL) {
throw Swig::DirectorMethodException();
}
}
</pre>
</div>
<p>
This code will check the Python error state after each method call from
a director into Python, and throw a C++ exception if an error occurred.
This exception can be caught in C++ to implement an error handler.
Currently no information about the Python error is stored in the
Swig::DirectorMethodException object, but this will likely change in
the future.
</p>
<p>
It may be the case that a method call originates in Python, travels up
to C++ through a proxy class, and then back into Python via a director
method. If an exception occurs in Python at this point, it would be nice
for that exception to find its way back to the original caller. This can
be done by combining a normal %exception directive with the
<tt>director:except</tt> handler shown above. Here is an example of a
suitable exception handler:
</p>
<div class="code">
<pre>
%exception {
try { $action }
catch (Swig::DirectorException &amp;e) { SWIG_fail; }
}
</pre>
</div>
<p>
The class Swig::DirectorException used in this example is actually a
base class of Swig::DirectorMethodException, so it will trap this
exception. Because the Python error state is still set when
Swig::DirectorMethodException is thrown, Python will register the
exception as soon as the C wrapper function returns.
</p>
<H3><a name="Python_nn37">33.5.5 Overhead and code bloat</a></H3>
<p>
Enabling directors for a class will generate a new director method for
every virtual method in the class' inheritance chain. This alone can
generate a lot of code bloat for large hierarchies. Method arguments
that require complex conversions to and from target language types can
result in large director methods. For this reason it is recommended that
you selectively enable directors only for specific classes that are
likely to be extended in Python and used in C++.
</p>
<p>
Compared to classes that do not use directors, the call routing in the
director methods does add some overhead. In particular, at least one
dynamic cast and one extra function call occurs per method call from
Python. Relative to the speed of Python execution this is probably
completely negligible. For worst case routing, a method call that
ultimately resolves in C++ may take one extra detour through Python in
order to ensure that the method does not have an extended Python
implementation. This could result in a noticeable overhead in some cases.
</p>
<p>
Although directors make it natural to mix native C++ objects with Python
objects (as director objects) via a common base class pointer, one
should be aware of the obvious fact that method calls to Python objects
will be much slower than calls to C++ objects. This situation can be
optimized by selectively enabling director methods (using the %feature
directive) for only those methods that are likely to be extended in
Python.
</p>
<H3><a name="Python_nn38">33.5.6 Typemaps</a></H3>
<p>
Typemaps for input and output of most of the basic types from director
classes have been written. These are roughly the reverse of the usual
input and output typemaps used by the wrapper code. The typemap
operation names are 'directorin', 'directorout', and 'directorargout'.
The director code does not currently use any of the other kinds of typemaps.
It is not clear at this point which kinds are appropriate and
need to be supported.
</p>
<H3><a name="Python_nn39">33.5.7 Miscellaneous</a></H3>
<p>
Director typemaps for STL classes are in place, and hence you should
be able to use std::vector, std::string, etc., as you would any other type.
</p>
<p>
<b>Note:</b> The director typemaps for return types based in const
references, such as
<div class="code">
<pre>
class Foo {
&hellip;
virtual const int&amp; bar();
&hellip;
};
</pre>
</div>
<p>
will work only for simple call scenarios. Usually the resulting code
is neither thread or reentrant safe. Hence, the user is advised to
avoid returning const references in director methods. For example,
the user could modify the method interface to use lvalue return
types, wherever possible, for example
</p>
<div class="code">
<pre>
class Foo {
&hellip;
virtual int bar();
&hellip;
};
</pre>
</div>
<p>
If that is not possible, the user should avoid enabling the
director feature for reentrant, recursive or threaded member
methods that return const references.
</p>
<H2><a name="Python_nn40">33.6 Common customization features</a></H2>
<p>
The last section presented the absolute basics of C/C++ wrapping. If
you do nothing but feed SWIG a header file, you will get an interface
that mimics the behavior described. However, sometimes this isn't
enough to produce a nice module. Certain types of functionality might
be missing or the interface to certain functions might be awkward.
This section describes some common SWIG features that are used to
improve your the interface to an extension module.
</p>
<H3><a name="Python_nn41">33.6.1 C/C++ helper functions</a></H3>
<p>
Sometimes when you create a module, it is missing certain bits of functionality. For
example, if you had a function like this
</p>
<div class="code">
<pre>
void set_transform(Image *im, double m[4][4]);
</pre>
</div>
<p>
it would be accessible from Python, but there may be no easy way to call it.
For example, you might get errors like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; a = [
... [1, 0, 0, 0],
... [0, 1, 0, 0],
... [0, 0, 1, 0],
... [0, 0, 0, 1]]
&gt;&gt;&gt; set_transform(im, a)
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in ?
TypeError: Type error. Expected _p_a_4__double
</pre>
</div>
<p>
The problem here is that there is no easy way to construct and manipulate a suitable
<tt>double [4][4]</tt> value to use. To fix this, you can write some extra C helper
functions. Just use the <tt>%inline</tt> directive. For example:
</p>
<div class="code">
<pre>
%inline %{
/* Note: double[4][4] is equivalent to a pointer to an array double (*)[4] */
double (*new_mat44())[4] {
return (double (*)[4]) malloc(16*sizeof(double));
}
void free_mat44(double (*x)[4]) {
free(x);
}
void mat44_set(double x[4][4], int i, int j, double v) {
x[i][j] = v;
}
double mat44_get(double x[4][4], int i, int j) {
return x[i][j];
}
%}
</pre>
</div>
<p>
From Python, you could then write code like this:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; a = new_mat44()
&gt;&gt;&gt; mat44_set(a, 0, 0, 1.0)
&gt;&gt;&gt; mat44_set(a, 1, 1, 1.0)
&gt;&gt;&gt; mat44_set(a, 2, 2, 1.0)
...
&gt;&gt;&gt; set_transform(im, a)
&gt;&gt;&gt;
</pre>
</div>
<p>
Admittedly, this is not the most elegant looking approach. However, it works and it wasn't too
hard to implement. It is possible to clean this up using Python code, typemaps, and other
customization features as covered in later sections.
</p>
<H3><a name="Python_nn42">33.6.2 Adding additional Python code</a></H3>
<p>
If writing support code in C isn't enough, it is also possible to write code in
Python. This code gets inserted in to the <tt>.py</tt> file created by SWIG. One
use of Python code might be to supply a high-level interface to certain functions.
For example:
</p>
<div class="code">
<pre>
void set_transform(Image *im, double x[4][4]);
...
/* Rewrite the high level interface to set_transform */
%pythoncode %{
def set_transform(im, x):
a = new_mat44()
for i in range(4):
for j in range(4):
mat44_set(a, i, j, x[i][j])
_example.set_transform(im, a)
free_mat44(a)
%}
</pre>
</div>
<p>
In this example, <tt>set_transform()</tt> provides a high-level Python interface built on top of
low-level helper functions. For example, this code now seems to work:
</p>
<div class="targetlang">
<pre>
&gt;&gt;&gt; a = [
... [1, 0, 0, 0],
... [0, 1, 0, 0],
... [0, 0, 1, 0],
... [0, 0, 0, 1]]
&gt;&gt;&gt; set_transform(im, a)
&gt;&gt;&gt;
</pre>
</div>
<p>
Admittedly, this whole scheme for wrapping the two-dimension array
argument is rather ad-hoc. Besides, shouldn't a Python list or a
Numeric Python array just work normally? We'll get to those examples
soon enough. For now, think of this example as an illustration of
what can be done without having to rely on any of the more advanced
customization features.
</p>
<p>
There is also <tt>%pythonbegin