I'm writing a function meant to print out the context of a given
function call when executed - for example:
1. def main():
3. _st = stack_trace_closure("/path/to/log")
would print out
for each line when executed. Basic idea is to create a closure and
associate that closure with a filename, then run that closure to print
to the log without needing to give the filename over and over again.
So far so good. But when I write this function, the frames given by
getframeinfo or extract_stack skip the actual calling point of the
function, instead giving back the *point where the closure was
defined*. (in the above example, it would print /path/to/file.py:3,
/path/to/file.py:3 instead of incrementing to show 4 and 5).
However, when I insert a pdb statement, it gives me the expected
calling frame where _st is actually called.
What's going on here? It looks an awful lot like a bug to me, like an
extra frame is being optimized out of of the closure's stack
I've tried this in python2.7 and python3.3, both show this.
thanks much for any info,
def stack_trace_closure(message, file_name=None, frame=3):
fh = open(file_name, "w+")
return stack_trace(message, frame, fh)
def stack_trace(message _frame, fh):
_bt = traceback.extract_stack()
fh.write( "%s:%s - %s" % (_bt[_frame], _bt[_frame], _message))