diff options
author | R. Tyler Ballance <tyler@monkeypox.org> | 2010-01-03 13:40:06 -0800 |
---|---|---|
committer | R. Tyler Ballance <tyler@monkeypox.org> | 2010-01-03 13:42:57 -0800 |
commit | 020059f74d1fe5b4d2147718dda001e1c1ec6323 (patch) | |
tree | d5b47e8706af22c040d69cd55cc01e4ca174b37e /cheetah | |
parent | dd56eea7375f24b782c136cfacca30d4022e9132 (diff) | |
download | python-cheetah-020059f74d1fe5b4d2147718dda001e1c1ec6323.tar.gz |
Import __builtin__ in generated template code in a compatible way with 2 and 3
Diffstat (limited to 'cheetah')
-rw-r--r-- | cheetah/Compiler.py | 7 | ||||
-rw-r--r-- | cheetah/Tests/NameMapper.py | 14 |
2 files changed, 17 insertions, 4 deletions
diff --git a/cheetah/Compiler.py b/cheetah/Compiler.py index 8946710..f4c7668 100644 --- a/cheetah/Compiler.py +++ b/cheetah/Compiler.py @@ -266,7 +266,7 @@ class GenUtils(object): + repr(defaultUseAC and useAC) + ')' + remainder) else: - pythonCode = ('VFSL([locals()]+SL+[globals(), __builtin__],' + pythonCode = ('VFSL([locals()]+SL+[globals(), builtin],' '"'+ name + '",' + repr(defaultUseAC and useAC) + ')' + remainder) @@ -1615,7 +1615,10 @@ class ModuleCompiler(SettingsManager, GenUtils): "import sys", "import os", "import os.path", - "import __builtin__", + 'try:', + ' import builtins as builtin', + 'except ImportError:', + ' import __builtin__ as builtin', "from os.path import getmtime, exists", "import time", "import types", diff --git a/cheetah/Tests/NameMapper.py b/cheetah/Tests/NameMapper.py index 8b47b24..ac42244 100644 --- a/cheetah/Tests/NameMapper.py +++ b/cheetah/Tests/NameMapper.py @@ -1,6 +1,5 @@ #!/usr/bin/env python - import sys import types import os @@ -11,7 +10,7 @@ from Cheetah.NameMapper import NotFound, valueForKey, \ valueForName, valueFromSearchList, valueFromFrame, valueFromFrameOrSearchList -class DummyClass: +class DummyClass(object): classVar1 = 123 def __init__(self): @@ -518,6 +517,17 @@ if sys.platform.startswith('java'): del VFF, VFFSL, VFFSL_2, VFFSL_3, VFFSL_4 +class MapBuiltins(unittest.TestCase): + def test_int(self): + from Cheetah.Template import Template + t = Template(''' + #def intify(val) + #return $int(val) + #end def''', compilerSettings={'useStackFrames' : False}) + self.assertEquals(5, t.intify('5')) + + + ################################################## ## if run from the command line ## |