Permalink
Browse files

bugfix: Until Python 2.7/3.2, compile() does't like sources which don…

…'t end in a newline.

Thanks to Iuri de Silvio (for his research) and Benjamin Peterson (for his help in python-list)
  • Loading branch information...
1 parent fbe3543 commit 0df5e35ce755665563aae238fd062e5b95340622 @defnull defnull committed May 20, 2010
Showing with 8 additions and 2 deletions.
  1. +3 −2 bottle.py
  2. +5 −0 test/test_stpl.py
View
@@ -877,6 +877,7 @@ def flush(allow_nobreak=False):
code.append((' ' * indent + '\n') * len(strbuffer)) # to preserve line numbers
del strbuffer[:]
for line in template.splitlines(True):
+ lineend = '\n' if not line.endswith('\n') else ''
m = self.re_python.match(line)
if m:
flush(allow_nobreak=True)
@@ -905,9 +906,9 @@ def flush(allow_nobreak=False):
% (repr(name), args))
elif end:
indent -= 1
- code.append(' ' * indent + '#' + line[m.start(3):])
+ code.append(' ' * indent + '#' + line[m.start(3):] + lineend)
elif statement:
- code.append(' ' * indent + line[m.start(4):])
+ code.append(' ' * indent + line[m.start(4):] + lineend)
else:
splits = self.re_inline.split(line) # text, (expr, text)*
if len(splits) == 1:
View
@@ -38,6 +38,11 @@ def test_blocks(self):
self.assertEqual('start\nTrue \nend', ''.join(t.render(i=True)))
self.assertEqual('start\nend', ''.join(t.render(i=False)))
+ def test_eof_in_block(self):
+ """ Templates: compile() < 2.7/3.1 breaks on EOF in block """
+ t = SimpleTemplate("start\n%for i in l:\n{{i}} \n%end")
+ self.assertEqual('start\n1 \n2 \n3 \n', ''.join(t.render(l=[1,2,3])))
+
def test_nobreak(self):
""" Templates: Nobreak statements"""
t = SimpleTemplate("start\\\\\n%pass\nend")

0 comments on commit 0df5e35

Please sign in to comment.