开发者

Python: sys.exc_info() is missing local variables

开发者 https://www.devze.com 2023-01-02 23:44 出处:网络
In Python, I have code that catches an exception, like this: try: <do stuff> except: exc_info = sys.exc_info()

In Python, I have code that catches an exception, like this:

try:
    <do stuff>
except:
    exc_info = sys.exc_info()

The problem I'm running into is that the traceback object (exc_info[2]) is non-deterministically missing local variables in the traceback objects. I know this for a fact because I run the exact same code and sometim开发者_高级运维es the variables are in tb.tb_frame.f_locals and sometimes not. The global variables are always correct in tb.tb_frame.f_globals.

What is populating the f_locals structure? What might cause that local variables dictionary to be empty sometimes?


What is in the locals is completely dependent on the code in that local context. For example:

if condition:
    foo='foo'
else:
    bar='bar'

if condition==True then foo will be in locals() and bar will not, otherwise bar will be in locals() and foo will not.

0

精彩评论

暂无评论...
验证码 换一张
取 消