diff --git a/Examples/python/weave/.cvsignore b/Examples/python/weave/.cvsignore new file mode 100644 index 000000000..ebe81fc87 --- /dev/null +++ b/Examples/python/weave/.cvsignore @@ -0,0 +1 @@ +example.py example_wrap.cxx example_wrap.h diff --git a/Examples/python/weave/Makefile b/Examples/python/weave/Makefile new file mode 100644 index 000000000..822779bd1 --- /dev/null +++ b/Examples/python/weave/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +check: all diff --git a/Examples/python/weave/example.h b/Examples/python/weave/example.h new file mode 100644 index 000000000..d09d60850 --- /dev/null +++ b/Examples/python/weave/example.h @@ -0,0 +1,18 @@ +#ifndef _EXAMPLE_H +#define _EXAMPLE_H + +class Foo { + public: + int x; +}; + +class Bar { + public: + int y; +}; + +class FooBar : public Foo, public Bar { + public: + int z; +}; +#endif diff --git a/Examples/python/weave/example.i b/Examples/python/weave/example.i new file mode 100644 index 000000000..0364ee2ef --- /dev/null +++ b/Examples/python/weave/example.i @@ -0,0 +1,13 @@ +%module(directors="1") example +%feature("director"); + +%{ +#include "example.h" +%} + +%include "std_vector.i" + +%include "example.h" + +%template(VectorBar) std::vector; +%template(VectorFoo) std::vector; diff --git a/Examples/python/weave/runme.py b/Examples/python/weave/runme.py new file mode 100644 index 000000000..d040a2560 --- /dev/null +++ b/Examples/python/weave/runme.py @@ -0,0 +1,72 @@ +""" +Test weave support for SWIG wrapped objects. + +This example requires that one has weave installed. Weave is +distributed as part of SciPy (http://www.scipy.org). More information +on Weave may be had from here: + + http://www.scipy.org/documentation/weave + +As of November 22, 2004, this only works with weave from CVS. If +there is a more recent release of SciPy after this data, it should +work fine. + +""" + +import example +import weave +from weave import converters +from weave import swig2_spec + +# Weave does not support swig2 by default (yet). So add this to the +# list of default converters to test. +converters.default.insert(0, swig2_spec.swig2_converter()) + +def test(): + """ A simple test case for weave.""" + a = example.Foo() + a.x = 1 + b = example.Bar() + b.y = 2 + c = example.FooBar() + c.x = 1 + c.y = 2 + c.z = 3 + v = example.VectorBar() + v.append(b) + v.append(c) + d = v[0] + e = v[1] + v = example.VectorFoo() + v.append(a) + v.append(c) + f = v[0] + g = v[1] + + code = """ + std::cout << a->x << std::endl; + assert(a->x == 1); + std::cout << b->y << std::endl; + assert(b->y == 2); + std::cout << c->x << std::endl; + std::cout << c->y << std::endl; + std::cout << c->z << std::endl; + assert(c->x == 1); + assert(c->y == 2); + assert(c->z == 3); + std::cout << d->y << std::endl; + assert(d->y == 2); + std::cout << e->y << std::endl; + assert(e->y == 2); + std::cout << f->x << std::endl; + assert(f->x == 1); + std::cout << g->x << std::endl; + assert(g->x == 1); + """ + weave.inline(code, ['a', 'b', 'c', 'd', 'e', 'f', 'g'], + include_dirs=['.'], + headers=['"example.h"'], + verbose=2) + +if __name__ == "__main__": + test()