Limit traceback from most recent call

Ethan Furman ethan at stoneleaf.us
Mon Dec 15 11:31:53 EST 2008


Brian Allen Vanderburg II wrote:
> I've looked at traceback module but I can't find how to limit traceback 
> from the most recent call if it is possible.  I see that extract_tb has 
> a limit parameter, but it limits from the start and not the end.  
> Currently I've made my own traceback code to do this but wonder if 
> python already has a way to do this build in:
> 
> def format_partial_exc(limit=None):
> 
>    (type, value, tb) = sys.exc_info()
> 
>    items = traceback.extract_tb(tb)
> 
>    if limit:
> 
>        items = items[-limit:] # Get last 'limit' items and not first
> 
>    result = 'Traceback (most recent call last):\n'
> 
>    items = traceback.format_list(items)
> 
>    for i in items:
> 
>        result += i # Newline already included
> 
>    result += type.__name__ + ': ' + str(value)
> 
>    return result
> 
> Is this possible currently from traceback or other python module?
> 
> Brian A. Vanderburg II

If memory serves, you can catch the exception, then re-raise it -- this 
should hide all the traceback below where you caught it, but still 
return the actual error.

Hope this helps.

~Ethan~



More information about the Python-list mailing list