diff options
author | Marcel Hellkamp <marc@gsites.de> | 2010-03-08 00:47:08 +0100 |
---|---|---|
committer | Marcel Hellkamp <marc@gsites.de> | 2010-03-08 00:47:08 +0100 |
commit | 7355063b042dd45225a71e91220f91dbc7a9213d (patch) | |
tree | a7d83d74d076947eecfdf1ff6d291188e25a437c | |
parent | 369be6170b8725e201b1c85970e0994f926118a9 (diff) | |
download | bottle-7355063b042dd45225a71e91220f91dbc7a9213d.tar.gz |
Fixed: SimpleTemplate should ignore pep263 strings in text-lines
Fixed: SimpleTemplate mixed up comment-only code-lines
Added proper unit tests.
-rwxr-xr-x | bottle.py | 10 | ||||
-rw-r--r-- | test/test_stpl.py | 33 |
2 files changed, 33 insertions, 10 deletions
@@ -1435,15 +1435,15 @@ class SimpleTemplate(BaseTemplate): lineno += 1 line = unicode(line, encoding=self.encoding) if not isinstance(line, unicode) else line if lineno <= 2 and 'coding' in line: - m = re.search(r"coding[:=]\s*([-\w\.]+)", line) + m = re.search(r"%.*coding[:=]\s*([-\w\.]+)", line) if m: self.encoding = m.group(1) - if m: line = u'# encoding removed: %s\n' % self.encoding + if m: line = line.replace('coding','coding (removed)') if line.strip().startswith('%') and not line.strip().startswith('%%'): line = line.strip().lstrip('%') # Full line - cline = line.split('#')[0].strip() # Strip comments + cline = line.split('#')[0] + cline = cline.strip() cmd = line.split()[0] # Command word - if cline: - flush() ##encodig + flush() ##encodig if cmd in self.blocks: if cmd in self.dedent_blocks: cmd = stack.pop() #last block ended code(line) diff --git a/test/test_stpl.py b/test/test_stpl.py index 40478c7..6c4cd2d 100644 --- a/test/test_stpl.py +++ b/test/test_stpl.py @@ -97,12 +97,35 @@ class TestSimpleTemplate(unittest.TestCase): t = t.render(var=5) self.assertEqual(u'6\r\n', ''.join(t)) - def test_detect_encodung(self): + def test_commentonly(self): + """ Templates: Commentd should behave like code-lines (e.g. flush text-lines) """ + t = SimpleTemplate('...\n%#test\n...') + self.failIfEqual('#test', t.code.splitlines()[0]) + + def test_detect_pep263(self): + ''' PEP263 strings in code-lines change the template encoding on the fly ''' + t = SimpleTemplate(u'%#coding: iso8859_15\nöäü?@€'.encode('utf8')) + self.failIfEqual(u'öäü?@€', ''.join(t.render())) + self.assertEqual(t.encoding, 'iso8859_15') + t = SimpleTemplate(u'%#coding: iso8859_15\nöäü?@€'.encode('iso8859_15')) + self.assertEqual(u'öäü?@€', ''.join(t.render())) + self.assertEqual(t.encoding, 'iso8859_15') + self.assertEqual(2, len(t.code.splitlines())) + + def test_ignore_pep263_in_textline(self): + ''' PEP263 strings in text-lines have no effect ''' + self.assertRaises(UnicodeError, SimpleTemplate, u'#coding: iso8859_15\nöäü?@€'.encode('iso8859_15')) t = SimpleTemplate(u'#coding: iso8859_15\nöäü?@€'.encode('utf8')) - self.failIfEqual(u'# encoding removed: iso8859_15\nöäü?@€', ''.join(t.render())) - t = SimpleTemplate(u'#coding: iso8859_15\nöäü?@€'.encode('iso8859_15')) - self.assertEqual(u'# encoding removed: iso8859_15\nöäü?@€', ''.join(t.render())) - + self.assertEqual(u'#coding: iso8859_15\nöäü?@€', ''.join(t.render())) + self.assertEqual(t.encoding, 'utf8') + + def test_ignore_late_pep263(self): + ''' PEP263 strings must appear within the first two lines ''' + self.assertRaises(UnicodeError, SimpleTemplate, u'\n\n%#coding: iso8859_15\nöäü?@€'.encode('iso8859_15')) + t = SimpleTemplate(u'\n\n%#coding: iso8859_15\nöäü?@€'.encode('utf8')) + self.assertEqual(u'\n\nöäü?@€', ''.join(t.render())) + self.assertEqual(t.encoding, 'utf8') + if __name__ == '__main__': unittest.main() |