compiler package vs parser

Robin Becker robin at reportlab.com
Mon Apr 20 05:47:35 EDT 2009


Kay Schluehr wrote:
>> 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.
.......
thinking about it I just made the wrong decision back in 2004; we observed a 
semantic change caused by the new scoping rules and tried to fix using the wrong 
model; back then we were probably supporting 2.0 as well so the parser module 
probably wasn't available everywhere anyway; even today the ast stuff isn't 
available in 2.4. I prefer the ast approach as preppy is effectively indentation 
free which makes the tree harder to synthesize for the parser tree.
-- 
Robin Becker




More information about the Python-list mailing list