< October 2005 >
SuMoTuWeThFrSa
       1
2 3 4 5 6 7 8
9101112131415
16171819202122
23242526272829
3031     
Tue, 04 Oct 2005:

This is some code I re-did for lawgon, while on IRC. This one dates to the time I was trying to get BitTorrent to tunnel over HTTP and ran into trouble figuring out which function is called by what. This is not as good as the original version - but this is quite good if you want to debug python without putting too many print statements :)

import sys 

def tracer (frame, event, arg):
    if event == "call":
        print "%s() --> from %s:%d" % (frame.f_code.co_name, frame.f_back.f_code.co_filename, frame.f_back.f_lineno)

def fact(i):
    if(i == 1):
        return 1
    return fact(i-1) * i

sys.settrace(tracer)

def main():
    print fact(9)

This is by no means original code and shouldn't be misunderstood to be my invention. If you want, you could try playing around with frame.f_globals["__file__"] or __module__. I think you could even hack in linecache to build a debugger of sorts. Python rocks !!

--
* If you see this blog rockin', don't come a knockin'

posted at: 18:43 | path: /hacks | permalink | Tags: