Understanding some python internals
Gerson Kurz
gerson.kurz at t-online.de
Mon Nov 11 14:33:42 EST 2002
I'm trying to understand how the python debugger package works. To
that effect, I've written a kind of "strace for python", that should
tell me each instruction as it happens, in a tracefile.
The code basically does this:
- use sys.settrace() to attach a tracefunc to every statement executed
- try to analyze the arguments passed to the tracefunc
So far, so good. Preliminary results can be seen here:
http://p-nand-q.com/python/strace.zip
The test code I want to analyze is this:
------------------------
test = 60
def test(x):
return x*2
i = 0
while i < 5:
print test(i)
i += 1
exec("test(42)")
------------------------
Nothing too deviant, right? Try to run strace.py over this, and look
into the result (STRACE.LOG).
Ok, my questions:
(1) I want to disassemble the current step that the tracefunc is
called for - nothing else. dis.disassemble takes a code object, but
will disassemble the *whole* code object, not just some part. (Same,
if I pass it frame rather than code). How can I do that?
I would guess that it has something to do with the members
frame.f_code.co_code and co_lnotab - the "documentation" says the
latter is "encoded mapping of line numbers to bytecode indices", can
somebody enlighten me on that?
(2) I want to print the source for the current step that the tracefunc
is called for - nothing else. My first attempt (which is in the
current source) is to read the file from frame.f_code.co_filename and
read the line that is given in frame.f_lineno). This has two problems:
a) if the source spans more than one line, only the first line will be
shown
b) if the source is in an exec() statement, co_filename is "<string>".
(See also question (3))
Is there are a "canonic" way of getting the code about to be executed
inside a tracefunc?
I tried inspect.getsource(frame) - but that prints the source for the
whole code object, not the current frame. Bug or intended behaviour?
(3) If the code object is in an exec statement, co_filename is
"<string>" and as far as I can see the code executed cannot be read
from inside the tracefunc. Or can it?
More information about the Python-list
mailing list