Limit traceback from most recent call

Brian Allen Vanderburg II BrianVanderburg2 at aim.com
Mon Dec 15 22:46:28 CET 2008


yinon.me at gmail.com wrote:
> On Dec 14, 8:07 pm, Brian Allen Vanderburg II
> <BrianVanderbu... at aim.com> wrote:
>   
>
> Hi,
>
> The interface of extract_tb is:
> traceback.extract_tb(tb, limit=None)
> try to play with the 'limit' argument
>
> Good luck,
>    Yinon
> --
> http://mail.python.org/mailman/listinfo/python-list
>   
I have, but the limit argument shows the first items and not the last 
items (where the exception occurred):

def a():
    b()

def b():
    c()

def c():
    d()

def d():
    open('/path/to/fake/file')

try:
    a():
except:
    print traceback.format_exc(limit=2)

try:
    a():
except:
    print format_partial_exc(limit=2)



Prints something like this


Traceback (most recent call last):
  File "b.py", line 52, in ?
    a()
  File "b.py", line 19, in a
    b()
IOError: [Errno 2] No such file or directory: '/path/to/fake/file'

Traceback (most recent call last):
  File "b.py", line 25, in c
    d()
  File "b.py", line 29, in d
    handle=open('/path/to/fake/file', 'rt')
IOError: [Errno 2] No such file or directory: '/path/to/fake/file'


The second one (using format_partial_exc) prints the last two items of 
the traceback instead of the first, showing the error is line 29 in 
function d, the first one limits the traceback but gives no indication 
of where the error occurred.


Brian A. Vanderburg II
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081215/f9adde2f/attachment.html>


More information about the Python-list mailing list