I want to build a visual debugger, which helps programming students to see how expression evaluation takes place (how subexpressions get evaluated and "replaced" by their values, something like expression evaluation visualizer in Excel).
Looks like you can't step through this process with Python's pdb, as its finest step granularity is line of code. Is it somehow possible to step through Python bytecode? Any other ideas how to achieve this goal?
EDIT: I need a lightweight solution that can be built on top of CPython standard library.
To evaluate an algebraic expression, you have to substitute a number for each variable and perform the arithmetic operations. In the example above, the variable x is equal to 6 since 6 + 6 = 12. If we know the value of our variables, we can replace the variables with their values and then evaluate the expression.
Python executes a statement by evaluating its expressions to values one by one, then performing some operation on those values. Python evaluates an expression by first evaluating its sub-expressions, then performing an operation on the values.
The eval() function evaluates the specified expression, if the expression is a legal Python statement, it will be executed.
I have a solution idea also myself -- I could instrument the code (or AST) by wrapping all (sub)expressions in a dummy method call, which does nothing more than returning its argument. Eg.
x = f(sin(x + y))
becomes
x = dummy(f(dummy(sin(dummy(dummy(x) + dummy(y))))))
This way I'm guaranteed to be notified after each subexpression gets evaluated and I also get the values. I can also add extra location/AST information about which part of the expression is currently dealt with, eg:
... dummy(x, line=23, col=13, length=1) ...
Unfortunately this requires messing with AST and compilation ...
Have you tried pudb? http://pypi.python.org/pypi/pudb On a debian-like: apt-get install python-pudb
It attaches to pdb, so I guess this is not what you're looking for. At least, when you step in a function, it clearly appears which one you're in.
For teaching students, something that you could can be:
I know, it is far to be perfect, but this is the best I can think of, at the moment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With