473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Linenumbers: inspect.getinnerframes vs. traceback.format_exception

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

Jul 18 '05 #1
1 2522
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

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Hunter | last post by:
What is the best way to get the traceback as a string. I tried def exception_to_str(s = None): sh = StringIO.StringIO() if s is not None: print >>sh, s traceback.print_stack(sh) return...
1
by: Oliver Walczak | last post by:
This seems to be a quite difficult approach. Try this: ##################################################################### import traceback class MyTraceback: def __init__(self):...
1
by: Thomas Guettler | last post by:
Hi, Sometimes the linenumbers of a traceback are wrong. See this traceback. I don't use any attribute "key" here. The method search_dict does not use this attribute, too. Traceback (most...
7
by: Robin Becker | last post by:
def raise_an_error(): a = 3 b = 4 c = 0 try: a = a/c except: import sys, cgitb, traceback, inspect tbt,tbv,tb = sys.exc_info() print...
6
by: EAS | last post by:
Hi, I'm wondering if there is any way to keep a program running when it runs into an error (using the 'try' structure) and print the traceback to the screen? Thanks.
4
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a problem to get the command that has called a function if the command was given on multiple lines. E.g.: ################################################### import inspect ...
1
by: Josh Close | last post by:
When sys.excepthook is called, type, value and traceback are passed into it. How do I get the values of traceback? I've tried printing it, but I get the memory location. I'm gussing there are...
2
by: Fernando Perez | last post by:
Hi all, IPython has suffered quite a few problems with the inspect module in python 2.3. For these, unfortunately all I've been able to do is guard with overreaching except clauses, as I had...
0
by: Stef Mientki | last post by:
Gabriel Genellina wrote: REALLY GREAT ! But this is fully beyond my understanding: m = getmembers ( wx ) print m runs fine print getmembers ( wx )
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.