parselinenos() raises out of range error

This commit is contained in:
Takeshi KOMIYA 2017-02-12 23:55:08 +09:00
parent 6b20c72521
commit f1d4248eea
3 changed files with 7 additions and 6 deletions

View File

@ -266,11 +266,7 @@ class LiteralIncludeReader(object):
raise ValueError(_('Cannot use "lineno-match" with a disjoint '
'set of "lines"'))
# just ignore non-existing lines
lines = [lines[n] for n in linelist if n < len(lines)]
if not lines:
raise ValueError(_('Line spec %r: no lines pulled from include file %r') %
(linespec, self.filename))
lines = [lines[n] for n in linelist]
return lines

View File

@ -394,7 +394,7 @@ def parselinenos(spec, total):
elif len(begend) == 2:
start, end = begend
start = int(start or 1) # left half open (cf. -10)
end = int(end or total) # right half open (cf. 10-)
end = int(end or max(start, total)) # right half open (cf. 10-)
if start > end: # invalid range (cf. 10-1)
raise ValueError
items.extend(range(start - 1, end))
@ -402,6 +402,9 @@ def parselinenos(spec, total):
raise ValueError
except Exception:
raise ValueError('invalid line number spec: %r' % spec)
if any(i >= total for i in items):
raise ValueError('line number spec is out of range(0-%d): %r' % (total, spec))
return items

View File

@ -114,3 +114,5 @@ def test_parselinenos():
parselinenos('-', 10)
with pytest.raises(ValueError):
parselinenos('3-1', 10)
with pytest.raises(ValueError):
parselinenos('11-', 10)