Chris Warburton’s Python-Decompiler

Python-Decompiler is a project by Chris Warburton which can generate source code from ASTs.

Summary

The Python-Decompiler project includes a directory called python_rewriter which defines an OMeta grammar to parse Python’s ASTs and thence render Python code. it is based on the ASTs of the 2.x compiler module

It is released into the Public Domain, so can be copied for investigation.

Rendering

Running Python-Decompiler on itself

Running

Run the unparser, with the commands:

$ python
>>> from python_rewriter import base
>>> source_text = file('base.py').read()
>>> parsed_source = base.parse(source_text)
>>> parsed_sources = [parsed_source]
>>> grammared_sources = base.grammar(parsed_sources)
>>> pythoned_sources = grammared_sources.apply('python', 0)
>>> text = pythoned_sources[0]
>>> file('base-out.py','w').write(text)

Some differences are apparent

  1. Comments are stripped
    This is expected.
  2. Leading tabs are converted to spaces.
    This is unsurprising, but not in the spirit of PEP 8’s distinction about “new code”
  3. Some extra blank lines are added
  4. Extraneous parentheses are introduced
    Some of these seem improvident, such as those introduced around conditions of (some) if statements. Others seem harmless, or even helpful, such as introducing parentheses around a literal tuple. Introducing parentheses around expressions after a print statement is dubious, given that I used python 2, not 3.
  5. Re-formatting expressions to introduce spaces around operators
  6. Reduction of a multi-line, triple-quoted string to a single-quoted string containing ‘n’ characters.
    This is unfortunate as this string is a few hundred lines long and contains the OMeta grammar. The reduction renders the grammar effectively unreadable.

Most of the above issues seem to be matters of opinion and should be deferred to PEP 8. This unparser’s effects seem very similar to those of Python’s unparser.

Pepping

Conclusion

This is not a test of Python-Decompiler, just an indication of where some of its strengths/weaknesses may lie.