Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix a minor issue with inline import statements

This will hopefully be the last issue related to these changes, added two more unit tests to cover these cases
and ran the whole suite successfully.

This may not have actually been the issue reported by mobiledreamers@gmail.com, but investigating his issue led
to these changes

Signed-off-by: R. Tyler Ballance <tyler@slide.com>
  • Loading branch information...
commit a87db1b880f7bb14d775e60261f7b228f518407a 1 parent 276916c
R. Tyler Ballance authored
Showing with 35 additions and 9 deletions.
  1. +4 −1 src/Compiler.py
  2. +31 −8 src/Tests/Regressions.py
View
5 src/Compiler.py
@@ -1843,7 +1843,10 @@ def addSpecialVar(self, basename, contents, includeUnderscores=True):
self._specialVars[name] = contents.strip()
def addImportStatement(self, impStatement):
- self._importStatements.append(impStatement)
+ if not self._methodBodyChunks:
+ # 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)
#@@TR 2005-01-01: there's almost certainly a cleaner way to do this!
importVarNames = impStatement[impStatement.find('import') + len('import'):].split(',')
View
39 src/Tests/Regressions.py
@@ -41,14 +41,14 @@ def test_NotFoundException(self):
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):
+class InlineImportTest(unittest.TestCase):
+ def test_FromFooImportThing(self):
+ '''
+ Verify that a bug introduced in v2.1.0 where an inline:
+ #from module import class
+ would result in the following code being generated:
+ import class
+ '''
template = '''
#def myfunction()
#if True
@@ -66,6 +66,29 @@ def runTest(self):
rc = template.myfunction()
assert rc == 17, (template, 'Didn\'t get a proper return value')
+ def test_ImportFailModule(self):
+ template = '''
+ #try
+ #import invalidmodule
+ #except
+ #set invalidmodule = dict(FOO='BAR!')
+ #end try
+
+ $invalidmodule.FOO
+ '''
+ template = Cheetah.Template.Template.compile(template, compilerSettings={}, keepRefToGeneratedCode=True)
+ template = template(searchList=[{}])
+
+ assert template, 'We should have a valid template object by now'
+ assert str(template), 'We weren\'t able to properly generate the result from the template'
+
+ def test_ProperImportOfBadModule(self):
+ template = '''
+ #from invalid import fail
+
+ This should totally $fail
+ '''
+ self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={}, keepRefToGeneratedCode=True)
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.