Hooking exceptions outside of call stack

Jean-Paul Calderone exarkun at divmod.com
Sat Jun 9 23:31:57 CEST 2007

On Sat, 9 Jun 2007 13:52:19 -0700, Warren Stringer <warren at muse.com> wrote:
>Am still trying to hook a NameError exception and continue to run. After a
>few more hours of searching the web and pouring over Martelli's book, the
>closest I've come is:
>>>> import sys
>>>> def new_exit(arg=0):
>...     print 'new_exit called'
>...     #old_exit(arg)
>>>> def hook(type, value, tb):
>...     print 'hook called with', type
>...     return
>>>> sys.excepthook = hook
>>>> old_exit = sys.exit
>>>> sys.exit = new_exit
>>>> a[b]=1 # would like to get new_exit called
>hook called with exceptions.NameError
>>>> def test():
>...     sys.exit()
>>>> test()
>new_exit called
>The interactive session is different from running in the IDE (am using
>ActiveState's Visual Python) which exits just after the `a[b]=1` without
>calling hook.
>Am I missing something? Perhaps this is the wrong approach? I want Python to
>check a specific set of locals first, before checking globals. For instance:

Yes.  Python doesn't have restartable exceptions.  Perhaps you would like to
take a look at CL or Smalltalk?


More information about the Python-list mailing list