Problems executing code with exec

JB jb at yahoo.de
Sun Oct 13 11:41:03 CEST 2002


I have a piece of code that I should like to execute with 
the exec statement. Here is this code:

def lotfusspunkt(p,g):
  (p1,p2) = schnitt(kreis(g.a(),p),kreis(g.b(),p))
  return schnitt(gerade(p1,p2),g)

and here is the procedure that executes it (with the text 
above being in the variable text):

  def start(self):

    i = 0
    statement_list = string.split(txt,'\n')
    for statement in statement_list:
      try:
        exec statement+'\n' in user_name_space
      except:
        txt = str(sys.exc_info()[1])
        return i
      i += 1

The idea is that upon return the line number of the line 
that caused the error is returned and I can set a cursor to 
this line in the text editor window that contains the 
statements.

On the other hand, when I call exec with

exec text+'\n' in user_name_space

that works but then I cannot find out the number of the line 
that caused the error. If I use something like

    i = traceback.tb_lineno(sys.exc_info()[2])

then the line number of the line that contains the exec 
statement is returned and not the line number of the line 
in the variable text. Sometime it is possible to parse the 
error message (in sys.exc_info()[2]) to get the correct 
line number but sometimes this is not possible.

Can anybody help me?

TIA,
-- 
Janos Blazi


-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
   http://www.newsfeed.com       The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----



More information about the Python-list mailing list