Chris Warburton’s Python-Decompiler¶
Python-Decompiler is a project by Chris Warburton which can generate source code from ASTs.
Links to Python-Decompiler¶
- The most recent form of the project is stored on gitorious.
- A previous version was stored on Github (whence I forked it while working with PyMeta).
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
- Comments are stripped
- This is expected.
- Leading tabs are converted to spaces.
- This is unsurprising, but not in the spirit of PEP 8’s distinction about “new code”
- Some extra blank lines are added
- 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.
- Re-formatting expressions to introduce spaces around operators
- 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.