From: Don T. <nos...@gm...> - 2006-06-01 15:40:15
|
SourceForge.net wrote: I found Fabio's example very handy so I made it into a module and used the pretty printer instead of print: FWIW, here is my excepthooker.py file: """ Print locals and globals on any exception. Usage: import sys from excepthooker import myHook ... if __name__ == '__main__': sys.excepthook = myHook ... """ import sys import pprint def myHook(type, value, traceback): ppr = pprint.PrettyPrinter(indent=4) initial_ctx = traceback.tb_next while initial_ctx.tb_next is not None: initial_ctx = initial_ctx.tb_next print 'locals on exception:' ppr.pprint(initial_ctx.tb_frame.f_locals) print 'globals on exception:' ppr.pprint(initial_ctx.tb_frame.f_globals) #now, call the original. sys.__excepthook__(type, value, traceback) # Testing code follows def m1(a = 10, b = 27): global c c=42 raise RuntimeError('here') if __name__ == '__main__': sys.excepthook = myHook m1() |