compiler package vs parser

Kay Schluehr kay.schluehr at gmx.net
Fri Apr 17 19:45:02 CEST 2009


> I realize that I probably ought to be trying this out with the newer ast stuff,
> but currently I am supporting code back to 2.3 and there's not much hope of
> doing it right there without using the compiler package.

You might consider using the *builtin* parser module and forget about
the compiler package if it is broken ( I take your word that it is )
or modern ast representations which aren't really necessary for Python
anyway.

>>> import parser
>>> tree = parser.suite("def foo():\n print 42}\n")
>>> code = tree.compile()
>>> exec code
>>> foo()
42

This is also not 100% reliable ( at least not for all statements in
all Python versions ) but it uses the internal parser/compiler and not
a standard library compiler package that might not be that well
maintained.



More information about the Python-list mailing list