lineno and filename in exceptions

Udo Gleich Udo.Gleich at gmx.de
Sun Oct 3 09:38:42 EDT 1999


Hi,

I would like to catch syntax errors and other errors in script
files and open an editor at the right line number.

If I catch a syntax error the exeption has the attributes lineno and
filename. This information is not included e.g. in attribute errors
name errors.

Example:

--- script.py ------------------------------------------------------

print a

--- another_script.py ----------------------------------------------

try:
    execfile("script.py")
except Exception, argument:
    print argument.__class__.__name__
    print argument.__dict__
    try:
	print argument.lineno
	print argument.filename
    except:
	pass

--------------------------------------------------------------------

If I execute another_script.py I get

NameError
{'args': ('a',)}
Line number:

If I execute script.py I get

Traceback (innermost last):
  File "script.py", line 1, in ?
    print a
NameError: a

If I put a syntax error in script.py  (pr int a) I get something like

SyntaxError
{'filename': 'script.py',
 'lineno': 1,
 'args': ('invalid syntax', ('script.py', 1, 6, 'pr int a\012')),
 'offset': 6,
 'text': 'pr int a\012',
 'msg': 'invalid syntax'}
Line number: 1
File name: script.py

Question: Is there a way to get line number and file name in errors
other than SyntaxError?

Thank you,

Udo




More information about the Python-list mailing list