diff options
author | R. Tyler Ballance <tyler@slide.com> | 2009-04-16 21:46:32 -0700 |
---|---|---|
committer | R. Tyler Ballance <tyler@slide.com> | 2009-04-16 21:46:32 -0700 |
commit | 1801afa6eeaa1d81f9a82d8384923d79f260fb27 (patch) | |
tree | c2ce92b2625a485fa5f559c400990195ea12b35c | |
parent | fef8967cff00b79bf27af55b634ac0d90289cd0a (diff) | |
download | python-cheetah-1801afa6eeaa1d81f9a82d8384923d79f260fb27.tar.gz |
Add the "useLegacyImportMode" to support the older way to import in Cheetah
If you need to perform inline imports, or if you want to be compatible with future
versions of cheetah, compiling with "useLegacyImportMode=False" is probably a good idea.
Signed-off-by: R. Tyler Ballance <tyler@slide.com>
-rw-r--r-- | src/Compiler.py | 7 | ||||
-rw-r--r-- | src/Tests/Regressions.py | 10 | ||||
-rw-r--r-- | src/Tests/Template.py | 2 |
3 files changed, 11 insertions, 8 deletions
diff --git a/src/Compiler.py b/src/Compiler.py index a885d07..71eeeb9 100644 --- a/src/Compiler.py +++ b/src/Compiler.py @@ -63,6 +63,7 @@ DEFAULT_COMPILER_SETTINGS = { 'alwaysFilterNone':True, # filter out None, before the filter is called 'useFilters':True, # use str instead if =False 'includeRawExprInFilterArgs':True, + 'useLegacyImportMode' : True, #'lookForTransactionAttr':False, 'autoAssignDummyTransactionToSelf':False, @@ -1719,9 +1720,10 @@ class ModuleCompiler(SettingsManager, GenUtils): return self._importedVarNames def addImportedVarNames(self, varNames, raw_statement=None): + settings = self.settings() if not varNames: return - if self._methodBodyChunks and raw_statement: + if self._methodBodyChunks and raw_statement and not settings.get('useLegacyImportMode'): self.addChunk(raw_statement) else: self._importedVarNames.extend(varNames) @@ -1843,7 +1845,8 @@ class ModuleCompiler(SettingsManager, GenUtils): self._specialVars[name] = contents.strip() def addImportStatement(self, impStatement): - if not self._methodBodyChunks: + settings = self.settings() + if not self._methodBodyChunks or settings.get('useLegacyImportMode'): # In the case where we are importing inline in the middle of a source block # we don't want to inadvertantly import the module at the top of the file either self._importStatements.append(impStatement) diff --git a/src/Tests/Regressions.py b/src/Tests/Regressions.py index eed4bc0..80c1336 100644 --- a/src/Tests/Regressions.py +++ b/src/Tests/Regressions.py @@ -59,7 +59,7 @@ class InlineImportTest(unittest.TestCase): #end if #end def ''' - template = Cheetah.Template.Template.compile(template, compilerSettings={}, keepRefToGeneratedCode=True) + template = Cheetah.Template.Template.compile(template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True) template = template(searchList=[{}]) assert template, 'We should have a valid template object by now' @@ -77,7 +77,7 @@ class InlineImportTest(unittest.TestCase): $invalidmodule.FOO ''' - template = Cheetah.Template.Template.compile(template, compilerSettings={}, keepRefToGeneratedCode=True) + template = Cheetah.Template.Template.compile(template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True) template = template(searchList=[{}]) assert template, 'We should have a valid template object by now' @@ -89,7 +89,7 @@ class InlineImportTest(unittest.TestCase): This should totally $fail ''' - self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={}, keepRefToGeneratedCode=True) + self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True) def test_AutoImporting(self): template = ''' @@ -99,7 +99,7 @@ class InlineImportTest(unittest.TestCase): ''' self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template) - def test_GarbageBeforeImport(self): + def test_StuffBeforeImport_Legacy(self): template = ''' ### ### I like comments before import @@ -107,7 +107,7 @@ class InlineImportTest(unittest.TestCase): #extends Foo Bar ''' - self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template) + self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={'useLegacyImportMode' : True}, keepRefToGeneratedCode=True) diff --git a/src/Tests/Template.py b/src/Tests/Template.py index c8b6ee8..bca1b30 100644 --- a/src/Tests/Template.py +++ b/src/Tests/Template.py @@ -319,7 +319,7 @@ class TryExceptImportTest(TemplateTest): #end def ''' # This should raise an IndentationError (if the bug exists) - klass = Template.compile(source=source) + klass = Template.compile(source=source, compilerSettings={'useLegacyImportMode' : False}) t = klass(namespaces={'foo' : 1234}) |