Linenumbers: inspect.getinnerframes vs. traceback.format_exception

T

Thomas Guettler

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()
 
T

Thomas Guettler

Am Mon, 11 Oct 2004 13:29:30 +0200 schrieb Thomas Guettler:
Hi,

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

Found the fix in the CVS:

Revision 1.47.8.1 - (view) (download) (as text) (annotate) - [select for
diffs] Sat Jun 5 14:14:48 2004 UTC (4 months ago) by akuchling Branch:
release23-maint
Changes since 1.47: +3 -1 lines
Diff to previous 1.47

[Bug #954364] inspect.getframeinfo() sometimes produces incorrect traceback line #s;
fix is to look at tb.tb_lineno, not tb.frame.f_lineno. Patch from Robin
Becker and me.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top