gpt4 book ai didi

python - 为什么Python日志记录模块的源代码在try中引发异常?

转载 作者:行者123 更新时间:2023-11-30 22:00:26 24 4
gpt4 key购买 nike

在阅读python日志记录模块的源代码时,我发现了如下代码:

# next bit filched from 1.5.2's inspect.py
def currentframe():
"""Return the frame object for the caller's stack frame."""
try:
raise Exception
except:
return sys.exc_info()[2].tb_frame.f_back


为什么?等于这个吗?

def currentframe():
return sys.exc_info()[2].tb_frame.f_back

最佳答案

显而易见-在出现某些异常之前,没有exc_info(异常信息)。因此,我们需要引发异常来访问其信息并从中获取调用堆栈。

这似乎是访问当前调用堆栈的最简单方法。

关于python - 为什么Python日志记录模块的源代码在try中引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com