# Week 3 ## Python Debugging ## ###################################################################### # Autoload debugger # sitecustomize.py # from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65287 # # code snippet, to be included in 'sitecustomize.py' # 'sitecustomize.py' needs to be in the python path, # such as in the site-packages directory import sys def info(type, value, tb): if hasattr(sys, 'ps1') or not sys.stderr.isatty(): # we are in interactive mode or we don't have a tty-like # device, so we call the default hook sys.__excepthook__(type, value, tb) else: import traceback, pdb # we are NOT in interactive mode, print the exception... traceback.print_exception(type, value, tb) print # ...then start the debugger in post-mortem mode. pdb.pm() sys.excepthook = info ###################################################################### # Buggy program example import math import sitecustomize def factorize(): print "Number to factorize: ", a = raw_input() for i in primes(math.sqrt(a)): while (a % i == 0) : a = a / i result.append(i) print result def isPrime(n): if n == 2: return True else: return primeCheck(n, primes(int(math.sqrt(n)))) def primeCheck(n, l): return n%l[0] and primeCheck(n,l[1:]) def primes(n): return filter(isPrime, range(2, n+1)) if __name__ == "__main__": factorize()