how to enable error message in exec??

Jeremy Hylton jeremy at digicool.com
Mon Apr 9 18:05:42 EDT 2001


>>>>> "ET" == ed tsang <ed_tsang at yahoo.com> writes:

  ET> Hi My current script, say script A, need to execute some python
  ET> code in other file, say script B, by using exec() function. But
  ET> if there is any syntax error in script B, the exec() returns,
  ET> but I would not know what kind of syntax error is that.

I'm afraid I don't understand what problem you are having.

If you use the exec statement or the builtin compile function to
compile Python source code and the source code contains a SyntaxError,
the exec/compile will raise a SyntaxError.

>>> exec "() += 1"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1
SyntaxError: can't assign to ()

>>> compile("[x for x in x] = x", "<string>", "exec")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1
SyntaxError: can't assign to list comprehension

>>> try:
...	exec "() += 1"
... except SyntaxError, err:
...	pass
... else:
...	err = None
... 
>>> err
<exceptions.SyntaxError instance at 0x80d82cc>

Is your application generates a SyntaxError?  If it is, what do you
want to happen?

Jeremy







More information about the Python-list mailing list