From 020059f74d1fe5b4d2147718dda001e1c1ec6323 Mon Sep 17 00:00:00 2001 From: "R. Tyler Ballance" Date: Sun, 3 Jan 2010 13:40:06 -0800 Subject: Import __builtin__ in generated template code in a compatible way with 2 and 3 --- cheetah/Compiler.py | 7 +++++-- cheetah/Tests/NameMapper.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'cheetah') 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 ## -- cgit v1.2.1