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.
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.
becomes
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:
Unfortunately this requires messing with AST and compilation …