From f1d4248eea5a481d2cda10f524924f4442e0ca54 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 12 Feb 2017 23:55:08 +0900 Subject: [PATCH] parselinenos() raises out of range error --- sphinx/directives/code.py | 6 +----- sphinx/util/__init__.py | 5 ++++- tests/test_util.py | 2 ++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 1b07ade0a..5b375a590 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -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 diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index a4ebaee68..586d2d244 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -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 diff --git a/tests/test_util.py b/tests/test_util.py index 43434c80b..6f8ba7da1 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -114,3 +114,5 @@ def test_parselinenos(): parselinenos('-', 10) with pytest.raises(ValueError): parselinenos('3-1', 10) + with pytest.raises(ValueError): + parselinenos('11-', 10)