From b7043badf84b67b6482e53df2eb1babe14602bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 24 Jun 2016 18:43:40 +0200 Subject: [PATCH] ezt: detect unmatched else tags (#11581) --- tests/test_ezt.py | 11 ++++++++++- wcs/qommon/ezt.py | 8 +++++++- wcs/qommon/form.py | 3 ++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/test_ezt.py b/tests/test_ezt.py index 83ef3c2..628acbc 100644 --- a/tests/test_ezt.py +++ b/tests/test_ezt.py @@ -1,6 +1,6 @@ import pytest from StringIO import StringIO -from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError +from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError, UnmatchedElseError def test_simple_qualifier(): template = Template() @@ -91,6 +91,15 @@ def test_unmatched_end(): except UnmatchedEndError as e: assert e.column == 15 and e.line == 0 +def test_unmatched_else(): + template = Template() + with pytest.raises(UnmatchedElseError): + template.parse('

[else]

') + try: + template.parse('

[else]

') + except UnmatchedElseError as e: + assert e.column == 3 and e.line == 0 + def test_array_index(): template = Template() template.parse('

[foo.0]

') diff --git a/wcs/qommon/ezt.py b/wcs/qommon/ezt.py index 9c260fb..e212b8c 100644 --- a/wcs/qommon/ezt.py +++ b/wcs/qommon/ezt.py @@ -377,7 +377,10 @@ class Template: if len(args) > 1: raise ArgCountSyntaxError(str(args[1:]), line, column) ### check: don't allow for 'for' cmd - idx = stack[-1][1] + try: + idx = stack[-1][1] + except IndexError: + raise UnmatchedElseError('', line, column) true_section = program[idx:] del program[idx:] stack[-1][3] = true_section @@ -751,6 +754,9 @@ class UnclosedBlocksError(EZTException): class UnmatchedEndError(EZTException): """This error may be caused by a misspelled if directive.""" +class UnmatchedElseError(EZTException): + """This error may be caused by a misspelled if directive.""" + class BaseUnavailableError(EZTException): """Base location is unavailable, which disables includes.""" diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 1fec3b2..1d5815d 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -2275,11 +2275,12 @@ class ComputedExpressionWidget(StringWidget): ezt.NeedSequenceError: _('sequence required'), ezt.UnclosedBlocksError: _('unclosed block'), ezt.UnmatchedEndError: _('unmatched [end]'), + ezt.UnmatchedElseError: _('unmatched [else]'), ezt.BaseUnavailableError: _('unavailable base location'), ezt.BadFormatConstantError: _('bad format constant'), ezt.UnknownFormatConstantError: _('unknown format constant'), }.get(e.__class__)) - if e.line: + if e.line is not None: parts.append(_('at line %(line)d and column %(column)d') % { 'line': e.line+1, 'column': e.column+1}) -- 2.8.1