Can I get the local variables of a Python function from which an exception was thrown?

后端 未结 6 775
时光说笑
时光说笑 2020-12-08 00:54

I\'m writing a custom logging system for a project. If a function throws an exception, I want to log its local variables. Is it possible to access the raising function\'s

6条回答
  •  遥遥无期
    2020-12-08 01:26

    try:
        myfunction()
    except:
        import sys
        type, value, tb = sys.exc_info()
        while tb.tb_next:
            tb = tb.tb_next
        frame = tb.tb_frame
        print frame.f_locals['v1']
    

提交回复
热议问题