| 1 |
(#2) Conditionalized #import behavior |
| 2 |
===================================== |
| 3 |
|
| 4 |
|
| 5 |
:CHEP: 2 |
| 6 |
:Title: Conditionalized #import behavior |
| 7 |
:Version: 1 |
| 8 |
:Author: R Tyler Ballance <tyler at slide.com> |
| 9 |
:Status: Draft |
| 10 |
:Type: Standards Track |
| 11 |
:Content-Type: text/x-rst |
| 12 |
:Created: 07-Jun-2009 |
| 13 |
|
| 14 |
---- |
| 15 |
|
| 16 |
Abstract |
| 17 |
-------- |
| 18 |
This CHEP proposes an update to the way the #import and #from |
| 19 |
directives are handled such that locally scoped imports and |
| 20 |
module-level imports are handled appropriately. |
| 21 |
|
| 22 |
|
| 23 |
Motivation |
| 24 |
---------- |
| 25 |
Currently Cheetah (v2.2.1) provides two different, but mutually exclusive, |
| 26 |
means of importing Python modules with the #from/#import directives. The |
| 27 |
"traditional" handling for #from/#import (hereafter referred to as "module imports") |
| 28 |
is that the generated import statements shall all be relocated to |
| 29 |
the top of the generated module's source code, i.e. :: |
| 30 |
|
| 31 |
#import cjson |
| 32 |
|
| 33 |
Hello $cjson.encode([1, 2, 3]) |
| 34 |
|
| 35 |
|
| 36 |
Will result in generated module code along the lines of:: |
| 37 |
|
| 38 |
import cjson |
| 39 |
|
| 40 |
class Foo(Template): |
| 41 |
def writeBody(self): |
| 42 |
write('Hello ') |
| 43 |
write(cjson.encode([1, 2, 3])) |
| 44 |
|
| 45 |
|
| 46 |
Also currently in Cheetah is the ability to switch off this |
| 47 |
behavior and enable location specific #from/#import handling |
| 48 |
(hereafter referred to as "function imports") in the generated |
| 49 |
code, with this block of Cheetah for example:: |
| 50 |
|
| 51 |
#def aFunction(arg) |
| 52 |
#try |
| 53 |
#from hashlib import md5 |
| 54 |
#except ImportError |
| 55 |
#from md5 import md5 |
| 56 |
#end try |
| 57 |
#return $md5.new(arg).hexdigest() |
| 58 |
#end def |
| 59 |
|
| 60 |
Will result in code generated with everything in |
| 61 |
place such that the Python looks something like:: |
| 62 |
|
| 63 |
class Foo(Template): |
| 64 |
def aFunction(self, arg): |
| 65 |
try: |
| 66 |
from hashlib import md5 |
| 67 |
except ImportError: |
| 68 |
from md5 import md5 |
| 69 |
return md5.new(arg).hexdigest() |
| 70 |
|
| 71 |
|
| 72 |
These two approaches to handling the #from/#import directives |
| 73 |
are both beneficial for different situations but currently they |
| 74 |
are handled in mutually exclusive code paths and in mutually |
| 75 |
exclusive fashions. |
| 76 |
|
| 77 |
Specification |
| 78 |
------------- |
| 79 |
The change in #from/#import behavior and how these directives |
| 80 |
are handled is all based on context of their use, making the |
| 81 |
#from/#import handling "intelligent". For module imports, the |
| 82 |
"traditional" handling of the #from/#import directives will |
| 83 |
still apply. Whereas function imports will result in inline |
| 84 |
generated import code. |
| 85 |
|
| 86 |
The basic premise of the change proposed by this document is |
| 87 |
that all #from/#import directives contained within a #def/#end def |
| 88 |
closure will result in import statements contained within that |
| 89 |
function block whereas everywhere else the statements will be relocated |
| 90 |
to the top of the generated module code (i.e. the module import) |
| 91 |
|
| 92 |
|
| 93 |
Rationale |
| 94 |
--------- |
| 95 |
The concept of the "function import" was introduced in Cheetah v2.1.0 |
| 96 |
and quickly retrofitted to "live" behind a compiler setting due to the |
| 97 |
regressions with older templates or templates that were designed to utilize |
| 98 |
module imports (through heavy #block/#end block use, etc). Through discussion |
| 99 |
with Tavis Rudd, this middle ground between the two styles of importing was |
| 100 |
concluded to be the most reasonable solution to providing "pythonic" import |
| 101 |
functionality (i.e. "function import" also known as "inline imports") while |
| 102 |
still providing the ability to have #from/#import directives declared at the |
| 103 |
module scope within the template (within the Cheetah templates, markup and most |
| 104 |
directives declared within the module scope are placed inside the default method). |
| 105 |
|
| 106 |
|
| 107 |
Backwards Compatibility |
| 108 |
----------------------- |
| 109 |
Changes proposed in this document should be *mostly* backwards |
| 110 |
compatible with current versions of Cheetah, Any unforeseen issues |
| 111 |
could arise from the use of #from/#import inside of a function |
| 112 |
expecting those symbols to be available outside of the function |
| 113 |
that they're declared in. |
| 114 |
|
| 115 |
|
| 116 |
Reference Implementation |
| 117 |
------------------------ |
| 118 |
*still in development* |
| 119 |
|
| 120 |
Copyright |
| 121 |
--------- |
| 122 |
This document has been placed in the public domain. |