2

What would be the easiest way to execute a Python script and, for each executed statement, pass the line number or the line itself to a callback function? For example

A piece of code to be executed

for i in range(5):
    z = i**2

and a callback

def callback(line):
   print line

The output would be:

for i in range(5):
    z = i**2
for i in range(5):
    z = i**2
for i in range(5):
    z = i**2
...
etc

Another way of stating this is that I want to know the piece of code that would be next to execute if I were running the script stepping through a debugger.. I've seen this question about tracing but I'm interested in doing more than tracing the number of times executed in the callback function.

A debugger is going to be helpful, but only if I can run it non-interactive mode with the ability to call back to the python code

3 Answers 3

5

Look at the sys.settrace function. This allows you to specify a tracing function which is executed for every line of code. It was, I think, specifically implemented to create pdb so it's at the right level of abstraction you seem to want. Implementing what you want will not be trivial but I think it's the best place to start.

For an production program that uses this, look at coverage.py.

Sign up to request clarification or add additional context in comments.

Comments

3

Accepting Noufal's answer since it put me on the right path, but this helped as well

http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html

Comments

3

The April's fools module goto is a working example of tracing implemented in Python, so although it's doubtlessly slower (than the C implementation in coverage.py), it's rather easy to understand. http://entrian.com/goto/

2 Comments

Thats... sort of horrible, but interesting :)
@dfb Indeed. It also shows how to change the execution head to another line. Based upon that code, I implemented an absolute and relative JMP instruction. Quite fun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.