[Python-Dev] AST optimizer implemented in Python (original) (raw)

martin at v.loewis.de martin at v.loewis.de
Sun Aug 12 21:48:19 CEST 2012


I'm not sure if this is directly related or not, but making this mechanism support custom compilation for new filename suffixes would be nice, especially for various e.g. HTML/XML templating systems that compile to Python or bytecode.

Specifically, having a way to add a new source suffix (e.g. ".kid", ".zpt", etc.) and a matching compilation function, such that it's automatically picked up for compilation by both the filesystem and zip importers would be awesome. It'd also allow for DSLs and syntax experiments using alternative filename extensions.

How would the compilation (and the resulting code) then be invoked?

If it is through import statements, it should already be possible to have import load an html file.

However, ISTM that what you want is not modules, but files which rather are similar to individual functions. So the feature would go as an extension to exec() or eval(). I'm skeptical that this can be generalized in a useful manner - the exec/eval/execfile family already has variations depending on whether the thing to run is a single statement, a block, or an expression. It matters whether it gets its parameters passed, or somehow draws them from the environment - and then, which sources?

If you would want to support HTML template engines alone, you find that they typically have many distinct parameter sets (the request, the ORM, the process environment, and then actual python-level parameters). So defining something that compiles it may be the easy part; the tricky part is defining something that then executes it.

Regards, Martin



More information about the Python-Dev mailing list