gpt4 book ai didi

python - inspect.currentframe() 在某些实现下可能不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:33 29 4
gpt4 key购买 nike

根据docs :

inspect.currentframe()

Return the frame object for the caller’s stack frame.

CPython implementation detail: This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns None.

怎么只有这个函数被标记为“implementation-dependent”?如果这个功能不行,那inspect.traceinspect.stack等类似功能不也无法使用吗?

此外,“堆栈框架支持”是什么意思,为什么它会不存在?

最佳答案

inspect.currentframe 的可用性与 sys._getframe 相关:

def currentframe():
"""Return the frame of the caller or None if this is not possible."""
return sys._getframe(1) if hasattr(sys, "_getframe") else None

因此,该限制适用于所有其他也使用 sys._getframe 的函数。对于 inspect,这只是 inspect.stack

相比之下,inspect.trace 使用sys.exc_info。这是异常处理方案的组成部分,应该始终可用。所有其他相关功能,例如getframeinfo,已经依赖有框架了。它们的适用性取决于您是要检查异常还是调用回溯。

请注意,我的本地默认 jython 确实支持 sys._getframe。如果使用 -X:Frames 运行,ipy 可以工作。

关于python - inspect.currentframe() 在某些实现下可能不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9938980/

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