Linenumbers: inspect.getinnerframes vs. traceback.format_exception

Thomas Guettler guettli at thomas-guettler.de
Mon Oct 11 07:29:30 EDT 2004


Hi,

the line numbers of inspect.getinnerframes are
different from traceback.format_exception.

This results in wrong lines being shown in cgitb.

An example is below.

I looked at the source of both methods. One uses f_lineno (wrong)
the other tb_lineno (correct).

I use python 2.3.3

Is this fixed in a newer version?

See:
===> python tb.py
Correct traceback (traceback.format_exception)
Traceback (most recent call last):
  File "tb.py", line 34, in ?
    main()
  File "tb.py", line 18, in main
    printtb()
  File "tb.py", line 13, in printtb
    assert(0)
AssertionError


Wrong traceback (inspect.getinnerframe)
  File "tb.py", line 34, ...
if __name__=="__main__":
  File "tb.py", line 18, ...
    printtb()
  File "tb.py", line 15, ...
        pass



tb.py:
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-


# Python Imports
import os
import sys
import inspect
import traceback

def printtb():
    try:
        assert(0)
    except ValueError:
        pass

def main():
    printtb()

class Hook:
    def __call__(self, etype, evalue, etb):
        print "Correct traceback (traceback.format_exception)"
        print ''.join(traceback.format_exception(etype, evalue, etb))
        print
        print "Wrong traceback (inspect.getinnerframe)"
        context=5
        records = inspect.getinnerframes(etb, context)
        for frame, file, lnum, func, lines, index in records:
            print '  File "%s", line %s, ...\n%s' % (
                file, lnum, lines[context/2].rstrip())
            
if __name__=="__main__":
    sys.excepthook=Hook()
    main()






More information about the Python-list mailing list