summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Hellkamp <marc@gsites.de>2014-03-06 16:33:51 +0100
committerMarcel Hellkamp <marc@gsites.de>2014-03-06 16:33:51 +0100
commit4d06a7bbb2cb877274fea3d462513884207f162a (patch)
tree6611af78b5a80d70203de0d924e3f8b52560c670
parentd7780fd5d5c2baeb0152864138adbbdfcf83e6e4 (diff)
downloadbottle-4d06a7bbb2cb877274fea3d462513884207f162a.tar.gz
fix #595: If expressions broken in STPL after upgrading
Fixed an error where everything before a block keyword (if/for/...) was removed by the template parser. Example:: % m = 'a' if True else 'b' was translated to:: if True else 'b' insead of:: m = 'a' if True else 'b'
-rw-r--r--bottle.py3
-rwxr-xr-xtest/test_stpl.py6
2 files changed, 9 insertions, 0 deletions
diff --git a/bottle.py b/bottle.py
index a6f0c06..77f6f83 100644
--- a/bottle.py
+++ b/bottle.py
@@ -3486,6 +3486,9 @@ class StplParser(object):
code_line += self.source[self.offset:self.offset+m.start()]
self.offset += m.end()
_str, _com, _blk1, _blk2, _end, _cend, _nl = m.groups()
+ if code_line and (_blk1 or _blk2): # a if b else c
+ code_line += _blk1 or _blk2
+ continue
if _str: # Python string
code_line += _str
elif _com: # Python comment (up to EOL)
diff --git a/test/test_stpl.py b/test/test_stpl.py
index 982d1b9..4c38f07 100755
--- a/test/test_stpl.py
+++ b/test/test_stpl.py
@@ -250,6 +250,12 @@ class TestSimpleTemplate(unittest.TestCase):
def test_bug_no_whitespace_before_stmt(self):
self.assertRenders('\n{{var}}', '\nx', var='x')
+ def test_bug_block_keywords_eat_prefixed_code(self):
+ ''' #595: Everything before an 'if' statement is removed, resulting in
+ SyntaxError. '''
+ tpl = "% m = 'x' if True else 'y'\n{{m}}"
+ self.assertRenders(tpl, 'x')
+
class TestSTPLDir(unittest.TestCase):
def fix_ident(self, string):