Skip to content

Commit ae47888

Browse files
authored
pythonGH-121970: Extract misc_news into a new extension (python#129577)
1 parent 0612a89 commit ae47888

File tree

8 files changed

+85
-50
lines changed

8 files changed

+85
-50
lines changed

Doc/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'changes',
2929
'glossary_search',
3030
'lexers',
31+
'misc_news',
3132
'pydoc_topics',
3233
'pyspecific',
3334
'sphinx.ext.coverage',

Doc/make.bat

+4-6
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,14 @@ goto end
127127
:build
128128
if not exist "%BUILDDIR%" mkdir "%BUILDDIR%"
129129

130-
rem PY_MISC_NEWS_DIR is also used by our Sphinx extension in tools/extensions/pyspecific.py
131-
if not defined PY_MISC_NEWS_DIR set PY_MISC_NEWS_DIR=%BUILDDIR%\%1
132-
if not exist "%PY_MISC_NEWS_DIR%" mkdir "%PY_MISC_NEWS_DIR%"
130+
if not exist build mkdir build
133131
if exist ..\Misc\NEWS (
134-
echo.Copying Misc\NEWS to %PY_MISC_NEWS_DIR%\NEWS
135-
copy ..\Misc\NEWS "%PY_MISC_NEWS_DIR%\NEWS" > nul
132+
echo.Copying existing Misc\NEWS file to Doc\build\NEWS
133+
copy ..\Misc\NEWS build\NEWS > nul
136134
) else if exist ..\Misc\NEWS.D (
137135
if defined BLURB (
138136
echo.Merging Misc/NEWS with %BLURB%
139-
%BLURB% merge -f "%PY_MISC_NEWS_DIR%\NEWS"
137+
%BLURB% merge -f build\NEWS
140138
) else (
141139
echo.No Misc/NEWS file and Blurb is not available.
142140
exit /B 1

Doc/tools/extensions/misc_news.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Support for including Misc/NEWS."""
2+
3+
from __future__ import annotations
4+
5+
import re
6+
from pathlib import Path
7+
from typing import TYPE_CHECKING
8+
9+
from docutils import nodes
10+
from sphinx.locale import _ as sphinx_gettext
11+
from sphinx.util.docutils import SphinxDirective
12+
13+
if TYPE_CHECKING:
14+
from typing import Final
15+
16+
from docutils.nodes import Node
17+
from sphinx.application import Sphinx
18+
from sphinx.util.typing import ExtensionMetadata
19+
20+
21+
BLURB_HEADER = """\
22+
+++++++++++
23+
Python News
24+
+++++++++++
25+
"""
26+
27+
bpo_issue_re: Final[re.Pattern[str]] = re.compile(
28+
"(?:issue #|bpo-)([0-9]+)", re.ASCII
29+
)
30+
gh_issue_re: Final[re.Pattern[str]] = re.compile(
31+
"gh-(?:issue-)?([0-9]+)", re.ASCII | re.IGNORECASE
32+
)
33+
whatsnew_re: Final[re.Pattern[str]] = re.compile(
34+
r"^what's new in (.*?)\??$", re.ASCII | re.IGNORECASE | re.MULTILINE
35+
)
36+
37+
38+
class MiscNews(SphinxDirective):
39+
has_content = False
40+
required_arguments = 1
41+
optional_arguments = 0
42+
final_argument_whitespace = False
43+
option_spec = {}
44+
45+
def run(self) -> list[Node]:
46+
# Get content of NEWS file
47+
source, _ = self.get_source_info()
48+
news_file = Path(source).resolve().parent / self.arguments[0]
49+
self.env.note_dependency(news_file)
50+
try:
51+
news_text = news_file.read_text(encoding="utf-8")
52+
except (OSError, UnicodeError):
53+
text = sphinx_gettext("The NEWS file is not available.")
54+
return [nodes.strong(text, text)]
55+
56+
# remove first 3 lines as they are the main heading
57+
news_text = news_text.removeprefix(BLURB_HEADER)
58+
59+
news_text = bpo_issue_re.sub(r":issue:`\1`", news_text)
60+
# Fallback handling for GitHub issues
61+
news_text = gh_issue_re.sub(r":gh:`\1`", news_text)
62+
news_text = whatsnew_re.sub(r"\1", news_text)
63+
64+
self.state_machine.insert_input(news_text.splitlines(), str(news_file))
65+
return []
66+
67+
68+
def setup(app: Sphinx) -> ExtensionMetadata:
69+
app.add_directive("miscnews", MiscNews)
70+
71+
return {
72+
"version": "1.0",
73+
"parallel_read_safe": True,
74+
"parallel_write_safe": True,
75+
}

Doc/tools/extensions/pyspecific.py

-41
Original file line numberDiff line numberDiff line change
@@ -141,46 +141,6 @@ def run(self):
141141
return PyMethod.run(self)
142142

143143

144-
# Support for including Misc/NEWS
145-
146-
issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)', re.I)
147-
gh_issue_re = re.compile('(?:gh-issue-|gh-)([0-9]+)', re.I)
148-
whatsnew_re = re.compile(r"(?im)^what's new in (.*?)\??$")
149-
150-
151-
class MiscNews(SphinxDirective):
152-
has_content = False
153-
required_arguments = 1
154-
optional_arguments = 0
155-
final_argument_whitespace = False
156-
option_spec = {}
157-
158-
def run(self):
159-
fname = self.arguments[0]
160-
source = self.state_machine.input_lines.source(
161-
self.lineno - self.state_machine.input_offset - 1)
162-
source_dir = getenv('PY_MISC_NEWS_DIR')
163-
if not source_dir:
164-
source_dir = path.dirname(path.abspath(source))
165-
fpath = path.join(source_dir, fname)
166-
self.env.note_dependency(path.abspath(fpath))
167-
try:
168-
with io.open(fpath, encoding='utf-8') as fp:
169-
content = fp.read()
170-
except Exception:
171-
text = 'The NEWS file is not available.'
172-
node = nodes.strong(text, text)
173-
return [node]
174-
content = issue_re.sub(r':issue:`\1`', content)
175-
# Fallback handling for the GitHub issue
176-
content = gh_issue_re.sub(r':gh:`\1`', content)
177-
content = whatsnew_re.sub(r'\1', content)
178-
# remove first 3 lines as they are the main heading
179-
lines = ['.. default-role:: obj', ''] + content.splitlines()[3:]
180-
self.state_machine.insert_input(lines, fname)
181-
return []
182-
183-
184144
# Support for documenting Opcodes
185145

186146
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
@@ -268,6 +228,5 @@ def setup(app):
268228
app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction)
269229
app.add_directive_to_domain('py', 'awaitablemethod', PyAwaitableMethod)
270230
app.add_directive_to_domain('py', 'abstractmethod', PyAbstractMethod)
271-
app.add_directive('miscnews', MiscNews)
272231
app.connect('env-check-consistency', patch_pairindextypes)
273232
return {'version': '1.0', 'parallel_read_safe': True}

Doc/whatsnew/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.. _changelog:
22

3+
.. default-role:: py:obj
4+
35
+++++++++
46
Changelog
57
+++++++++

Misc/NEWS.d/3.5.3rc1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ after a commit.
11461146
.. section: Library
11471147
11481148
A new version of typing.py from https://github.com/python/typing:
1149-
Collection (only for 3.6) (Issue #27598). Add FrozenSet to __all__
1149+
Collection (only for 3.6) (issue #27598). Add FrozenSet to __all__
11501150
(upstream #261). Fix crash in _get_type_vars() (upstream #259). Remove the
11511151
dict constraint in ForwardRef._eval_type (upstream #252).
11521152

Misc/NEWS.d/3.6.0a4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Support keyword arguments to zlib.decompress(). Patch by Xiang Zhang.
177177
.. section: Library
178178
179179
Prevent segfault after interpreter re-initialization due to ref count
180-
problem introduced in code for Issue #27038 in 3.6.0a3. Patch by Xiang
180+
problem introduced in code for issue #27038 in 3.6.0a3. Patch by Xiang
181181
Zhang.
182182

183183
..

Misc/NEWS.d/3.6.0b1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ chunked transfer-encoding.
11371137
.. section: Library
11381138
11391139
A new version of typing.py from https://github.com/python/typing: -
1140-
Collection (only for 3.6) (Issue #27598) - Add FrozenSet to __all__
1140+
Collection (only for 3.6) (issue #27598) - Add FrozenSet to __all__
11411141
(upstream #261) - fix crash in _get_type_vars() (upstream #259) - Remove the
11421142
dict constraint in ForwardRef._eval_type (upstream #252)
11431143

0 commit comments

Comments
 (0)