471,318 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Hooking exceptions outside of call stack

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.

Many thanks!

Warren

Jun 9 '07 #1
1 1215
Warren Stringer wrote:
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
#------------------------------------------------------------
Note that your a[b]=3 is the same as '__ = b;a[__]=3' You get that
exception because b is not a bound name in the namespace you are
currently using. In order to get what you want, you will either need to
use a['b'] = 3, a.b = 3, or a method that I refuse to describe to you.
This is intended for production code.
The reason I refuse to describe to you the method that could 'solve'
your particular problem is because it would be very difficult to
differentiate between what you *want* to happen, and actual errors,
which would make production code *very* difficult to get right.

As an alternative to a['b'], you could use a[Z.b], for an object Z:

class Z:
def __getattr__(self, a):
return a

Z = Z()

Which will have much less potential for destroying the maintainability
and testability of your production code than hooking any trace function.
- Josiah
Jun 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by David Turner | last post: by
59 posts views Thread by kk_oop | last post: by
15 posts views Thread by Bernard | last post: by
7 posts views Thread by Andrew Ducker | last post: by
2 posts views Thread by Carl Tribble | last post: by
14 posts views Thread by dcassar | last post: by
2 posts views Thread by Bob | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.