Skip to content

Commit 14b5a4d

Browse files
committed
Closes #22537: Make sphinx extensions compatible with Python 2 or 3, like in the 3.x branches
1 parent 2f33456 commit 14b5a4d

File tree

4 files changed

+47
-74
lines changed

4 files changed

+47
-74
lines changed

Doc/conf.py

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
# unit titles (such as .. function::).
6060
add_module_names = True
6161

62+
# Require Sphinx 1.2 for build.
63+
needs_sphinx = '1.2'
64+
6265

6366
# Options for HTML output
6467
# -----------------------

Doc/tools/patchlevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ def get_version_info():
6868
return version, release
6969

7070
if __name__ == '__main__':
71-
print get_header_version_info('.')[1]
71+
print(get_header_version_info('.')[1])

Doc/tools/pyspecific.py

+7-57
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
Sphinx extension with Python doc-specific markup.
77
8-
:copyright: 2008-2013 by Georg Brandl.
8+
:copyright: 2008-2014 by Georg Brandl.
99
:license: Python license.
1010
"""
1111

@@ -14,11 +14,10 @@
1414

1515
from docutils import nodes, utils
1616

17-
import sphinx
1817
from sphinx.util.nodes import split_explicit_title
18+
from sphinx.util.compat import Directive
1919
from sphinx.writers.html import HTMLTranslator
2020
from sphinx.writers.latex import LaTeXTranslator
21-
from sphinx.locale import versionlabels
2221

2322
# monkey-patch reST parser to disable alphabetic and roman enumerated lists
2423
from docutils.parsers.rst.states import Body
@@ -27,18 +26,6 @@
2726
Body.enum.converters['lowerroman'] = \
2827
Body.enum.converters['upperroman'] = lambda x: None
2928

30-
if sphinx.__version__[:3] < '1.2':
31-
# monkey-patch HTML translator to give versionmodified paragraphs a class
32-
def new_visit_versionmodified(self, node):
33-
self.body.append(self.starttag(node, 'p', CLASS=node['type']))
34-
text = versionlabels[node['type']] % node['version']
35-
if len(node):
36-
text += ': '
37-
else:
38-
text += '.'
39-
self.body.append('<span class="versionmodified">%s</span>' % text)
40-
HTMLTranslator.visit_versionmodified = new_visit_versionmodified
41-
4229
# monkey-patch HTML and LaTeX translators to keep doctest blocks in the
4330
# doctest docs themselves
4431
orig_visit_literal_block = HTMLTranslator.visit_literal_block
@@ -88,8 +75,6 @@ def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
8875

8976
# Support for marking up implementation details
9077

91-
from sphinx.util.compat import Directive
92-
9378
class ImplementationDetail(Directive):
9479

9580
has_content = True
@@ -140,41 +125,6 @@ def run(self):
140125
return PyClassmember.run(self)
141126

142127

143-
# Support for documenting version of removal in deprecations
144-
145-
from sphinx.locale import versionlabels
146-
from sphinx.util.compat import Directive
147-
148-
versionlabels['deprecated-removed'] = \
149-
'Deprecated since version %s, will be removed in version %s'
150-
151-
class DeprecatedRemoved(Directive):
152-
has_content = True
153-
required_arguments = 2
154-
optional_arguments = 1
155-
final_argument_whitespace = True
156-
option_spec = {}
157-
158-
def run(self):
159-
node = addnodes.versionmodified()
160-
node.document = self.state.document
161-
node['type'] = 'deprecated-removed'
162-
version = (self.arguments[0], self.arguments[1])
163-
node['version'] = version
164-
if len(self.arguments) == 3:
165-
inodes, messages = self.state.inline_text(self.arguments[2],
166-
self.lineno+1)
167-
node.extend(inodes)
168-
if self.content:
169-
self.state.nested_parse(self.content, self.content_offset, node)
170-
ret = [node] + messages
171-
else:
172-
ret = [node]
173-
env = self.state.document.settings.env
174-
env.note_versionchange('deprecated', version[0], node, self.lineno)
175-
return ret
176-
177-
178128
# Support for building "topic help" for pydoc
179129

180130
pydoc_topic_labels = [
@@ -231,13 +181,14 @@ def write(self, *ignored):
231181
document.append(doctree.ids[labelid])
232182
destination = StringOutput(encoding='utf-8')
233183
writer.write(document, destination)
234-
self.topics[label] = str(writer.output)
184+
self.topics[label] = writer.output
235185

236186
def finish(self):
237-
f = open(path.join(self.outdir, 'topics.py'), 'w')
187+
f = open(path.join(self.outdir, 'topics.py'), 'wb')
238188
try:
239-
f.write('# Autogenerated by Sphinx on %s\n' % asctime())
240-
f.write('topics = ' + pformat(self.topics) + '\n')
189+
f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8'))
190+
f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8'))
191+
f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8'))
241192
finally:
242193
f.close()
243194

@@ -295,7 +246,6 @@ def setup(app):
295246
app.add_role('issue', issue_role)
296247
app.add_role('source', source_role)
297248
app.add_directive('impl-detail', ImplementationDetail)
298-
app.add_directive('deprecated-removed', DeprecatedRemoved)
299249
app.add_builder(PydocTopicsBuilder)
300250
app.add_builder(suspicious.CheckSuspiciousMarkupBuilder)
301251
app.add_description_unit('opcode', 'opcode', '%s (opcode)',

Doc/tools/suspicious.py

+36-16
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@
4949
from docutils import nodes
5050
from sphinx.builders import Builder
5151

52-
detect_all = re.compile(ur'''
52+
detect_all = re.compile(r'''
5353
::(?=[^=])| # two :: (but NOT ::=)
5454
:[a-zA-Z][a-zA-Z0-9]+| # :foo
5555
`| # ` (seldom used by itself)
5656
(?<!\.)\.\.[ \t]*\w+: # .. foo: (but NOT ... else:)
5757
''', re.UNICODE | re.VERBOSE).finditer
5858

59+
py3 = sys.version_info >= (3, 0)
60+
5961

6062
class Rule:
6163
def __init__(self, docname, lineno, issue, line):
@@ -147,21 +149,31 @@ def report_issue(self, text, lineno, issue):
147149
if not self.any_issue: self.info()
148150
self.any_issue = True
149151
self.write_log_entry(lineno, issue, text)
150-
self.warn('[%s:%d] "%s" found in "%-.120s"' % (
152+
if py3:
153+
self.warn('[%s:%d] "%s" found in "%-.120s"' %
154+
(self.docname, lineno, issue, text))
155+
else:
156+
self.warn('[%s:%d] "%s" found in "%-.120s"' % (
151157
self.docname.encode(sys.getdefaultencoding(),'replace'),
152158
lineno,
153159
issue.encode(sys.getdefaultencoding(),'replace'),
154160
text.strip().encode(sys.getdefaultencoding(),'replace')))
155161
self.app.statuscode = 1
156162

157163
def write_log_entry(self, lineno, issue, text):
158-
f = open(self.log_file_name, 'ab')
159-
writer = csv.writer(f, dialect)
160-
writer.writerow([self.docname.encode('utf-8'),
161-
lineno,
162-
issue.encode('utf-8'),
163-
text.strip().encode('utf-8')])
164-
f.close()
164+
if py3:
165+
f = open(self.log_file_name, 'a')
166+
writer = csv.writer(f, dialect)
167+
writer.writerow([self.docname, lineno, issue, text.strip()])
168+
f.close()
169+
else:
170+
f = open(self.log_file_name, 'ab')
171+
writer = csv.writer(f, dialect)
172+
writer.writerow([self.docname.encode('utf-8'),
173+
lineno,
174+
issue.encode('utf-8'),
175+
text.strip().encode('utf-8')])
176+
f.close()
165177

166178
def load_rules(self, filename):
167179
"""Load database of previously ignored issues.
@@ -171,18 +183,26 @@ def load_rules(self, filename):
171183
"""
172184
self.info("loading ignore rules... ", nonl=1)
173185
self.rules = rules = []
174-
try: f = open(filename, 'rb')
175-
except IOError: return
186+
try:
187+
if py3:
188+
f = open(filename, 'r')
189+
else:
190+
f = open(filename, 'rb')
191+
except IOError:
192+
return
176193
for i, row in enumerate(csv.reader(f)):
177194
if len(row) != 4:
178195
raise ValueError(
179196
"wrong format in %s, line %d: %s" % (filename, i+1, row))
180197
docname, lineno, issue, text = row
181-
docname = docname.decode('utf-8')
182-
if lineno: lineno = int(lineno)
183-
else: lineno = None
184-
issue = issue.decode('utf-8')
185-
text = text.decode('utf-8')
198+
if lineno:
199+
lineno = int(lineno)
200+
else:
201+
lineno = None
202+
if not py3:
203+
docname = docname.decode('utf-8')
204+
issue = issue.decode('utf-8')
205+
text = text.decode('utf-8')
186206
rule = Rule(docname, lineno, issue, text)
187207
rules.append(rule)
188208
f.close()

0 commit comments

Comments
 (0)