From 2824b0cbb66e715490e1ef13250bd675d87b32d9 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 2 Jun 2010 20:53:17 +0000 Subject: rel-2.0.0 git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- trunk/Examples/python/exception/Makefile | 21 ++++++++++++ trunk/Examples/python/exception/example.h | 53 +++++++++++++++++++++++++++++++ trunk/Examples/python/exception/example.i | 12 +++++++ trunk/Examples/python/exception/runme.py | 36 +++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 trunk/Examples/python/exception/Makefile create mode 100644 trunk/Examples/python/exception/example.h create mode 100644 trunk/Examples/python/exception/example.i create mode 100644 trunk/Examples/python/exception/runme.py (limited to 'trunk/Examples/python/exception') diff --git a/trunk/Examples/python/exception/Makefile b/trunk/Examples/python/exception/Makefile new file mode 100644 index 000000000..7dbdde944 --- /dev/null +++ b/trunk/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/trunk/Examples/python/exception/example.h b/trunk/Examples/python/exception/example.h new file mode 100644 index 000000000..8f9a977b0 --- /dev/null +++ b/trunk/Examples/python/exception/example.h @@ -0,0 +1,53 @@ +/* File : example.h */ + +#include +#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/trunk/Examples/python/exception/example.i b/trunk/Examples/python/exception/example.i new file mode 100644 index 000000000..08672c3a8 --- /dev/null +++ b/trunk/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/trunk/Examples/python/exception/runme.py b/trunk/Examples/python/exception/runme.py new file mode 100644 index 000000000..718707861 --- /dev/null +++ b/trunk/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 + + -- cgit v1.2.1