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: a.b[c] will call: a.__getattr__('b') but an exception is thrown before: a.b.__getitem__(c) Is there a way of intervening as `exec cmd in globals, locals` attempts to translate 'c' into an object? I thought that trapping a NameError might work. But now, I'm not so sure. Many thanks, \~/ > Here is what I would like to do: > > #------------------------------------------------------------ > a = Tr3() # implements domain specific language > a.b = 1 # this works, Tr3 overrides __getattr__ > a.__dict__['b'] = 2 # just so you know that b is local > a[b] = 3 # I want to resolve locally, but get: > > Traceback (most recent call last): ... > exec cmd in globals, locals ... > NameError: global name 'b' is not defined > #------------------------------------------------------------ > > So far, I've tried capturing exceptions in __getattr__, __setitem__, and > __setattr__, with no luck. I do NOT want to put `a[b]=3` inside a > try...except block because I want to enable a command line `>>>a[b]=3` > > Is there a way to hook a NameError exception outside of the call stack? > Perhaps, inside Tr3.__init__ ? Is there a way of putting back unhandled > NameError exceptions, so that they unwind the stack normally? > > This is intended for production code. -- http://mail.python.org/mailman/listinfo/python-list