Limit traceback from most recent call

Brian Allen Vanderburg II BrianVanderburg2 at aim.com
Sun Dec 14 13:07:10 EST 2008


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



More information about the Python-list mailing list