[FRPythoneers] compile() and syntax errors

Mike Romberg romberg at fsl.noaa.gov
Fri Aug 9 11:17:37 MDT 2002

  Since there are so many bright folks here on this list, I'd thought
I'd try asking my question here first.  I'm importing modules by using
pythons builting compile() command to get code objects.  These code
objects can then be loaded into modules.  This is all integrated in
with the imputil module.  But this problem is not really directly
related to imputil.  Say I have some code like this:

str = """
def foo():
    print 'hello'

codeobj = compile(str, "fakemod", 'exec')

  I've put in a deliberate syntax error in the code found in the
string.  So... I get a traceback like this:

Traceback (most recent call last):
  File "test.py", line 7, in ?
    codeobj = compile(str, "internal", 'exec')
  File "<string>", line 4
SyntaxError: invalid syntax

  What I'd really, really like is to not have the "<string>" show up
as the module name.  It seems to me that it should use the filename
argument I provided (internal).  If I could change <string> to
something more informative, it would help identify exactly where
errors occur (since I have many, many strings).  Perhaps there is some
way to catch the error, hack the traceback and then rethrow the
exception?  Anyone have an idea?


Mike Romberg (romberg at fsl.noaa.gov)

More information about the FRPythoneers mailing list