[BangPypers] suppressing the exception stack trace

steve steve at lonetwin.net
Wed Sep 16 09:01:34 CEST 2009


On 09/16/2009 11:39 AM, Vishal wrote:
>
>
> On Tue, Sep 15, 2009 at 9:07 PM, steve <steve at lonetwin.net
> <mailto:steve at lonetwin.net>> wrote:
>
>     On 09/15/2009 08:56 PM, Vishal wrote:
>
>         Hello,
>
>         I would like to raise an exception of type Exception(), however the
>         regular exception stack trace needs to be supressed.
>
>         This is needed in a function that takes raw_input() from the
>         user and
>         based on 'Y' or 'N', the function suspends further execution and
>         returns
>         to the python prompt or Hicontinues. An exit() brings it out of
>         the python
>         process....where as what is needed is coming back to the python
>         prompt.
>
>         I want a custom message to appear instead of a regular exception
>         stack
>         trace message.
>
>         How to do this? any pointers...?
>
>     Python 2.6 (r26:66714, Jun  8 2009, 16:07:29)
>     [GCC 4.4.0 20090506 (Red Hat 4.4.0-4)] on linux2
>     Type "help", "copyright", "credits" or "license" for more information.
>     > >> import traceback
>     > >> try:
>     ...     1 + "foo"
>     ... except Exception, e:
>     ...     traceback.format_stack(limit=1)
>     ...
>     ['  File "<stdin>", line 4, in <module>\n']
>     > >> help(traceback)
>     ....
>
>     HTH,
>     - steve
>
>
>
> Forgot to mention that we are currently stuck with python2.5.2
>
> As part of a debug/test hook, I would like to suspend further action of
> a program, after a certain point in the procedure. What I had come up
> with is a function named testHook() which does this:
>
> def testHook():
> """
> """
>      choice = raw_input("Would you like to continue (Y or N): ")
>      try:
>          if(str(choice).lower() != 'y'):
>              print("You chose not to go ahead. Ok!!\nAborting Now...")
>              raise Exception("User initiated Abort...")
>      except Exception:
>          raise # re-raise it..so we come out of execution loop
>
> In this case, a stack trace gets printed out, before the string 'user
> initiated abort' comes up. I want to remove that stack trace...so it
> looks more clean to a user.
>
> What can be done in this case?

Well, I don't understand how and from what context you are calling testHook(). 
My first instinct is to ask, why you can't simply return instead of 'raise' on 
an abort ? like so ...

def testHook(exc):
     choice = raw_input("Would you like to continue (Y or N): ")
     if(str(choice).lower() != 'y'):
         print("You chose not to go ahead. Ok!!\nAborting Now...")
         print "User initiated Abort..."
	return -1 # ...or any other value that makes sense to your app.
     else:
         raise exc # re-raise the passed exception

In which case, I am assuming testHook() called like this:
try:
     1 + "foo"
except Exception, e:
     return testHook(e)

> or any other way of creating such a hook?
Yes. The way I would do this sort of this is by using decorators:

 >>> def testHook(func):
...     def wrap_exception(*args):
...             try:
...                     func(*args)
...             except Exception, e:
...                     choice = raw_input("Would you like to continue (Y or N): ")
...                     if (str(choice).lower() != 'y'):
...                             print("You chose not to go ahead. Ok!!\nAborting 
Now...")
...                             print "User initiated abort ..."
...                             print "on recieving %s" % e.message
...                             return
...                     else:
...                             print "redirect to debugger in this block ..."
...     return wrap_exception
...
 >>> @testHook
... def main(a, b):
...     return a + b
...
 >>> main(1, "foo")
Would you like to continue (Y or N): n
You chose not to go ahead. Ok!!
Aborting Now...
User initiated abort ...
on recieving unsupported operand type(s) for +: 'int' and 'str'
 >>>


Here is a good article for introducing decorators:
http://www.ibm.com/developerworks/linux/library/l-cpdecor.html

HTH,
cheers,
- steve
-- 
random non tech spiel: http://lonetwin.blogspot.com/
tech randomness: http://lonehacks.blogspot.com/
what i'm stumbling into: http://lonetwin.stumbleupon.com/


More information about the BangPypers mailing list