lineno and filename in exceptions
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 5 05:16:51 EDT 1999
Udo Gleich <Udo.Gleich at gmx.de> wrote:
> 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.
> Question: Is there a way to get line number and file name in errors
> other than SyntaxError?
you can dissect traceback, frame, and
code objects to get the required info.
here's an example:
import sys, traceback
try:
execfile("script.py")
except Exception, argument:
et, ev, tb = sys.exc_info()
while tb:
co = tb.tb_frame.f_code
print co.co_filename,
print traceback.tb_lineno(tb)
tb = tb.tb_next
print et, ev
</F>
More information about the Python-list
mailing list