Project

General

Profile

0001-ezt-detect-unmatched-else-tags-11581.patch

Frédéric Péters, 24 June 2016 06:45 PM

Download (3.16 KB)

View differences:

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(-)
tests/test_ezt.py
1 1
import pytest
2 2
from StringIO import StringIO
3
from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError
3
from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError, UnmatchedElseError
4 4

  
5 5
def test_simple_qualifier():
6 6
    template = Template()
......
91 91
    except UnmatchedEndError as e:
92 92
        assert e.column == 15 and e.line == 0
93 93

  
94
def test_unmatched_else():
95
    template = Template()
96
    with pytest.raises(UnmatchedElseError):
97
        template.parse('<p>[else]</p>')
98
    try:
99
        template.parse('<p>[else]</p>')
100
    except UnmatchedElseError as e:
101
        assert e.column == 3 and e.line == 0
102

  
94 103
def test_array_index():
95 104
    template = Template()
96 105
    template.parse('<p>[foo.0]</p>')
wcs/qommon/ezt.py
377 377
          if len(args) > 1:
378 378
            raise ArgCountSyntaxError(str(args[1:]), line, column)
379 379
          ### check: don't allow for 'for' cmd
380
          idx = stack[-1][1]
380
          try:
381
            idx = stack[-1][1]
382
          except IndexError:
383
            raise UnmatchedElseError('', line, column)
381 384
          true_section = program[idx:]
382 385
          del program[idx:]
383 386
          stack[-1][3] = true_section
......
751 754
class UnmatchedEndError(EZTException):
752 755
  """This error may be caused by a misspelled if directive."""
753 756

  
757
class UnmatchedElseError(EZTException):
758
  """This error may be caused by a misspelled if directive."""
759

  
754 760
class BaseUnavailableError(EZTException):
755 761
  """Base location is unavailable, which disables includes."""
756 762

  
wcs/qommon/form.py
2275 2275
                ezt.NeedSequenceError: _('sequence required'),
2276 2276
                ezt.UnclosedBlocksError: _('unclosed block'),
2277 2277
                ezt.UnmatchedEndError: _('unmatched [end]'),
2278
                ezt.UnmatchedElseError: _('unmatched [else]'),
2278 2279
                ezt.BaseUnavailableError: _('unavailable base location'),
2279 2280
                ezt.BadFormatConstantError: _('bad format constant'),
2280 2281
                ezt.UnknownFormatConstantError: _('unknown format constant'),
2281 2282
                }.get(e.__class__))
2282
            if e.line:
2283
            if e.line is not None:
2283 2284
                parts.append(_('at line %(line)d and column %(column)d') % {
2284 2285
                    'line': e.line+1,
2285 2286
                    'column': e.column+1})
2286
-