File tree Expand file tree Collapse file tree 1 file changed +74
-0
lines changed
week-08/extensions/source Expand file tree Collapse file tree 1 file changed +74
-0
lines changed Original file line number Diff line number Diff line change 753753
754754XDress
755755
756+ [also Boost-Python -- nat really a wrapper generator]
757+
756758SWIG
757759=====
758760
@@ -782,4 +784,76 @@ Further reading
782784
783785http://www.swig.org/Doc1.3/Python.html
784786
787+ SWIGifying add()
788+ ================
789+
790+ SWIG doesn't require modification to your C source code
791+
792+ The language interface is defined by an "interface file", usually with a suffix of .i
793+
794+ From there, SWIG can generate interfaces for the languages it supports
795+
796+ The interface file contains ANSI C prototypes and variable declarations
797+
798+ The %module directive defines the name of the module that will be created by SWIG
799+
800+ Creating a wrapper:
801+ ===================
802+
803+ Create ``add.i ``::
804+
805+ %module add
806+ %{
807+ %}
808+ extern int add(int x, int y);
809+
810+ Create distutils ``setup.py ``::
811+
812+ from distutils.core import setup, Extension
813+
814+ setup(
815+ name='add',
816+ py_modules=['add'],
817+ ext_modules=[
818+ Extension('_add', sources=['add.c', 'add.i'])
819+ ]
820+ )
821+
822+ .. nextslide ::
823+
824+ And built it::
825+
826+ python setup.py build_ext --inplace
827+
828+ then run the code::
829+
830+ python -c 'import add; print add.add(4,5)'
831+
832+ http://www.swig.org/Doc2.0/SWIGDocumentation.html#Introduction_nn5
833+
834+ ***********************
835+ Decisions, Decisions...
836+ ***********************
837+
838+ So what to use???
839+
840+ My decision tree:
841+
842+ Are you calling a few system library calls?
843+ * Use ctypes
844+
845+ Do you have a really big library to wrap?
846+ * use a wrapper generator:
847+
848+ - SWIG (use this is you need to support other languages)
849+ - SIP
850+ - XDress
851+
852+ Are you writing extensions from scratch?
853+ * Cython
854+ * Do you love C++
855+ - Boost-Python
856+
857+ Do you want a "thick" wrapper around a C/C+= lib:
858+ * Cython
785859
You can’t perform that action at this time.
0 commit comments