summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Hellkamp <marc@gsites.de>2010-03-08 00:17:15 +0100
committerMarcel Hellkamp <marc@gsites.de>2010-03-08 00:17:15 +0100
commit369be6170b8725e201b1c85970e0994f926118a9 (patch)
tree3fa4cdb29840dca13af0b192b0371f110dae4aea
parent9a428ad87134b078c3e64181946cd62fd013bb8e (diff)
downloadbottle-369be6170b8725e201b1c85970e0994f926118a9.tar.gz
Fixed: Encoding detection (PEP263) in SimpleTemplate removed a linebreak.
Fixed: UnicodeError for non-native encodings in SimpleTemplate.
-rwxr-xr-xbottle.py6
-rw-r--r--test/test_stpl.py6
2 files changed, 8 insertions, 4 deletions
diff --git a/bottle.py b/bottle.py
index ea698df..cc4e2d4 100755
--- a/bottle.py
+++ b/bottle.py
@@ -1401,7 +1401,6 @@ class SimpleTemplate(BaseTemplate):
class PyStmt(object): # Python statement with filter function
def __init__(self, s, f='_str'): self.s, self.f = s, f
def __repr__(self): return '%s(%s)' % (self.f, self.s.strip())
- def __str__(self): return self.s
def prt(txt): # Add a string or a PyStmt object to ptrbuffer
if ptrbuffer and isinstance(txt, str) \
@@ -1419,7 +1418,8 @@ class SimpleTemplate(BaseTemplate):
out = []
for s in ptrbuffer:
out.append(repr(s))
- if '\n' in str(s): out.append('\n'*str(s).count('\n'))
+ if isinstance(s, PyStmt): s = s.s
+ if '\n' in s: out.append('\n'*s.count('\n'))
codeline = ', '.join(out)
if codeline.endswith('\n'): codeline = codeline[:-1] #Remove last newline
codeline = codeline.replace('\n, ','\n')
@@ -1437,7 +1437,7 @@ class SimpleTemplate(BaseTemplate):
if lineno <= 2 and 'coding' in line:
m = re.search(r"coding[:=]\s*([-\w\.]+)", line)
if m: self.encoding = m.group(1)
- if m: line = u'# encoding removed: ' + self.encoding
+ if m: line = u'# encoding removed: %s\n' % self.encoding
if line.strip().startswith('%') and not line.strip().startswith('%%'):
line = line.strip().lstrip('%') # Full line
cline = line.split('#')[0].strip() # Strip comments
diff --git a/test/test_stpl.py b/test/test_stpl.py
index 53051d5..40478c7 100644
--- a/test/test_stpl.py
+++ b/test/test_stpl.py
@@ -97,7 +97,11 @@ class TestSimpleTemplate(unittest.TestCase):
t = t.render(var=5)
self.assertEqual(u'6\r\n', ''.join(t))
-
+ def test_detect_encodung(self):
+ 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()))
if __name__ == '__main__':
unittest.main()