[Python-ideas] Enabling access to the AST for Python code
Andrew Barnert
abarnert at yahoo.com
Fri May 22 03:51:34 CEST 2015
On May 21, 2015, at 18:18, Ben Hoyt <benhoyt at gmail.com> wrote:
>
> (I know that there's the "ast" module and ast.parse(), which can give
> you an AST given a *source string*, but that's not very convenient
> here.)
Why not? Python modules are distributed as source. You can pretty easily write an import hook to intercept module loading at the AST level and transform it however you want. Or just use MacroPy, which wraps up all the hard stuff (especially 2.x compatibility) and provides a huge framework of useful tools. What do you want to do that can't be done that way?
For many uses, you don't even have to go that far--code objects remember their source file and line number, which you can usually use to retrieve the text and regenerate the AST.
More information about the Python-ideas
mailing list