Python exception not being caught by Swig

Robert Arkiletian <[email protected]> Wed, 28 Jul 2021 21:28:01 -0700
Newsgroups gmane.comp.programming.swig
Message-ID <CAN_+rpKcGeYPTqv7-NypYkEFcNT=jC1-dOn-z1__NJMSqeH11Q@mail.gmail.com>
FLTK is a C++ gui library which we are wrapping in Python.
Here is the python code
==================
import fltk

def foo(widget):
   raise Exception("this is an exception test")

win = fltk.Fl_Window(0, 0, 200, 200, "test")
but = fltk.Fl_Button(0,0,170,30,"exception")
but.callback(foo)
win.end()
win.show()

try:
   fltk.Fl.run() #this is the event loop
except Exception as e:
   print(e)
======================

in the swig interface file fltk.i, along with other code we have
======================
%module(docstring=DOCSTRING, directors="1", package="fltk._fltk") fltk
%feature("director");
%feature("director:except") {
   if ($error != NULL) {
       throw Swig::DirectorMethodException();
   }
}

%exception {
   try { $action }
   catch (Swig::DirectorException &e) { SWIG_fail; }
}
======================

When the button is clicked the output is:
======================
Traceback (most recent call last):
 File "bug2c.py", line 5, in foo
   raise Exception("this is an exception test")
Exception: this is an exception test
======================

But the program does NOT terminate. Any hints or help is much appreciated.