fix precatching exception behaviour to postcatching
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5617 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
d6a9dfa874
commit
b32024f2ab
3 changed files with 77 additions and 37 deletions
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
%}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue