How to step through Python expression evaluation p

2019-04-27 05:06发布

问题:

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.

回答1:

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:

  • first, write the program using variables, composing using several steps,
  • debug this program using whichever decent python debugger (pdb, winpdb, pudb ...),
  • then, once the process is well understood, get rid of temporary variables, by combining the code into fewer lines, gradually, until you come to the final code.

I know, it is far to be perfect, but this is the best I can think of, at the moment.



回答2:

Using pdb, any function call can be stepped into. For any other statement, pdb can print the values of the relevant names in the line. What additional functionality are you looking for that isn't covered?

If you're trying to 'step into' things like a list comprehension, that won't work from a pure Python perspective because it's a single opcode. At some point for every expression you'll need to tell your students 'and this is where Python goes into the C implementation and evaluates this...'.



回答3:

You should check out reinteract, it's pretty simple and you could contribute to that



回答4:

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 ...