Fix #1789 :pyobject: option of literalinclude directive includes following lines after definitions

This commit is contained in:
Takeshi KOMIYA
2015-03-29 10:31:26 +09:00
parent f6bcb6e414
commit 0215b1fb10
4 changed files with 24 additions and 6 deletions

View File

@@ -10,6 +10,9 @@ Features added
Bugs fixed Bugs fixed
---------- ----------
* #1789: ``:pyobject:`` option of ``literalinclude`` directive includes following
lines after class definitions
Documentation Documentation
------------- -------------

View File

@@ -10,6 +10,7 @@
""" """
from __future__ import print_function from __future__ import print_function
import re
import sys import sys
from os import path from os import path
@@ -45,6 +46,8 @@ number2name.update(token.tok_name)
_eq = nodes.Leaf(token.EQUAL, '=') _eq = nodes.Leaf(token.EQUAL, '=')
emptyline_re = re.compile('^\s*(#.*)?$')
class AttrDocVisitor(nodes.NodeVisitor): class AttrDocVisitor(nodes.NodeVisitor):
""" """
@@ -289,8 +292,9 @@ class ModuleAnalyzer(object):
indent = 0 indent = 0
defline = False defline = False
expect_indent = False expect_indent = False
emptylines = 0
def tokeniter(ignore = (token.COMMENT, token.NL)): def tokeniter(ignore = (token.COMMENT,)):
for tokentup in self.tokens: for tokentup in self.tokens:
if tokentup[0] not in ignore: if tokentup[0] not in ignore:
yield tokentup yield tokentup
@@ -303,7 +307,7 @@ class ModuleAnalyzer(object):
dtype, fullname, startline, _ = stack.pop() dtype, fullname, startline, _ = stack.pop()
endline = epos[0] endline = epos[0]
namespace.pop() namespace.pop()
result[fullname] = (dtype, startline, endline) result[fullname] = (dtype, startline, endline - emptylines)
expect_indent = False expect_indent = False
if tok in ('def', 'class'): if tok in ('def', 'class'):
name = next(tokeniter)[1] name = next(tokeniter)[1]
@@ -322,7 +326,7 @@ class ModuleAnalyzer(object):
dtype, fullname, startline, _ = stack.pop() dtype, fullname, startline, _ = stack.pop()
endline = spos[0] endline = spos[0]
namespace.pop() namespace.pop()
result[fullname] = (dtype, startline, endline) result[fullname] = (dtype, startline, endline - emptylines)
elif type == token.NEWLINE: elif type == token.NEWLINE:
# if this line contained a definition, expect an INDENT # if this line contained a definition, expect an INDENT
# to start the suite; if there is no such INDENT # to start the suite; if there is no such INDENT
@@ -330,6 +334,13 @@ class ModuleAnalyzer(object):
if defline: if defline:
defline = False defline = False
expect_indent = True expect_indent = True
emptylines = 0
elif type == token.NL:
# count up if line is empty or comment only
if emptyline_re.match(line):
emptylines += 1
else:
emptylines = 0
self.tags = result self.tags = result
return result return result

View File

@@ -10,4 +10,5 @@ class Bar:
def baz(): def baz():
pass pass
# comment after Bar class definition
def bar(): pass def bar(): pass

View File

@@ -116,7 +116,8 @@ def test_literal_include_linenos(app, status, warning):
'10\n' '10\n'
'11\n' '11\n'
'12\n' '12\n'
'13</pre></div></td>') '13\n'
'14</pre></div></td>')
assert linenos in html assert linenos in html
@@ -138,7 +139,8 @@ def test_literal_include_lineno_start(app, status, warning):
'209\n' '209\n'
'210\n' '210\n'
'211\n' '211\n'
'212</pre></div></td>') '212\n'
'213</pre></div></td>')
assert linenos in html assert linenos in html
@@ -168,7 +170,8 @@ def test_literal_include_lineno_match(app, status, warning):
'10\n' '10\n'
'11\n' '11\n'
'12\n' '12\n'
'13</pre></div></td>') '13\n'
'14</pre></div></td>')
assert start_after in html assert start_after in html