472,965 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 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 1269
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.