summaryrefslogtreecommitdiff
path: root/scipy/weave/examples/swig2_example.py
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2005-09-26 20:20:16 +0000
committerTravis Oliphant <oliphant@enthought.com>2005-09-26 20:20:16 +0000
commit45d01a4be1c4221132ba46d687e6af3a8df3329b (patch)
treece3be5290e918def7c7187e747c5460193b0ca85 /scipy/weave/examples/swig2_example.py
parentccd1c3db37672627aa4fe0fdb5437f5dddc0fe86 (diff)
downloadnumpy-45d01a4be1c4221132ba46d687e6af3a8df3329b.tar.gz
Moved weave
Diffstat (limited to 'scipy/weave/examples/swig2_example.py')
-rw-r--r--scipy/weave/examples/swig2_example.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/scipy/weave/examples/swig2_example.py b/scipy/weave/examples/swig2_example.py
new file mode 100644
index 000000000..96bdb4f8f
--- /dev/null
+++ b/scipy/weave/examples/swig2_example.py
@@ -0,0 +1,53 @@
+"""Simple example to show how to use weave.inline on SWIG2 wrapped
+objects. SWIG2 refers to SWIG versions >= 1.3.
+
+To run this example you must build the trivial SWIG2 extension called
+swig2_ext. To do this you need to do something like this::
+
+ $ swig -c++ -python -I. -o swig2_ext_wrap.cxx swig2_ext.i
+
+ $ g++ -Wall -O2 -I/usr/include/python2.3 -fPIC -I. -c \
+ -o swig2_ext_wrap.os swig2_ext_wrap.cxx
+
+ $ g++ -shared -o _swig2_ext.so swig2_ext_wrap.os \
+ -L/usr/lib/python2.3/config
+
+The files swig2_ext.i and swig2_ext.h are included in the same
+directory that contains this file.
+
+Note that weave's SWIG2 support works fine whether SWIG_COBJECT_TYPES
+are used or not.
+
+Author: Prabhu Ramachandran
+Copyright (c) 2004, Prabhu Ramachandran
+License: BSD Style.
+
+"""
+
+# Import our SWIG2 wrapped library
+import swig2_ext
+
+import weave
+from weave import swig2_spec, converters
+
+# SWIG2 support is not enabled by default. We do this by adding the
+# swig2 converter to the default list of converters.
+converters.default.insert(0, swig2_spec.swig2_converter())
+
+def test():
+ """Instantiate the SWIG wrapped object and then call its method
+ from C++ using weave.inline
+
+ """
+ a = swig2_ext.A()
+ b = swig2_ext.foo() # This will be an APtr instance.
+ b.thisown = 1 # Prevent memory leaks.
+ code = """a->f();
+ b->f();
+ """
+ weave.inline(code, ['a', 'b'], include_dirs=['.'],
+ headers=['"swig2_ext.h"'], verbose=1)
+
+
+if __name__ == "__main__":
+ test()