"""Timing and profiling traceback generation. This script requires IPython to be installed to run. Under Python 2.5, the generation of IPython's fancy tracebacks has become a LOT slower than it was under Python 2.4. This script can be run under IPython via run traceback_timings [args] to see the effect better. While it can also be run at a plain command line, the effect is less pronounced, because the problem gets worse the more modules have been loaded. If run inside ipython, the large number of modules loaded by ipython itself will make this much worse. Optionally, the following pair of args can be given (both need to be there): - mode: one of 'Context', 'Verbose', 'Plain'. These are the modes for exception reporting. - stack: how deep a stack trace to produce. This script causes an exception by using a recursive function. This value lets you control how deep the stack should be before the exception is raised. """ import sys,time,profile import pstats from IPython import ultraTB # get command-line args. try: mode,stack = sys.argv[1:3] stack = int(stack) except: mode = 'Context' stack = 5 # make a profiler to time things prof = profile.Profile() # Install ipython's exception handler xhandler = ultraTB.AutoFormattedTB(mode=mode,color_scheme='Linux') # Simple recursive function which will eventually raise an exception def f(x,y=None): if x==0: msg = 'Bad value' raise ValueError(msg) else: y = 'something...' return f(x-1,y) try: # Cause an exception f(stack) except: # And handle it under the control of the profiler pp = prof.run('xhandler(*sys.exc_info())') # Make a stats object and print some info about it. If this is run under # IPython, further exploration of the stats variable may be useful. stats = pstats.Stats(pp).strip_dirs() stats.sort_stats('pcalls').print_stats(.25)