Re: try except inside exec

2009-05-31 Thread alex23
Michele Petrazzo wrote: > I want to execute a python code inside a string and so I use the exec > statement. The strange thing is that the try/except couple don't catch > the exception and so it return to the main code. > Is there a solution to convert or make this code work? > > My code: > > STR

Re: try except inside exec

2009-05-31 Thread Michele Petrazzo
Aaron Brady wrote: Exceptions are only caught when raised in a 'try' statement. If you don't raise the exception in a try statement, it won't be caught. The function you posted will raise an exception. If you are making the call yourself, that is, if only the definition is foreign, then sim

Re: try except inside exec

2009-05-31 Thread Michele Petrazzo
David Stanek wrote: Is the thirdparty function the entire STR or just the a_funct part? Just the a_funct. Only for try and for see if it's possible to catch all the (possible) exception(s), I add the "try/except" clause that include the a_funct external code. Michele -- http://mail.python.org

Re: try except inside exec

2009-05-31 Thread Michele Petrazzo
Emile van Sebille wrote: Write the string out to a .py file and import it? I don't think that it's possible. I want to create an env that are accessible only from that peace of code and not from the code that "execute" the third-party one. Emile Michele -- http://mail.python.org/mailman/l

Re: try except inside exec

2009-05-29 Thread Carl Banks
On May 29, 7:21 am, Michele Petrazzo wrote: > Hi all, > I want to execute a python code inside a string and so I use the exec > statement. The strange thing is that the try/except couple don't catch > the exception and so it return to the main code. > Is there a solution to convert or make this co

Re: try except inside exec

2009-05-29 Thread Carl Banks
On May 29, 7:21 am, Michele Petrazzo wrote: > Hi all, > I want to execute a python code inside a string and so I use the exec > statement. The strange thing is that the try/except couple don't catch > the exception and so it return to the main code. > Is there a solution to convert or make this co

Re: try except inside exec

2009-05-29 Thread Aaron Brady
On May 29, 9:55 am, Michele Petrazzo wrote: > Aaron Brady wrote: > > STR = """ > > class Globals: > >    err = 0 > > def a_funct(): > >    try: > >       1/0 > >    except ZeroDivisionError: > >       import traceback > >       Globals.err = traceback.format_exc() > > """ > > exec STR in env > > t

Re: try except inside exec

2009-05-29 Thread David Stanek
On Fri, May 29, 2009 at 11:55 AM, Michele Petrazzo wrote: > > My goal is to execute a function received from a third-part, so I cannot > modify as you made in your first piece of code. > I want a "clean" exception with the real line code/tb so I can show a "real" > error message. This means that t

Re: try except inside exec

2009-05-29 Thread Emile van Sebille
On 5/29/2009 8:55 AM Michele Petrazzo said... Aaron Brady wrote: Then the formatted exception will be available in 'err'. Do you want the exception-handling to be part of the function you are entering into 'env'? My goal is to execute a function received from a third-part, so I cannot modi

Re: try except inside exec

2009-05-29 Thread Michele Petrazzo
Aaron Brady wrote: STR = """ class Globals: err = 0 def a_funct(): try: 1/0 except ZeroDivisionError: import traceback Globals.err = traceback.format_exc() """ exec STR in env try: env["a_funct"]() except ZeroDivisionError: import traceback err = traceback.for

Re: try except inside exec

2009-05-29 Thread Aaron Brady
On May 29, 8:21 am, Michele Petrazzo wrote: > Hi all, > I want to execute a python code inside a string and so I use the exec > statement. The strange thing is that the try/except couple don't catch > the exception and so it return to the main code. > Is there a solution to convert or make this co