Inspect.currentframe () might not work on some implementations?
According to the docs :
inspect.currentframe()
Returns the frame object for the stack callers of the frame.
CPython implementation details: This feature is based on the Python stack support for a frame in the interpreter, which is guaranteed not to exist in all Python implementations. Returns None if executed in a non-Python Stack Frame implementation.
How is it that only this function is marked as "implementation-dependent"? If this function does not work, will similar functions such as inspect.trace
, inspect.stack
etc. not be available ?
Also, what does "stack frame support" mean and why was it ever missing?
source to share
Stumbled upon this question while looking for the answer itself. Availability is inspect.currentframe
tied to 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
Thus, the limitation applies to all other functions using sys._getframe
. For inspect
this only inspect.stack
.
Unlike inspect.trace
used sys.exc_info
. It is an integral part of the exception handling schemes and should always be available. All other related functions eg. getframeinfo
already rely on having a frame. Their applicability depends on whether you want to check for an exception or monitor traffic.
Note that mine is local, jython supports by default sys._getframe
. ipy works if works with -X:Frames
.
source to share