new command for getting traceback?

Fredrik Lundh fredrik at effbot.org
Sun Feb 4 17:52:22 EST 2001


Pete wrote:
> one trick in python i've always thought was bizarre was getting
> the current stack trace by raising a false exception.
>
>         import sys
>         try:
>             raise None
>         except:
>             return sys.exc_info()[2].tb_frame

in 2.1, you can use sys._getframe():

    Python 2.1a1
    >>> import sys
    >>> print sys._getframe.__doc__
    _getframe([depth]) -> frameobject

    Return a frame object from the call stack.  If optional integer depth is
    given, return the frame object that many calls below the top of the stack.
    If that is deeper than the call stack, ValueError is raised.  The default
    for depth is zero, returning the frame at the top of the call stack.

    This function should be used for internal and specialized
    purposes only.

for more traceback/frame manipulation stuff, see the traceback
module.

Cheers /F





More information about the Python-list mailing list