2008-04-10 03:06:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
pyspecific.py
|
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Sphinx extension with Python doc-specific markup.
|
|
|
|
|
|
2014-01-21 19:20:31 +01:00
|
|
|
:copyright: 2008-2014 by Georg Brandl.
|
2008-04-10 03:06:53 +00:00
|
|
|
:license: Python license.
|
|
|
|
|
"""
|
|
|
|
|
|
2014-10-30 22:55:13 +01:00
|
|
|
import re
|
2017-06-16 08:59:01 +02:00
|
|
|
import io
|
2018-12-19 18:20:06 -08:00
|
|
|
from os import getenv, path
|
2023-06-23 08:58:45 +01:00
|
|
|
|
2024-07-19 08:33:51 +01:00
|
|
|
from docutils import nodes
|
2024-07-20 20:46:41 +01:00
|
|
|
from docutils.parsers.rst import directives
|
2025-03-19 18:35:11 +00:00
|
|
|
from docutils.utils import unescape
|
2014-10-30 22:55:13 +01:00
|
|
|
from sphinx import addnodes
|
2024-07-20 20:46:41 +01:00
|
|
|
from sphinx.domains.python import PyFunction, PyMethod, PyModule
|
2022-11-11 09:18:11 +08:00
|
|
|
from sphinx.locale import _ as sphinx_gettext
|
2023-02-12 15:20:11 +01:00
|
|
|
from sphinx.util.docutils import SphinxDirective
|
2014-10-30 22:55:13 +01:00
|
|
|
|
2024-04-15 21:22:00 +03:00
|
|
|
# Used in conf.py and updated here by python/release-tools/run_release.py
|
2024-05-11 13:47:45 +02:00
|
|
|
SOURCE_URI = 'https://github.com/python/cpython/tree/main/%s'
|
2008-04-10 03:06:53 +00:00
|
|
|
|
Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69129 | benjamin.peterson | 2009-01-30 19:42:55 -0600 (Fri, 30 Jan 2009) | 1 line
check the errno in bad fd cases
........
r69130 | andrew.kuchling | 2009-01-30 20:50:09 -0600 (Fri, 30 Jan 2009) | 1 line
Add a section
........
r69131 | andrew.kuchling | 2009-01-30 21:26:02 -0600 (Fri, 30 Jan 2009) | 1 line
Text edits and markup fixes
........
r69139 | mark.dickinson | 2009-01-31 10:44:04 -0600 (Sat, 31 Jan 2009) | 2 lines
Add an extra test for long <-> float hash equivalence.
........
r69140 | benjamin.peterson | 2009-01-31 10:52:03 -0600 (Sat, 31 Jan 2009) | 1 line
PyErr_BadInternalCall() raises a SystemError, not TypeError #5112
........
r69143 | benjamin.peterson | 2009-01-31 15:00:10 -0600 (Sat, 31 Jan 2009) | 1 line
I believe the intention here was to avoid a global lookup
........
r69154 | benjamin.peterson | 2009-01-31 16:33:02 -0600 (Sat, 31 Jan 2009) | 1 line
fix indentation in comment
........
r69155 | david.goodger | 2009-01-31 16:53:46 -0600 (Sat, 31 Jan 2009) | 1 line
markup fix
........
r69156 | gregory.p.smith | 2009-01-31 16:57:30 -0600 (Sat, 31 Jan 2009) | 4 lines
- Issue #5104: The socket module now raises OverflowError when 16-bit port and
protocol numbers are supplied outside the allowed 0-65536 range on bind()
and getservbyport().
........
r69157 | benjamin.peterson | 2009-01-31 17:43:25 -0600 (Sat, 31 Jan 2009) | 1 line
add explanatory comment
........
r69158 | benjamin.peterson | 2009-01-31 17:54:38 -0600 (Sat, 31 Jan 2009) | 1 line
more flags which only work for function blocks
........
r69159 | gregory.p.smith | 2009-01-31 18:16:01 -0600 (Sat, 31 Jan 2009) | 2 lines
Update doc wording as suggested in issue4903.
........
r69169 | guilherme.polo | 2009-01-31 20:56:16 -0600 (Sat, 31 Jan 2009) | 3 lines
Restore Tkinter.Tk._loadtk so this test doesn't fail for problems
related to ttk.
........
r69288 | georg.brandl | 2009-02-05 04:30:57 -0600 (Thu, 05 Feb 2009) | 1 line
#5153: fix typo in example.
........
r69289 | georg.brandl | 2009-02-05 04:37:07 -0600 (Thu, 05 Feb 2009) | 1 line
#5144: document that PySys_SetArgv prepends the script directory (or the empty string) to sys.path.
........
r69293 | georg.brandl | 2009-02-05 04:59:28 -0600 (Thu, 05 Feb 2009) | 1 line
#5059: fix example.
........
r69297 | georg.brandl | 2009-02-05 05:32:18 -0600 (Thu, 05 Feb 2009) | 1 line
#5015: document PythonHome API functions.
........
r69298 | georg.brandl | 2009-02-05 05:33:21 -0600 (Thu, 05 Feb 2009) | 1 line
#4827: fix callback example.
........
r69299 | georg.brandl | 2009-02-05 05:35:28 -0600 (Thu, 05 Feb 2009) | 1 line
#4820: use correct module for ctypes.util.
........
r69300 | georg.brandl | 2009-02-05 05:38:23 -0600 (Thu, 05 Feb 2009) | 1 line
#4563: disable alpha and roman lists, fixes wrong formatting of contributor list.
........
r69301 | georg.brandl | 2009-02-05 05:40:35 -0600 (Thu, 05 Feb 2009) | 1 line
#5031: fix Thread.daemon property docs.
........
r69348 | benjamin.peterson | 2009-02-05 19:47:31 -0600 (Thu, 05 Feb 2009) | 1 line
fix download link
........
2009-02-06 02:40:07 +00:00
|
|
|
# monkey-patch reST parser to disable alphabetic and roman enumerated lists
|
|
|
|
|
from docutils.parsers.rst.states import Body
|
|
|
|
|
Body.enum.converters['loweralpha'] = \
|
|
|
|
|
Body.enum.converters['upperalpha'] = \
|
|
|
|
|
Body.enum.converters['lowerroman'] = \
|
|
|
|
|
Body.enum.converters['upperroman'] = lambda x: None
|
|
|
|
|
|
2022-04-25 12:05:54 +02:00
|
|
|
|
2018-09-18 17:55:44 -04:00
|
|
|
class PyAwaitableMixin(object):
|
|
|
|
|
def handle_signature(self, sig, signode):
|
|
|
|
|
ret = super(PyAwaitableMixin, self).handle_signature(sig, signode)
|
|
|
|
|
signode.insert(0, addnodes.desc_annotation('awaitable ', 'awaitable '))
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
2020-09-18 18:22:36 +09:00
|
|
|
class PyAwaitableFunction(PyAwaitableMixin, PyFunction):
|
2018-09-18 17:55:44 -04:00
|
|
|
def run(self):
|
|
|
|
|
self.name = 'py:function'
|
2020-09-18 18:22:36 +09:00
|
|
|
return PyFunction.run(self)
|
2018-09-18 17:55:44 -04:00
|
|
|
|
|
|
|
|
|
2020-09-18 18:22:36 +09:00
|
|
|
class PyAwaitableMethod(PyAwaitableMixin, PyMethod):
|
2018-09-18 17:55:44 -04:00
|
|
|
def run(self):
|
|
|
|
|
self.name = 'py:method'
|
2020-09-18 18:22:36 +09:00
|
|
|
return PyMethod.run(self)
|
2018-09-18 17:55:44 -04:00
|
|
|
|
|
|
|
|
|
2008-07-23 15:19:11 +00:00
|
|
|
# Support for documenting Opcodes
|
|
|
|
|
|
2010-07-03 10:41:33 +00:00
|
|
|
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
|
2008-07-23 15:19:11 +00:00
|
|
|
|
2014-10-30 22:55:13 +01:00
|
|
|
|
2008-07-23 15:19:11 +00:00
|
|
|
def parse_opcode_signature(env, sig, signode):
|
|
|
|
|
"""Transform an opcode signature into RST nodes."""
|
|
|
|
|
m = opcode_sig_re.match(sig)
|
|
|
|
|
if m is None:
|
|
|
|
|
raise ValueError
|
|
|
|
|
opname, arglist = m.groups()
|
|
|
|
|
signode += addnodes.desc_name(opname, opname)
|
2010-07-03 10:41:33 +00:00
|
|
|
if arglist is not None:
|
|
|
|
|
paramlist = addnodes.desc_parameterlist()
|
|
|
|
|
signode += paramlist
|
|
|
|
|
paramlist += addnodes.desc_parameter(arglist, arglist)
|
2008-07-23 15:19:11 +00:00
|
|
|
return opname.strip()
|
|
|
|
|
|
|
|
|
|
|
2010-11-12 08:57:12 +00:00
|
|
|
# Support for documenting pdb commands
|
|
|
|
|
|
2010-07-18 10:11:03 +00:00
|
|
|
pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)')
|
|
|
|
|
|
|
|
|
|
# later...
|
2014-10-30 22:55:13 +01:00
|
|
|
# pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers
|
2010-07-18 10:11:03 +00:00
|
|
|
# [.,:]+ | # punctuation
|
|
|
|
|
# [\[\]()] | # parens
|
|
|
|
|
# \s+ # whitespace
|
|
|
|
|
# ''', re.X)
|
|
|
|
|
|
2014-10-30 22:55:13 +01:00
|
|
|
|
2010-07-18 10:11:03 +00:00
|
|
|
def parse_pdb_command(env, sig, signode):
|
|
|
|
|
"""Transform a pdb command signature into RST nodes."""
|
|
|
|
|
m = pdbcmd_sig_re.match(sig)
|
|
|
|
|
if m is None:
|
|
|
|
|
raise ValueError
|
|
|
|
|
name, args = m.groups()
|
|
|
|
|
fullname = name.replace('(', '').replace(')', '')
|
|
|
|
|
signode += addnodes.desc_name(name, name)
|
|
|
|
|
if args:
|
|
|
|
|
signode += addnodes.desc_addname(' '+args, ' '+args)
|
|
|
|
|
return fullname
|
|
|
|
|
|
|
|
|
|
|
2023-10-18 16:15:42 -07:00
|
|
|
def parse_monitoring_event(env, sig, signode):
|
|
|
|
|
"""Transform a monitoring event signature into RST nodes."""
|
|
|
|
|
signode += addnodes.desc_addname('sys.monitoring.events.', 'sys.monitoring.events.')
|
|
|
|
|
signode += addnodes.desc_name(sig, sig)
|
|
|
|
|
return sig
|
|
|
|
|
|
|
|
|
|
|
2023-05-08 20:01:25 +01:00
|
|
|
def patch_pairindextypes(app, _env) -> None:
|
|
|
|
|
"""Remove all entries from ``pairindextypes`` before writing POT files.
|
|
|
|
|
|
|
|
|
|
We want to run this just before writing output files, as the check to
|
|
|
|
|
circumvent is in ``I18nBuilder.write_doc()``.
|
|
|
|
|
As such, we link this to ``env-check-consistency``, even though it has
|
|
|
|
|
nothing to do with the environment consistency check.
|
|
|
|
|
"""
|
2023-05-04 08:11:09 +01:00
|
|
|
if app.builder.name != 'gettext':
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# allow translating deprecated index entries
|
|
|
|
|
try:
|
|
|
|
|
from sphinx.domains.python import pairindextypes
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
# Sphinx checks if a 'pair' type entry on an index directive is one of
|
|
|
|
|
# the Sphinx-translated pairindextypes values. As we intend to move
|
|
|
|
|
# away from this, we need Sphinx to believe that these values don't
|
|
|
|
|
# exist, by deleting them when using the gettext builder.
|
2023-05-08 20:01:25 +01:00
|
|
|
pairindextypes.clear()
|
2023-05-04 08:11:09 +01:00
|
|
|
|
|
|
|
|
|
2008-04-10 03:06:53 +00:00
|
|
|
def setup(app):
|
2018-10-13 08:14:08 +02:00
|
|
|
app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature)
|
|
|
|
|
app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command)
|
2023-10-18 16:15:42 -07:00
|
|
|
app.add_object_type('monitoring-event', 'monitoring-event', '%s (monitoring event)', parse_monitoring_event)
|
2018-09-18 17:55:44 -04:00
|
|
|
app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction)
|
|
|
|
|
app.add_directive_to_domain('py', 'awaitablemethod', PyAwaitableMethod)
|
2023-05-08 20:01:25 +01:00
|
|
|
app.connect('env-check-consistency', patch_pairindextypes)
|
2014-09-30 22:17:41 +02:00
|
|
|
return {'version': '1.0', 'parallel_read_safe': True}
|