From b32024f2ab2673de890bc29de2f8884adf6d4a01 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Mon, 12 Jan 2004 23:40:58 +0000 Subject: [PATCH] fix precatching exception behaviour to postcatching git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5617 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/exception_order.i | 51 ++++++++++++++----- .../python/exception_order_runme.py | 16 +++++- SWIG/Source/Modules/emit.cxx | 47 +++++++++-------- 3 files changed, 77 insertions(+), 37 deletions(-) diff --git a/SWIG/Examples/test-suite/exception_order.i b/SWIG/Examples/test-suite/exception_order.i index fc7cbdb14..71cef2303 100644 --- a/SWIG/Examples/test-suite/exception_order.i +++ b/SWIG/Examples/test-suite/exception_order.i @@ -2,30 +2,53 @@ %include "exception.i" -#if 1 + +/* + last resource, catch everything but don't override + user's throw declarations. +*/ %exception { - try { - $action - } catch(...) { - SWIG_exception(SWIG_RuntimeError,"Unknown exception"); - } + try { + $action + } catch(...) { + SWIG_exception(SWIG_RuntimeError,"postcatch unknown"); + } } -#endif %inline %{ - + struct E1 + { + }; - struct E + struct E2 + { + }; + + struct E3 { }; struct A { - int foo() throw(E) - { - throw E(); - return 0; - } + /* catched by the user's throw definition */ + int foo() throw(E1) + { + throw E1(); + return 0; + } + + int bar() throw(E2) + { + throw E2(); + return 0; + } + + /* catched by the %postexception */ + int foobar() + { + throw E3(); + return 0; + } }; %} diff --git a/SWIG/Examples/test-suite/python/exception_order_runme.py b/SWIG/Examples/test-suite/python/exception_order_runme.py index a9dc96b35..7967c2619 100644 --- a/SWIG/Examples/test-suite/python/exception_order_runme.py +++ b/SWIG/Examples/test-suite/python/exception_order_runme.py @@ -6,9 +6,21 @@ a = exception_order.A() try: a.foo() except RuntimeError,e: - if e.args[0] != "E": + if e.args[0] != "E1": print "bad exception order", raise RuntimeError, e.args +try: + a.bar() +except RuntimeError,e: + if e.args[0] != "E2": + print "bad exception order", + raise RuntimeError, e.args + +try: + a.foobar() +except RuntimeError,e: + if e.args[0] != "postcatch unknown": + print "bad exception order", + raise RuntimeError, e.args - diff --git a/SWIG/Source/Modules/emit.cxx b/SWIG/Source/Modules/emit.cxx index c8868d9ea..e8e3abdc8 100644 --- a/SWIG/Source/Modules/emit.cxx +++ b/SWIG/Source/Modules/emit.cxx @@ -393,11 +393,31 @@ void emit_action(Node *n, Wrapper *f) { } /* Exception handling code */ + /* saves action -> eaction for postcatching exception */ + String *eaction = NewString(""); + /* If we are in C++ mode and there is a throw specifier. We're going to enclose the block in a try block */ + if (throws) { + Printf(eaction,"try {\n"); + } + + Printv(eaction, action, "\n",NIL); if (throws) { - Printf(f->code,"try {\n"); + Printf(eaction,"}\n"); + for (Parm *ep = throws; ep; ep = nextSibling(ep)) { + String *em = Swig_typemap_lookup_new("throws",ep,"_e",0); + if (em) { + Printf(eaction,"catch(%s) {\n", SwigType_str(Getattr(ep,"type"),"&_e")); + Printv(eaction,em,"\n",NIL); + Printf(eaction,"}\n"); + } else { + Swig_warning(WARN_TYPEMAP_THROW, Getfile(n), Getline(n), + "No 'throw' typemap defined for exception type '%s'\n", SwigType_str(Getattr(ep,"type"),0)); + } + } + Printf(eaction,"catch(...) { throw; }\n"); } /* Look for except typemap (Deprecated) */ @@ -407,33 +427,18 @@ void emit_action(Node *n, Wrapper *f) { if (!tm) { tm = Getattr(n,"feature:except"); if (tm) tm = Copy(tm); - } + } if ((tm) && Len(tm) && (Strcmp(tm,"1") != 0)) { Replaceall(tm,"$name",Getattr(n,"name")); Replaceall(tm,"$symname", Getattr(n,"sym:name")); - Replaceall(tm,"$function", action); - Replaceall(tm,"$action", action); + Replaceall(tm,"$function", eaction); + Replaceall(tm,"$action", eaction); Printv(f->code,tm,"\n", NIL); Delete(tm); } else { - Printv(f->code, action, "\n",NIL); - } - - if (throws) { - Printf(f->code,"}\n"); - for (Parm *ep = throws; ep; ep = nextSibling(ep)) { - String *em = Swig_typemap_lookup_new("throws",ep,"_e",0); - if (em) { - Printf(f->code,"catch(%s) {\n", SwigType_str(Getattr(ep,"type"),"&_e")); - Printv(f->code,em,"\n",NIL); - Printf(f->code,"}\n"); - } else { - Swig_warning(WARN_TYPEMAP_THROW, Getfile(n), Getline(n), - "No 'throw' typemap defined for exception type '%s'\n", SwigType_str(Getattr(ep,"type"),0)); - } - } - Printf(f->code,"catch(...) { throw; }\n"); + Printv(f->code,eaction,"\n",NIL); } + Delete(eaction); /* Emit contract code (if any) */ if (Swig_contract_mode_get()) {