--- profile.py.bak 2011-08-03 11:36:42.484780000 +1000 +++ profile.py 2011-08-03 11:37:37.665153000 +1000 @@ -74,14 +74,15 @@ """ prof = Profile() try: - prof = prof.runctx(statement, globals, locals) - except SystemExit: - pass - - if filename is not None: - prof.dump_stats(filename) - else: - return prof.print_stats(sort) + try: + prof = prof.runctx(statement, globals, locals) + except SystemExit: + pass + finally: + if filename is not None: + prof.dump_stats(filename) + else: + return prof.print_stats(sort) if hasattr(os, "times"): def _get_time_times(timer=os.times):