diff options
author | R. Tyler Ballance <tyler@slide.com> | 2009-03-27 15:32:00 -0700 |
---|---|---|
committer | R. Tyler Ballance <tyler@slide.com> | 2009-03-27 15:32:00 -0700 |
commit | 2ae79eb1baabb372bf8aa712a4ab2140c07209bf (patch) | |
tree | 10abba8b72124698db624c93b8d3baa8f9befb33 | |
parent | b08f08ed19194e75953b75fc183d150851264368 (diff) | |
download | python-cheetah-2ae79eb1baabb372bf8aa712a4ab2140c07209bf.tar.gz |
Correct a bug introduced in v2.1.0 that affected odd inline imports
Added a regression test as well to prevent it from happening again, what basically
was going on is that in v2.1.0 I introduced code to properly place inline imports, for
syntax like the following:
#try
#import cjson
#except ImportError
#import simplejson
#end try
The actual bug was with blocks of code like this:
#def function
#if $something
#from package import module
$module.do_thing()
#end if
#end def
Which would result in:
def function(self):
if something:
import module
module.do_thing()
Instead of the proper "from package import module" syntax.
Sorted, all tests passing now.
Signed-off-by: R. Tyler Ballance <tyler@slide.com>
-rw-r--r-- | src/Compiler.py | 10 | ||||
-rw-r--r-- | src/Tests/Regressions.py | 25 |
2 files changed, 30 insertions, 5 deletions
diff --git a/src/Compiler.py b/src/Compiler.py index 1d00ff6..8bcb552 100644 --- a/src/Compiler.py +++ b/src/Compiler.py @@ -1711,11 +1711,11 @@ class ModuleCompiler(SettingsManager, GenUtils): def importedVarNames(self): return self._importedVarNames - def addImportedVarNames(self, varNames): + def addImportedVarNames(self, varNames, raw_statement=None): if not varNames: return - if self._methodBodyChunks: - self.addChunk('import %s' % ', '.join(varNames)) + if self._methodBodyChunks and raw_statement: + self.addChunk(raw_statement) else: self._importedVarNames.extend(varNames) @@ -1841,8 +1841,8 @@ class ModuleCompiler(SettingsManager, GenUtils): #@@TR 2005-01-01: there's almost certainly a cleaner way to do this! importVarNames = impStatement[impStatement.find('import') + len('import'):].split(',') importVarNames = [var.split()[-1] for var in importVarNames] # handles aliases - importVarNames = [var for var in importVarNames if var!='*'] - self.addImportedVarNames(importVarNames) #used by #extend for auto-imports + importVarNames = [var for var in importVarNames if not var == '*'] + self.addImportedVarNames(importVarNames, raw_statement=impStatement) #used by #extend for auto-imports def addAttribute(self, attribName, expr): self._getActiveClassCompiler().addAttribute(attribName + ' =' + expr) diff --git a/src/Tests/Regressions.py b/src/Tests/Regressions.py index 96f740b..fb8327f 100644 --- a/src/Tests/Regressions.py +++ b/src/Tests/Regressions.py @@ -41,6 +41,31 @@ class GetAttrTest(unittest.TestCase): self.failUnlessRaises(GetAttrException, template.raiseme) +class InlineFromImportTest(unittest.TestCase): + ''' + Verify that a bug introduced in v2.1.0 where an inline: + #from module import class + would result in the following code being generated: + improt class + ''' + def runTest(self): + template = ''' + #def myfunction() + #if True + #from os import path + #return 17 + Hello! + #end if + #end def + ''' + template = Cheetah.Template.Template.compile(template, compilerSettings={}, keepRefToGeneratedCode=True) + template = template(searchList=[{}]) + + assert template, 'We should have a valid template object by now' + + rc = template.myfunction() + assert rc == 17, (template, 'Didn\'t get a proper return value') + if __name__ == '__main__': unittest.main() |