Python equivalent of #line?
Jeff Epler
jepler at unpythonic.net
Mon May 26 14:41:07 EDT 2003
If you are printing your own tracebacks, you could add your own machinery
to interpret '#line' directives in the .py file. This would be possible
because '#line' will obviously be treated as a comment by Python itself.
See traceback.py for how this is currently done. It seems that in each
place (filename,lineno) is used, you'll need to put a call to your new
function:
# You write this function
def find_origin_line(filename, lineno):
"""return the originating filename and line-number for this
possibly-preprocessed Python file"""
...
# and change these functions
def print_tb(tb, limit=None, file=None):
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
If 'limit' is omitted or None, all entries are printed. If 'file'
is omitted or None, the output goes to sys.stderr; otherwise
'file' should be an open file or file-like object with a write()
method.
"""
if not file:
file = sys.stderr
if limit is None:
if hasattr(sys, 'tracebacklimit'):
limit = sys.tracebacklimit
n = 0
while tb is not None and (limit is None or n < limit):
f = tb.tb_frame
lineno = tb_lineno(tb)
co = f.f_code
filename = co.co_filename
filename, lineno = find_origin_line(filename, lineno) # XXX and similarly elsewhere
name = co.co_name
_print(file,
' File "%s", line %d, in %s' % (filename,lineno,name))
line = linecache.getline(filename, lineno)
if line: _print(file, ' ' + line.strip())
tb = tb.tb_next
n = n+1
Jeff
More information about the Python-list
mailing list