[Tutor] function signatures for callbacks

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Mar 24 19:59:17 CET 2006



On Fri, 24 Mar 2006, Don Taylor wrote:

> When I first read your response I did not see how it helped me, but now
> I realise that it allows me to add some context to the exception message.
>
> I don't suppose that I can change the traceback to point at the
> definition of f2 instead of shout() but I can give a better hint to the
> user at why this went wrong.

Hi Don,

Actually, adding something like this would also be possible.

###########################################################
## Pseudocode for sending 'x' to every listener (untested)
class CallbackError(Exception):
    pass

for l in self.listeners:
    try:
        l(x)
    except Exception, e:
        raise CallbackError, ("%s failed" % l.__name__, e)
###########################################################

That is, we can wrap an exception handler around calls to the callback
function, and if something bad happens, return enough of the context to
make the error message reasonable.  We can then combine this technique
with the way that shout() captured exceptions, in order to hide our
module's internals.



More information about the Tutor mailing list