Import __builtin__ in generated template code in a compatible way with 2 and 3

This commit is contained in:
R. Tyler Ballance 2010-01-03 13:40:06 -08:00
parent dd56eea737
commit 020059f74d
2 changed files with 17 additions and 4 deletions

View File

@ -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",

View File

@ -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 ##