-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPython.html
7446 lines (6147 loc) · 207 KB
/
Python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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>
<li><a href="#Python_stable_abi">Stable ABI</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 a 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>
<li><a href="#Python_doxygen_docstrings">Doxygen comments</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_annotations">Python function annotations and variable annotations</a>
<ul>
<li><a href="#Python_annotations_c">C/C++ annotation types</a>
</ul>
<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>
This chapter describes SWIG's support of Python. SWIG is compatible
with all recent Python versions (Python 2.7 and Python >= 3.3). SWIG 4.0.x
supported Python 3.2. SWIG 3.0.x supported older Python 2.x and 3.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 < 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
>>> import example
>>> example.fact(4)
24
>>>
</pre></div>
<p>
A common error received by first-time users is the following:
</p>
<div class="targetlang">
<pre>
>>> import example
Traceback (most recent call last):
File "<stdin>", 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>
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function (init_example)
>>>
</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>
>>> import example
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: libfoo.so: cannot open shared object file: No such file or directory
>>>
</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 => /usr/lib/libstdc++-libc6.1-1.so.2 (0x40019000)
libm.so.6 => /lib/libm.so.6 (0x4005b000)
libc.so.6 => /lib/libc.so.6 (0x40077000)
/lib/ld-linux.so.2 => /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
>>> import example
>>> print example.fact(4)
24
>>>
</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>-flatstaticmethod </td><td>Generate additional flattened Python methods for C++ static methods</td></tr>
<tr><td>-globals <name> </td><td>Set <name> used to access C global variable (default: 'cvar')</td></tr>
<tr><td>-interface <mod></td><td>Set low-level C/C++ module name to <mod> (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>-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>