Is there a reliable, automatic way (such as a command-line utility) to check if two Python files are equivalent modulo whitespace, semicolons, backslash continuations, comments, etc.? In other words, that they are identical to the interpreter?
For example, this:
import sys
sys.stdout.write('foo\n')
sys.stdout.write('bar\n')
should be considered equivalent to this:
import sys
sys.stdout.\
write('foo\n'); sys.stdout.\
write(
'bar\n') # This is an unnecessary comment
Use the
astmodule.Example (for Python 2):
You can of course read in the source code from actual files instead of string literals.
Edit:
So that the comments make sense, I’d like to note that I originally proposed using the built-in
compile()function. However, @Jian found a simple case that it didn’t handle well. Perhaps it could be adapted, as suggested by @DSM, but then the solution becomes a little less tidy. Maybe not unreasonably so, but if theastparse-and-dump works as well or better, it’s the more straightforward way.