add more docs and cases about how to use the %execption directive
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7550 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
f482f4346a
commit
6777a49b1f
3 changed files with 34 additions and 4 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
TOP = ../..
|
TOP = ../..
|
||||||
SWIG = $(TOP)/../swig
|
SWIG = $(TOP)/../preinst-swig
|
||||||
CXXSRCS =
|
CXXSRCS =
|
||||||
TARGET = example
|
TARGET = example
|
||||||
INTERFACE = example.i
|
INTERFACE = example.i
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ template<typename T> class Queue {
|
||||||
last = (last + 1) % maxsize;
|
last = (last + 1) % maxsize;
|
||||||
nitems++;
|
nitems++;
|
||||||
}
|
}
|
||||||
T dequeue() throw(EmptyError) {
|
T dequeue() {
|
||||||
T x;
|
T x;
|
||||||
if (nitems == 0) throw EmptyError();
|
if (nitems == 0) throw EmptyError();
|
||||||
x = items[(last + maxsize - nitems) % maxsize];
|
x = items[(last + maxsize - nitems) % maxsize];
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,27 @@
|
||||||
the handler for any class with this method defined.
|
the handler for any class with this method defined.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
First we need to 'disable' the default swig throw mechanism for the
|
||||||
|
FullError class. We do this by rethrowing the exception.
|
||||||
|
|
||||||
|
Note that this is necessary since the class appears in a throw
|
||||||
|
declaration:
|
||||||
|
|
||||||
|
|
||||||
|
void enqueue(T x) throw(FullError);
|
||||||
|
|
||||||
|
hence, swig recognizes it as an exception class and it will generate
|
||||||
|
the necessary code to catch it and rethrow it to the python side.
|
||||||
|
|
||||||
|
*/
|
||||||
|
%typemap(throws) FullError "throw;";
|
||||||
|
|
||||||
|
|
||||||
%exception *::enqueue {
|
%exception *::enqueue {
|
||||||
try {
|
try {
|
||||||
$action
|
$action
|
||||||
} catch(FullError e) {
|
} catch(FullError& e) {
|
||||||
FullError *ecopy = new FullError(e);
|
FullError *ecopy = new FullError(e);
|
||||||
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_FullError, 1);
|
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_FullError, 1);
|
||||||
PyErr_SetObject((PyObject *) SWIGTYPE_p_FullError->clientdata, err);
|
PyErr_SetObject((PyObject *) SWIGTYPE_p_FullError->clientdata, err);
|
||||||
|
|
@ -58,10 +75,23 @@
|
||||||
PyErr_SetObject()! A neat trick perhaps.
|
PyErr_SetObject()! A neat trick perhaps.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Now, the EmpytError doesn't appear in a throw declaration, and hence
|
||||||
|
we need to 'mark' it as an exception class. In python, classes that
|
||||||
|
are used as exception are 'special', and need to be wrapped as
|
||||||
|
'classic' ones.
|
||||||
|
|
||||||
|
This is a python issue, and if you don't mark the class, you will
|
||||||
|
see 'interesting' behaviours at the python side.
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
%exceptionclass EmptyError;
|
||||||
|
|
||||||
%exception *::dequeue {
|
%exception *::dequeue {
|
||||||
try {
|
try {
|
||||||
$action
|
$action
|
||||||
} catch(EmptyError e) {
|
} catch(EmptyError& e) {
|
||||||
EmptyError *ecopy = new EmptyError(e);
|
EmptyError *ecopy = new EmptyError(e);
|
||||||
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_EmptyError, 1);
|
PyObject *err = SWIG_NewPointerObj(ecopy, SWIGTYPE_p_EmptyError, 1);
|
||||||
PyErr_SetObject((PyObject *) SWIGTYPE_p_EmptyError->clientdata, err);
|
PyErr_SetObject((PyObject *) SWIGTYPE_p_EmptyError->clientdata, err);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue