Ticket #349 (new enhancement)
Make generated Python code importable
| Reported by: | moraes | Owned by: | mitsuhiko |
|---|---|---|---|
| Priority: | trivial | Milestone: | |
| Component: | general | Version: | dev |
| Keywords: | compiler | Cc: |
Description
Currently the compiled Python code generated by Jinja2 is not importable because it uses a 'environment' variable injected during exec. Wrapping the generated code by a function would make it usable through import, creating a new modality of template loading - import as module.
It's an edge case, but it's super fast and would be particularly useful in App Engine - where we need to use precompiled templates to have a performance comparable to the one provided by the bytecode cache.
The idea: since we are precompiling templates on deployment, why go through compile() and exec while we can simply import the precompiled code as a module and run it? Making the generated code importable would allow this, while keeping other loading methods unaffected.
A proof of concept is here:
http://paste.pocoo.org/show/124381/
And the related discussion is here:
http://groups.google.com/group/pocoo-libs/browse_thread/thread/748b0d2024f88f64?pli=1