Wednesday, October 19, 2016

Having fun with python tracer

import tkinter,sys,inspect
def checkfun(frame,event,arg) :
    caller = inspect.getframeinfo(inspect.stack()[1][0])
    print ("%s:%d - %s" % (caller.filename, caller.lineno, event))
def test() :
for i in range(4) :
sys.setprofile(checkfun)
sys.settrace(checkfun)
exec("x=0\nx=x+1\nx=4")
sys.setprofile(None)
sys.settrace(None)

This has the shell output:

C:\Users\anthony1\Python\Lib\tkinter\test.py:11 - c_call
<string>:1 - call
<string>:1 - call
<string>:3 - return
C:\Users\anthony1\Python\Lib\tkinter\test.py:11 - c_return
C:\Users\anthony1\Python\Lib\tkinter\test.py:12 - c_call
C:\Users\anthony1\Python\Lib\tkinter\test.py:10 - c_call
C:\Users\anthony1\Python\Lib\tkinter\test.py:10 - c_return
C:\Users\anthony1\Python\Lib\tkinter\test.py:11 - c_call
<string>:1 - call
<string>:1 - call
<string>:3 - return
C:\Users\anthony1\Python\Lib\tkinter\test.py:11 - c_return
C:\Users\anthony1\Python\Lib\tkinter\test.py:12 - c_call

And we can see that both the test code and the code inside the quotes for exec have line numbers relative to the start of the string or the start of the file, test.

I will use this wisely in the rednecks debugger, and fix a few other things.

Inexperienced programmers! A red flag.  If you try to run your message loop from the interpreter, and do it just right, it would be neat.  If it doesn't work, the floor of your microprocessor will heat up, generally melting out the power traces, causing flames. Or, that could have been the inch of dust.

No comments: