473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 1278
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
59
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been...
15
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions...
7
by: Andrew Ducker | last post by:
My unhandled exceptions seem to just vanish. If I put the line: int x = int.Parse("XXX"); in (which generates an exception, obviously) then the code that's executing just vanishes, leaving me...
2
by: Carl Tribble | last post by:
Is there some reason the exceptions in a class with a shared constructor will not bubble up the call stack? I have a class with a shared constructor which calls a shared method Requery() which...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
2
by: Bob | last post by:
I MUST be able to trap unhandled exceptions, bring the thread to a routine that then closes the thread on which the execption occurred without closing or affecting the other threads. Think of an...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.