打印python的堆栈stack

import sys

def pstack(depth = 0):
    frame = sys._getframe(depth)
    cnt = 0
    while frame:
        print "###", cnt, frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno
        frame = frame.f_back
        cnt = cnt + 1

def test():
    pstack(0)
    print "-"*20
    pstack(1)

if __name__ == "__main__":
    test()
posted @ 2015-08-12 23:16  aprial  阅读(1278)  评论(0编辑  收藏  举报