Getting a stack frame

How do I get the stack stack to go to traceback.print_stack

?

From Python 3.3a1 :

traceback.print_stack(f=None, limit=None, file=None)

This function prints the stack trace from the point of the call. An optional f

argument can be used to specify an alternate stack frame to start. the optional arguments limit and file have the same meaning as for print_exception()

.

But nowhere in the docs I found a way to get the stack frame. To be specific, let's say I want to print a stack trace starting at the level above the calling point. How can i do this?

+3


source to share


3 answers


inspect.stack()

will give you the current stack as a list. You can choose any frame you want from. You can also do eg. inspect.currentframe().f_back

to get your caller's frame. Basically, the module inspect

is where it is.



+2


source


This documentation contains information about functions that you can use to get a stack frame, eg inspect.currentframe()

.



+2


source


In addition to the validation module, you can try:

import sys
sys._getframe(1)

      

or

import sys
sys._getframe().f_back

      

Warn that this is a private function, some python versions may not implement it.

+1


source







All Articles