[Tutor] capturing exceptions from an exec statement

Garry Willgoose Garry.Willgoose at newcastle.edu.au
Tue Apr 8 11:05:47 CEST 2008


Andreas, Alan,

Oops ... I'm embarassed to admit the problem seems to have gone away.  
I don't know maybe in my testing I fed it some valid python. But  
anyway I made sure to feed it something that was invalid it now works  
fine. Its been a long day .... Sorry to bother you.

Alan thanks for the reference to module cmd ... I didn't know about  
it and I'll have a look at it to see if its a more robust soln to my  
problem.


> Python 2.4.5 (#2, Mar 12 2008, 00:15:51)
> [GCC 4.2.3 (Debian 4.2.3-2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> try:
> ...     exec("raise ValueError")
> ... except:
> ...     print "raised some exception"
> ...     import sys
> ...     print sys.exc_info()
> ...
> raised some exception
> (<class exceptions.ValueError at 0xb7d83b3c>,  
> <exceptions.ValueError instance at 0xb7d6738c>, <traceback object  
> at 0xb7d65aa4>)
>>>>
>
> Something like this?
>
> Andreas
>
> Am Dienstag, den 08.04.2008, 17:12 +1000 schrieb Garry Willgoose:
>> I'm writing a (very simple) command line window for use within my
>> Tkinter GUI menu driven program (Aside: I want to provide a Matlab
>> like command line capability within an otherwise menu driven
>> program ... but where the script language is Python). The guts of
>> executing the command are (where commandtext is a text variable
>> captured from my command line window)
>>
>>      try:
>>        exec(commandtext)
>>      except:
>>        print 'failed to execute command'      # we'll do something
>> more sophisticated in due course
>>
>> The question is if commandtext contains invalid python I thought exec
>> raised an exception that I could capture. I seem unable to get this
>> to throw an exception no matter what is in commandtext. I assume I'm
>> missing something simple here.
>>
>> Alternatively should I open a process with popen ... I want to be
>> able to modify variables in the existing scope and the way I read
>> popen I can't do that.
>>
>>
>> ====================================================================
>> Prof Garry Willgoose,
>> Australian Professorial Fellow in Environmental Engineering,
>> Director, Centre for Climate Impact Management (C2IM),
>> School of Engineering, The University of Newcastle,
>> Callaghan, 2308
>> Australia.
>>
>> Centre webpage: www.c2im.org.au
>>
>> Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574
>> (Fri PM-Mon)
>> FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal
>> and Telluric)
>> Env. Engg. Secretary: (International) +61 2 4921 6042
>>
>> email:  garry.willgoose at newcastle.edu.au;
>> g.willgoose at telluricresearch.com
>> email-for-life: garry.willgoose at alum.mit.edu
>> personal webpage: www.telluricresearch.com/garry
>> ====================================================================
>> "Do not go where the path may lead, go instead where there is no path
>> and leave a trail"
>>                            Ralph Waldo Emerson
>> ====================================================================
>>
>>
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor

====================================================================
Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574  
(Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal  
and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose at newcastle.edu.au;  
g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
personal webpage: www.telluricresearch.com/garry
====================================================================
"Do not go where the path may lead, go instead where there is no path  
and leave a trail"
                           Ralph Waldo Emerson
====================================================================







More information about the Tutor mailing list