all,

I'm writing a function meant to print out the context of a given
function call when executed - for example:

1. def main():
2.
3. _st = stack_trace_closure("/path/to/log")
4. _st()
5. _st()

would print out

    /path/to/file.py:4
    /path/to/file.py:5

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
prematurely.

I've tried this in python2.7 and python3.3, both show this.

thanks much for any info,

Ed

code follows:
---

def stack_trace_closure(message, file_name=None, frame=3):

    fh = open(file_name, "w+")

    def _helper():
        return stack_trace(message, frame, fh)

    return _helper

def stack_trace(message _frame, fh):

    _bt = traceback.extract_stack()

     fh.write( "%s:%s - %s" % (_bt[_frame][0], _bt[_frame][1], _message))
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/4MKHPCRNAJACKIBMLILMQMUPTEVFD3HW/

Reply via email to