Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 562b7cb

Browse files
committed
fix parsing reST with code or code-block directives (closes #23063)
Patch by Marc Abramowitz.
1 parent 29ad011 commit 562b7cb

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

Lib/distutils/command/check.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _check_rst_data(self, data):
122122
"""Returns warnings when the provided data doesn't compile."""
123123
source_path = StringIO()
124124
parser = Parser()
125-
settings = frontend.OptionParser().get_default_values()
125+
settings = frontend.OptionParser(components=(Parser,)).get_default_values()
126126
settings.tab_width = 4
127127
settings.pep_references = None
128128
settings.rfc_references = None
@@ -138,8 +138,8 @@ def _check_rst_data(self, data):
138138
document.note_source(source_path, -1)
139139
try:
140140
parser.parse(data, document)
141-
except AttributeError:
142-
reporter.messages.append((-1, 'Could not finish the parsing.',
143-
'', {}))
141+
except AttributeError as e:
142+
reporter.messages.append(
143+
(-1, 'Could not finish the parsing: %s.' % e, '', {}))
144144

145145
return reporter.messages

Lib/distutils/tests/test_check.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for distutils.command.check."""
2+
import textwrap
23
import unittest
34
from test.support import run_unittest
45

@@ -92,6 +93,36 @@ def test_check_restructuredtext(self):
9293
cmd = self._run(metadata, strict=1, restructuredtext=1)
9394
self.assertEqual(cmd._warnings, 0)
9495

96+
@unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
97+
def test_check_restructuredtext_with_syntax_highlight(self):
98+
# Don't fail if there is a `code` or `code-block` directive
99+
100+
example_rst_docs = []
101+
example_rst_docs.append(textwrap.dedent("""\
102+
Here's some code:
103+
104+
.. code:: python
105+
106+
def foo():
107+
pass
108+
"""))
109+
example_rst_docs.append(textwrap.dedent("""\
110+
Here's some code:
111+
112+
.. code-block:: python
113+
114+
def foo():
115+
pass
116+
"""))
117+
118+
for rest_with_code in example_rst_docs:
119+
pkg_info, dist = self.create_dist(long_description=rest_with_code)
120+
cmd = check(dist)
121+
cmd.check_restructuredtext()
122+
self.assertEqual(cmd._warnings, 0)
123+
msgs = cmd._check_rst_data(rest_with_code)
124+
self.assertEqual(len(msgs), 0)
125+
95126
def test_check_all(self):
96127

97128
metadata = {'url': 'xxx', 'author': 'xxx'}

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ Core and Builtins
4444
Library
4545
-------
4646

47+
- Issue #23063: In the disutils' check command, fix parsing of reST with code or
48+
code-block directives.
49+
4750
- Issue #23209, #23225: selectors.BaseSelector.close() now clears its internal
4851
reference to the selector mapping to break a reference cycle. Initial patch
4952
written by Martin Richard.

0 commit comments

Comments
 (0)