diff --git a/CHANGES b/CHANGES index 1cde57abd..f0e7e1b2d 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,8 @@ Bugs fixed that contains regular-expression like string * #1565: Show warning if Pygments throws an ErrorToken * #2211: Fix paragraphs in table cell doesn't work in Latex output +* #2253: ``:pyobject:`` option of ``literalinclude`` directive can't detect indented + body block when the block starts with blank or comment lines. Release 1.3.4 (released Jan 12, 2016) ===================================== diff --git a/sphinx/pycode/__init__.py b/sphinx/pycode/__init__.py index 8e8ecf706..3da887d6c 100644 --- a/sphinx/pycode/__init__.py +++ b/sphinx/pycode/__init__.py @@ -300,7 +300,7 @@ class ModuleAnalyzer(object): yield tokentup tokeniter = tokeniter() for type, tok, spos, epos, line in tokeniter: - if expect_indent: + if expect_indent and type != token.NL: if type != token.INDENT: # no suite -- one-line definition assert stack diff --git a/tests/roots/test-directive-code/python.rst b/tests/roots/test-directive-code/python.rst new file mode 100644 index 000000000..794c190f1 --- /dev/null +++ b/tests/roots/test-directive-code/python.rst @@ -0,0 +1,13 @@ +=========================== +Literal Includes for python +=========================== + +block start with blank or comment +================================= + +.. literalinclude:: target.py + :pyobject: block_start_with_comment + +.. literalinclude:: target.py + :pyobject: block_start_with_blank + diff --git a/tests/roots/test-directive-code/target.py b/tests/roots/test-directive-code/target.py new file mode 100644 index 000000000..61ad717ec --- /dev/null +++ b/tests/roots/test-directive-code/target.py @@ -0,0 +1,22 @@ +# Literally included file using Python highlighting +# -*- coding: utf-8 -*- + +foo = "Including Unicode characters: üöä" + +class Foo: + pass + +class Bar: + def baz(): + pass + +# comment after Bar class definition +def bar(): pass + +def block_start_with_comment(): + # Comment + return 1 + +def block_start_with_blank(): + + return 1 diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 1a488b0c7..8f3fb3161 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -98,6 +98,30 @@ def test_literal_include_dedent(app, status, warning): assert blocks[5].text == '\n\n' # dedent: 1000 +@with_app('xml', testroot='directive-code') +def test_literal_include_block_start_with_comment_or_brank(app, status, warning): + app.builder.build(['python']) + et = ElementTree.parse(app.outdir / 'python.xml') + secs = et.findall('./section/section') + literal_include = secs[0].findall('literal_block') + assert len(literal_include) > 0 + actual = literal_include[0].text + expect = ( + 'def block_start_with_comment():\n' + ' # Comment\n' + ' return 1\n' + ) + assert actual == expect + + actual = literal_include[1].text + expect = ( + 'def block_start_with_blank():\n' + '\n' + ' return 1\n' + ) + assert actual == expect + + @with_app('html', testroot='directive-code') def test_literal_include_linenos(app, status, warning): app.builder.build(['linenos'])