8000 Backport changes to pyspecific.py · stackless-dev/stackless@cd469b7 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit cd469b7

Browse files
committed
Backport changes to pyspecific.py
1 parent a69d886 commit cd469b7

File tree

1 file changed

+66
-58
lines changed

1 file changed

+66
-58
lines changed

Doc/tools/sphinxext/pyspecific.py

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,32 @@
99
:license: Python license.
1010
"""
1111

12-
ISSUE_URI = 'http://bugs.python.org/issue%s'
13-
SOURCE_URI = 'http://hg.python.org/cpython/file/3.3/%s'
12+
import re
13+
import codecs
14+
from os import path
15+
from time import asctime
16+
from pprint import pformat
17+
from docutils.io import StringOutput
18+
from docutils.parsers.rst import Directive
19+
from docutils.utils import new_document
1420

1521
from docutils import nodes, utils
1622

17-
import sphinx
23+
from sphinx import addnodes
24+
from sphinx.builders import Builder
1825
from sphinx.util.nodes import split_explicit_title
19-
from sphinx.util.compat import Directive
2026
from sphinx.writers.html import HTMLTranslator
27+
from sphinx.writers.text import TextWriter
2128
from sphinx.writers.latex import LaTeXTranslator
22-
from sphinx.locale import versionlabels
29+
from sphinx.domains.python import PyModulelevel, PyClassmember
30+
31+
# Support for checking for suspicious markup
32+
33+
import suspicious
34+
35+
36+
ISSUE_URI = 'https://bugs.python.org/issue%s'
37+
SOURCE_URI = 'https://github.com/python/cpython/tree/3.3/%s'
2338

2439
# monkey-patch reST parser to disable alphabetic and roman enumerated lists
2540
from docutils.parsers.rst.states import Body
@@ -28,23 +43,12 @@
2843
Body.enum.converters['lowerroman'] = \
2944
Body.enum.converters['upperroman'] = lambda x: None
3045

31-
SPHINX11 = sphinx.__version__[:3] < '1.2'
32-
33-
if SPHINX11:
34-
# monkey-patch HTML translator to give versionmodified paragraphs a class
35-
def new_visit_versionmodified(self, node):
36-
self.body.append(self.starttag(node, 'p', CLASS=node['type']))
37-
text = versionlabels[node['type']] % node['version']
38-
if len(node):
39-
text += ':'
40-
else:
41-
text += '.'
42-
self.body.append('<span class="versionmodified">%s</span> ' % text)
43-
HTMLTranslator.visit_versionmodified = new_visit_versionmodified
44-
4546
# monkey-patch HTML and LaTeX translators to keep doctest blocks in the
4647
# doctest docs themselves
4748
orig_visit_literal_block = HTMLTranslator.visit_literal_block
49+
orig_depart_literal_block = LaTeXTranslator.depart_literal_block
50+
51+
4852
def new_visit_literal_block(self, node):
4953
meta = self.builder.env.metadata[self.builder.current_docname]
5054
old_trim_doctest_flags = self.highlighter.trim_doctest_flags
@@ -55,9 +59,7 @@ def new_visit_literal_block(self, node):
5559
finally:
5660
self.highlighter.trim_doctest_flags = old_trim_doctest_flags
5761

58-
HTMLTranslator.visit_literal_block = new_visit_literal_block
5962

60-
orig_depart_literal_block = LaTeXTranslator.depart_literal_block
6163
def new_depart_literal_block(self, node):
6264
meta = self.builder.env.metadata[self.curfilestack[-1]]
6365
old_trim_doctest_flags = self.highlighter.trim_doctest_flags
@@ -68,8 +70,11 @@ def new_depart_literal_block(self, node):
6870
finally:
6971
self.highlighter.trim_doctest_flags = old_trim_doctest_flags
7072

73+
74+
HTMLTranslator.visit_literal_block = new_visit_literal_block
7175
LaTeXTranslator.depart_literal_block = new_depart_literal_block
7276

77+
7378
# Support for marking up and linking to bugs.python.org issues
7479

7580
def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
@@ -117,9 +122,6 @@ def run(self):
117122

118123
# Support for documenting decorators
119124

120-
from sphinx import addnodes
121-
from sphinx.domains.python import PyModulelevel, PyClassmember
122-
123125
class PyDecoratorMixin(object):
124126
def handle_signature(self, sig, signode):
125127
ret = super(PyDecoratorMixin, self).handle_signature(sig, signode)
@@ -129,18 +131,39 @@ def handle_signature(self, sig, signode):
129131
def needs_arglist(self):
130132
return False
131133

134+
132135
class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel):
133136
def run(self):
134137
# a decorator function is a function after all
135138
self.name = 'py:function'
136139
return PyModulelevel.run(self)
137140

141+
138142
class PyDecoratorMethod(PyDecoratorMixin, PyClassmember):
139143
def run(self):
140144
self.name = 'py:method'
141145
return PyClassmember.run(self)
142146

143147

148+
class PyCoroutineMixin(object):
149+
def handle_signature(self, sig, signode):
150+
ret = super(PyCoroutineMixin, self).handle_signature(sig, signode)
151+
signode.insert(0, addnodes.desc_annotation('coroutine ', 'coroutine '))
152+
return ret
153+
154+
155+
class PyCoroutineFunction(PyCoroutineMixin, PyModulelevel):
156+
def run(self):
157+
self.name = 'py:function'
158+
return PyModulelevel.run(self)
159+
160+
161+
class PyCoroutineMethod(PyCoroutineMixin, PyClassmember):
162+
def run(self):
163+
self.name = 'py:method'
164+
return PyClassmember.run(self)
165+
166+
144167
# Support for documenting version of removal in deprecations
145168

146169
class DeprecatedRemoved(Directive):
@@ -168,35 +191,31 @@ def run(self):
168191
messages = []
169192
if self.content:
170193
self.state.nested_parse(self.content, self.content_offset, node)
194+
if len(node):
171195
if isinstance(node[0], nodes.paragraph) and node[0].rawsource:
172196
content = nodes.inline(node[0].rawsource, translatable=True)
173197
content.source = node[0].source
174198
content.line = node[0].line
175199
content += node[0].children
176200
node[0].replace_self(nodes.paragraph('', '', content))
177-
if not SPHINX11:
178-
node[0].insert(0, nodes.inline('', '%s: ' % text,
179-
classes=['versionmodified']))
180-
elif not SPHINX11:
201+
node[0].insert(0, nodes.inline('', '%s: ' % text,
202+
classes=['versionmodified']))
203+
else:
181204
para = nodes.paragraph('', '',
182-
nodes.inline('', '%s.' % text, classes=['versionmodified']))
205+
nodes.inline('', '%s.' % text,
206+
classes=['versionmodified']))
183207
node.append(para)
184208
env = self.state.document.settings.env
185209
env.note_versionchange('deprecated', version[0], node, self.lineno)
186210
return [node] + messages
187211

188-
# for Sphinx < 1.2
189-
versionlabels['deprecated-removed'] = DeprecatedRemoved._label
190-
191212

192213
# Support for including Misc/NEWS
193214

194-
import re
195-
import codecs
196-
197215
issue_re = re.compile('([Ii])ssue #([0-9]+)')
198216
whatsnew_re = re.compile(r"(?im)^what's new in (.*?)\??$")
199217

218+
200219
class MiscNews(Directive):
201220
has_content = False
202221
required_arguments = 1
@@ -221,7 +240,7 @@ def run(self):
221240
text = 'The NEWS file is not available.'
222241
node = nodes.strong(text, text)
223242
return [node]
224-
content = issue_re.sub(r'`\1ssue #\2 <http://bugs.python.org/\2>`__',
243+
content = issue_re.sub(r'`\1ssue #\2 <https://bugs.python.org/\2>`__',
225244
content)
226245
content = whatsnew_re.sub(r'\1', content)
227246
# remove first 3 lines as they are the main heading
@@ -250,15 +269,6 @@ def run(self):
250269
'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield'
251270
]
252271

253-
from os import path
254-
from time import asctime
255-
from pprint import pformat
256-
from docutils.io import StringOutput
257-
from docutils.utils import new_document
258-
259-
from sphinx.builders import Builder
260-
from sphinx.writers.text import TextWriter
261-
262272

263273
class PydocTopicsBuilder(Builder):
264274
name = 'pydoc-topics'
@@ -286,29 +296,23 @@ def write(self, *ignored):
286296
document.append(doctree.ids[labelid])
287297
destination = StringOutput(encoding='utf-8')
288298
writer.write(document, destination)
289-
self.topics[label] = writer.output.encode('utf-8')
299+
self.topics[label] = writer.output
290300

291301
def finish(self):
292-
f = open(path.join(self.outdir, 'topics.py'), 'w')
302+
f = open(path.join(self.outdir, 'topics.py'), 'wb')
293303
try:
294-
f.write('# -*- coding: utf-8 -*-\n')
295-
f.write('# Autogenerated by Sphinx on %s\n' % asctime())
296-
f.write('topics = ' + pformat(self.topics) + '\n')
304+
f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8'))
305+
f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8'))
306+
f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8'))
297307
finally:
298308
f.close()
299309

300310

301-
# Support for checking for suspicious markup
302-
303-
import suspicious
304-
305-
306311
# Support for documenting Opcodes
307312

308-
import re
309-
310313
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
311314

315+
312316
def parse_opcode_signature(env, sig, signode):
313317
"""Transform an opcode signature into RST nodes."""
314318
m = opcode_sig_re.match(sig)
@@ -328,12 +332,13 @@ def parse_opcode_signature(env, sig, signode):
328332
pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)')
329333

330334
# later...
331-
#pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers
335+
# pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers
332336
# [.,:]+ | # punctuation
333337
# [\[\]()] | # parens
334338
# \s+ # whitespace
335339
# ''', re.X)
336340

341+
337342
def parse_pdb_command(env, sig, signode):
338343
"""Transform a pdb command signature into RST nodes."""
339344
m = pdbcmd_sig_re.match(sig)
@@ -361,4 +366,7 @@ def setup(app):
361366
app.add_description_unit('2to3fixer', '2to3fixer', '%s (2to3 fixer)')
362367
app.add_directive_to_domain('py', 'decorator', PyDecoratorFunction)
363368
app.add_directive_to_domain('py', 'decoratormethod', PyDecoratorMethod)
369+
app.add_directive_to_domain('py', 'coroutinefunction', PyCoroutineFunction)
370+
app.add_directive_to_domain('py', 'coroutinemethod', PyCoroutineMethod)
364371
app.add_directive('miscnews', MiscNews)
372+
return {'version': '1.0', 'parallel_read_safe': True}

0 commit comments

Comments
 (0)
0