2013/3/20 anatoly techtonik <techtonik@gmail.com>
Hi,

I've created a module to dump function trace during execution of Python script. You can see session example in attachment. The format is of PHP Xdebug tool [2] just because I had some scripts from the past to analyze it.

The module uses sys.settrace() to analyse frames in 'call' events with callback(frame, event, arg).

Recently I've got an anonymous report that some frame misses filename information when run under IDE:
https://bitbucket.org/techtonik/xtrace/issue/2/use-of-xtrace-module-within-ide-causes

There is unlikely to be any more feedback from the user to make me understand and reproduce the behavior, nor there is a sufficient documentation in sys.trace description [1]. So I am on my own. But I can not read C code the CPython is written in, so I ask here.

What are possible execution contexts for Python?
How each execution context affects data structure that is passed to trace function (frame and arg)?

It's simpler than that: when running from an interactive session, f_back is empty...

>>> def trace(frame, event, arg):
...     funcname = frame.f_code.co_name
...     if funcname == '<module>':
...         print frame.f_back.f_code.co_filename   
... 
>>> import sys
>>> sys.settrace(trace)
>>> 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in trace
AttributeError: 'NoneType' object has no attribute 'f_code'

--
Amaury Forgeot d'Arc