diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
---|---|---|
committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/python/exception | |
download | swig-tarball-master.tar.gz |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/python/exception')
-rw-r--r-- | Examples/python/exception/Makefile | 21 | ||||
-rw-r--r-- | Examples/python/exception/example.h | 53 | ||||
-rw-r--r-- | Examples/python/exception/example.i | 12 | ||||
-rw-r--r-- | Examples/python/exception/runme.py | 36 |
4 files changed, 122 insertions, 0 deletions
diff --git a/Examples/python/exception/Makefile b/Examples/python/exception/Makefile new file mode 100644 index 0000000..7dbdde9 --- /dev/null +++ b/Examples/python/exception/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +check: all + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/Examples/python/exception/example.h b/Examples/python/exception/example.h new file mode 100644 index 0000000..8f9a977 --- /dev/null +++ b/Examples/python/exception/example.h @@ -0,0 +1,53 @@ +/* File : example.h */ + +#include <string> +#ifndef SWIG +struct A { +}; +#endif + +class Exc { +public: + Exc(int c, const char *m) { + code = c; + strncpy(msg,m,256); + } + int code; + char msg[256]; +}; + +#if defined(_MSC_VER) + #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +#endif + +class Test { +public: + int simple() throw(int) { + throw(37); + return 1; + } + int message() throw(const char *) { + throw("I died."); + return 1; + } + int hosed() throw(Exc) { + throw(Exc(42,"Hosed")); + return 1; + } + int unknown() throw(A*) { + static A a; + throw &a; + return 1; + } + int multi(int x) throw(int, const char *, Exc) { + if (x == 1) throw(37); + if (x == 2) throw("Bleah!"); + if (x == 3) throw(Exc(42,"No-go-diggy-die")); + return 1; + } +}; + +#if defined(_MSC_VER) + #pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +#endif + diff --git a/Examples/python/exception/example.i b/Examples/python/exception/example.i new file mode 100644 index 0000000..08672c3 --- /dev/null +++ b/Examples/python/exception/example.i @@ -0,0 +1,12 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +%include "std_string.i" + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Examples/python/exception/runme.py b/Examples/python/exception/runme.py new file mode 100644 index 0000000..7187078 --- /dev/null +++ b/Examples/python/exception/runme.py @@ -0,0 +1,36 @@ +# file: runme.py + +# Throw a lot of exceptions + +import example + +t = example.Test() +try: + t.unknown() +except RuntimeError,e: + print "incomplete type", e.args[0] + +try: + t.simple() +except RuntimeError,e: + print e.args[0] + +try: + t.message() +except RuntimeError,e: + print e.args[0] + +try: + t.hosed() +except example.Exc,e: + print e.code, e.msg + +for i in range(1,4): + try: + t.multi(i) + except RuntimeError,e: + print e.args[0] + except example.Exc,e: + print e.code, e.msg + + |